Monday, June 2, 2008

Sequence point

The question was
What will be the output of the following program in UNIX OS with CC compiler and TC compiler?
int main()
{int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}
If any difference then Why it is difference?

The answer is the program is wrong. Unfortunately the program will compile and run and show some result.

According to C standard you can not modify the variable more than once in between two sequence points. Here you are modifying the variable i 5 times. Please note that + addition operation is not a sequence point. Only logical operator && and and the complete expression and function call are sequence points.

At a sequence point, all variable updates and side-effects since the previous sequence point are guaranteed to have taken place.

Read more about sequence points here.

http://c-faq.com/expr/seqpoints.html

and here http://en.wikipedia.org/wiki/Sequence_point . There is one more link to "THe C book" But currently i am unable to open it.



No comments: