My understanding of the whole sequence points thing is basic. All I have is some crude intuitive idea that "once a sequence point is encountered, we can be sure all side effects of previous evaluations are complete". I also read that in statements like printf("%d",a++,++a,a++)
the behavior is undefined as a comma doesn't signify a sequence point while a semi-colon does. So instead of making guesses and going by intuition, I feel a very rigorous and conclusive answer to this will help me a lot.
So are the following kind of statements safe & certain in C:
int a=4,*ptr=&a; //Statement 1
x+=4,y=x*2; //Statement 2 (Assume x and y are integer variables)
If yes, how? Especially in the second case, if a comma is not a sequence point, how can we be sure x
has been incremented by 4
before we use it in the assignment y=x*2
? And for the first statement, how can I be sure a
has been initialized and allocated memory before I assign its address to ptr
? Should I play safe and use the following for the above:
int a=4,*ptr;
ptr=&a;
and
x+=4;
y=x*2;
Edit My understanding of the comma operator tells me that those statements are safe. But after reading about sequence points and how something like printf("%d",a++,++a,a++)
is undefined, I am having second thoughts.