+ 2

can anyone explain me this c code?

int n =3,l,k,j; n=++n + ++n; printf("%d\n",n); l=++n; k=++n; j=l+k; printf("%d\n",j); why n value is 10 and j value is 9?

28th Apr 2022, 4:15 AM
đŸŒčLOVEđŸŒč
đŸŒčLOVEđŸŒč - avatar
2 Answers
+ 4
You can stop reading after line 2. From thereon after, the behaviour of your program is undefined, because of a sequence point violation. That means, in your second line you have multiple unsynchronised read and modification operations on n. The only thing you can do is ask why your compiler in its current version for your operating system with the given compilation flags produces these values. But that I evidentally cannot answer. You need to compile your program down to assembler and study the output.
28th Apr 2022, 7:23 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
You are trying to change value of an object more than once between sequence points that is giving you undefined behaviour. As it imply the behaviour is undefined it can give any value See this for more info: http://c-faq.com/expr/seqpoints.html
28th Apr 2022, 8:28 PM
Stuvan
Stuvan - avatar