2

I can't understand how this works and why it produces the following output.

int main()
{
    int i=0;
    int a=5;
    int x=0;

    for(i=0; i<5; x=(i++,a++))
    {
        printf("i=%d a=%d x=%d\n",i,a,x);
    }
}

this give as output:

i=0 a=5 x=0
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Gopal
  • 603
  • 10
  • 18

5 Answers5

3

Forget about the ++ for the moment, they're just a distraction.

int a = 5;
int i = 0;    
int x = (i, a);

Sets the value of x to 5. The i is evaluated and discarded, then the a is evaluated and assigned to x.

In your loop, the post-increment a++ does what it always does; returns the current value and then increments the variable. So x takes the value of a before it is incremented, then a's value is increased by 1. Because i++ was also evaluated, its value increases as well.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

I suppose you understand a for loop like this:

for(i=0; i<5; i++)

you might as well want to increment two variables; if you separate them with a comma; like this:

for(i=0; i<5; i++,a++)

at the start of each loop, both i and a will be incremented. Now only the expression x=(i++,a++) needs to be explained: x gets the last of both values assigned. It is the same as writing:

x=i++;
x=a++;

of course a is post-incremented so first its value is assigned to x; and only then a is incremented.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
1

It is simple both the operators that you have used are 'Post Increment', this means that assignment will happen first then the increment in variable will happen. Let us understand with an example, Suppose,

i=5
x=i++;
printf("%d", x)

The above code will give output as 5 and

i=5
x=++i;
printf("%d", x)

and this will give output as 6.

Hope you understand the logic.

Lovel_leo
  • 15
  • 4
1

The comma operator means: execute all, and return the last value;

Example:

A = (b+=1,c+2,d+1);

This sentence will add 1 to b, sum 2 to c (but will not modify it) and sum 1 to d. Hence d+1 is the last expression A will be set to its value.

The post and pre operator ++ are tricky.

A++ means: return the value of A and after that, you increment it. ++A means: increment A and then return the value of it.

so if A=2

b=A++; // set b to 2 and increments A one unity
b=++A; // increments A to 4 and sets b to 4
MasterID
  • 1,510
  • 1
  • 11
  • 15
1

Consider your example and the output (except I made the initial value of x to be 22):

int i=0;
int a=5;
int x=22;

for(i=0; i<5; x=(i++,a++))
{
    printf("i=%d a=%d x=%d\n",i,a,x);
}

Prints:

i=0 a=5 x=22
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8

Notice that x has either the initial value of x prior to the loop or the previous value from the last trip through the loop.

Recall that any for loop can be expressed as an equivelent while loop.

The for loop of:

for(exp 1; exp 2; exp 3){
    expressions
}

Is equivalent to:

exp 1;
while(exp 2){
   expressions
   exp 3;
}

So your for loop can be written as:

int i=0;              // exp 1 from the for loop
int a=5;
int x=22;

while(i<5){           // exp 2
    // loop body
    printf("i=%d a=%d x=%d\n",i,a,x);

    x=(i++,a++);      // exp 3 from the for loop
}

Prints same output.

The fact that exp 3 is evaluated at the end of the loop (whether it is a for or while loop) is why x has the previous value of x in the body of the loop.

The final thing to consider is the comma operator. The expression:

i=(a+=2, a+b)
   ^^^               evaluate a then add 2
       ^             comma operator in this case
          ^^         add b to a

 ^                   store the final expression -- the RH of the comma operator -- 
                     into i
dawg
  • 98,345
  • 23
  • 131
  • 206