big hero
big hero

Reputation: 11

How does the while loop work when the assignment and postfix operators are used in its condition

I found this code in a book on pointers and the book explains for the following code that. First the value at the address stored in ss replaces the value at the address stored it tt. After the assignment, the test is carried out to decide whether the while loop should continue or not. Since *tt gives 'l' which is the true value. Next both ss and tt are incremented.

My questions are:

  1. Why does the while loop first assigns the value and then check for true or false?
  2. Why is the value at tt checked, I mean why particularly tt?
  3. Why are ss and tt incremented after checking the condition; why not just after assigning the value?
int main()
{ 
    char s[]="lumps, bumps, swollen veins, new pains";
    char t[40];
    char *ss,*tt;
    tt=t;
    ss=s;
    while (*tt++ = *ss++ );
    printf("%s ",t);
}

output:

lumps, bumps, swollen veins, new pains

Upvotes: 1

Views: 1243

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

An expression like this

*ss++

can be equivalently rewritten like

*( ss++ )

That is the postfix increment operator ++ has a higher priority than the unary operator *.

According to the C Standard (6.5.2.4 Postfix increment and decrement operators, p. #2)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). ... The value computation of the result is sequenced before the side effect of updating the stored value of the operand....

So if for example the pointer ss points to the first character of the string s then the value of the expression ss++ is this address and only after returning the address the pointer is incremented.

Then the returned pointer that is the value of the expression ss++ is dereferenced and the value of the expression *( ss++ ) is the first character of the string stored in the array s.

Similarly the value of the expression *( tt++ ) is the object pointed to by the pointer tt before its incrementing.

So the first character of the array t is assigned with the first character of the array s.

Further according to the C Standard (6.5.16 Assignment operators, p. #3)

3 An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,111) but is not an lvalue....

So in the first step of the loop the value of the assignment operator is the character 'l'.

And at last (6.8.5 Iteration statements, p. #4)

4 An iteration statement causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0...

As the result of the assignment expression (the character 'l') is not equal to 0 then the loop continues. It will be executed until the expression *( ss++ ) returns the terminating zero character '\0' of the array s that is assigned to the array t using the assignment expression *tt++ = *ss++.

As in this case the result of the assignment expression is the character '\0' (that is promoted to 0 of the type int) then the loop stops its iterations.

As result the array t contains the string stored in the array s.

Upvotes: 0

Blaze
Blaze

Reputation: 16876

  1. why does the while loop first assigns the value and then check for true or false 2.

It copies the char, and then it checks if the copied char was the null terminator. If it is, then it evaluates to 0 (or false) and the loop stops.

why value at tt is checked, i mean why particularly tt.

Actually it's the result of the = that is checked, which is equal to the value that was assigned. So if *ss is '\0', then the = returns '\0' and the loop stops, because '\0' is equal to 0 or false.

why ss and tt are increment after checking the condition why not just after assigning the value

This is postifix increment, so the assignment (=) is done with the old values, not the incremented values.

Upvotes: 1

Related Questions