You are here:

C++/C Language

Advertisement


Question
‎#include <stdio.h>
main()
{
int n=1;

printf("%d %d %d", ++n, ++n, ++n);
}

what will be the output of this program some compiler gives 444 n turbo c gives 432 confused!!!

Answer
The program is ill-formed and results in undefined behaviour.

printf("%d %d %d", ++n, ++n, ++n);

violates C rules by modifying n more than once between sequence points.

To quote the C standard:

"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression."

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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.