mercredi 12 décembre 2018

How comma and unary opertors work in c/c++ [duplicate]

This question already has an answer here:

  #include<stdio.h> 
  int main() 
  { 
       int i =1,j=1,k=1;
       i = (++i + i++);
       j = (++j + j++,j++);
       k = (++k + k++,k++,k);
       printf("\n\ni=%d",i);
       printf("\nj=%d",j);
       printf("\nk=%d",k);
       return 0; 
 }


 **OUTPUT**
 i=5
 j=3
 k=4

sequence of steps for i:
1.initialy i=1 ==> ++i gives 2
2.i=2 ==> i++ gives 2 on next step i will be incremented;
3.( 2(from step 1) + 2(from step 2) ) + 1

There are 3 increment operators in j
so j should output 4 right?


but k outputs 4.

I don't understand the sequence.
How it is working?

Aucun commentaire:

Enregistrer un commentaire