+7

A question about ++ in c programming

#include <stdio.h> int main() { int a=10; printf("%d%d%d%d",a++,++a,a,a++); return 0; } Output is 12131310,why? plz explain,thanks update: I know Undefined behavior, but I am curious what the compiler did, how to get 12131310

c

12/4/2018 1:53:38 PM

Alex

25 Answers

New Answer

+4

Alex, actually I talked with Kishalaya Saha about this earlier. He came up with an interpretation of what MIGHT be happening here: 1.) The values are determined from right to left; 2.) results will be held back till the end of the calculations 3.) except its a post-increment, then the result will be dashed out on the spot. So it starts with a post-inc - 10 is determined immediately; then there's just a and next a with pre-inc, so the temp results will be held back; finally there's a post-inc again, so the temp result 12 is put to slot 1; now the calculation is over and the last remaining results - a is 13 now - end up in the middle slots. We were both not in the mood to test this assumption with other values yet.

+8

UB ¹ is a very interesting answer! The compiler log "warning: operation on 'a' may be undefined [-Wsequence-point]" Reason: "When modifying an object between two sequence points, reading the value of the object for any other purpose than determining the value to be stored is also undefined behavior." ______ ¹ https://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C_and_C++

+7

HonFu: thank you

+7

For those who are believing the false evidence! https://blog.regehr.org/archives/213

+6

*Asterisk* :I also got this result with other compilers,Oh, I got it wrong. I got 10131312 with other compilers. The order is different. It should be a compiler implementation, but why are these results? I am curious.

+6

"it is ultimately useless" Useful for language designers and compiler writers. You may someday become one! 8D "we can not rely on this or any other outcome to consistently happen" This is for us (at least for now)! 8P AND this quote from C FAQ in regard to UB: "Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended."

+5

I'm excited to read the answer myself. :)

+5

C++ Soldier (Babak), may I summarize like this: Even if we figure out how a compiler may have handled an undefined operation in a specific case (party game ^^), for real life purposes it is ultimately useless because we can not rely on this or any other outcome to consistently happen.

+5

To provide a little more direction for those attempting to find predictability in the unpredictable nature of undefined behavior, I recommend reading more about sequence points (https://en.m.wikipedia.org/wiki/Sequence_point) and then checkout Part 3 of the article posted by C++ Soldier (Babak) which can be found in this link: https://blog.regehr.org/archives/232 Look for the section titled: "A Technical Note on Side Effects in C and C++"

+4

Ahmad AL-qorashi don't ask your question in other's tread! I told you, if you try your best to provide us something, we will definitely help you out.

+3

C++ solider:Thank you, I know UB, I know that I shouldn't write programs like this, but I just want to know what the compiler does, why is there such a result?

+3

HonFu: I read the above link, I know this is the problem of Undefined behavior, but I am curious what the compiler did, how to get the above output

+3

C++ Soldier (Babak) Excellent article! It might be one of the best I've read on Undefined Behavior in compilers.

+3

David We are going to hang ourselves if these people don't budge! 8D

+2

I suppose, The C-oldier is right and it's just undefined (meaning a different compiler might do different stuff).

+1

the result is 10121212 an not 12131310

+1

*Asterisk*, have you tried it at home? I tried it on Sololearn today and it gave the op's values.

+1

HonFu yeh i did based on the normal standards of increementing a value and then i try it on my compiler with my device i get the same result

+1

ooops!!! HonFu i think the error is from the code play ground....

+1

yeh.... such do happen