2
int x=4, y=0;
y=(++x)+(++x);
printf("%d\n%d",x,y);

In this program y should be 11 and x should be 6, but my C program prints y=12,x=6 and my Java program prints y=11, x=6. I am very much confused and need to know why the C program prints y=12?

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
Shubhanshu
  • 73
  • 5
  • does c language take ++x as incremented x? and when second ++x increment the actual value of x increments. may be due to that both x has value 6 and prints 12.. – Shubhanshu Jun 27 '13 at 04:02
  • i am using code::block IDE which uses GNU GCC compiler. i also use Visual Studio 2010, but both prints 12. – Shubhanshu Jun 27 '13 at 04:05
  • 2
    @egrunin C could emit flying monkeys if it wanted. It's undefined behavior. – Kevin Jun 27 '13 at 04:06
  • It is undefined, as shown in the duplicate answer. @Kevin teach how to make c do that, please! – Fantastic Mr Fox Jun 27 '13 at 04:06
  • Just don't write code like this and you should be fine – aaronman Jun 27 '13 at 04:06
  • @Kevin i dont think any language should have undefined nature. there are rules, and that a language should follow. so if it is undefined then why?? – Shubhanshu Jun 27 '13 at 04:08
  • @Kevin - it is more traditional to emit nasal demons - http://www.catb.org/jargon/html/N/nasal-demons.html – Stephen C Jun 27 '13 at 04:08
  • 1
    @user2526511 - *"i dont think any language should have undefined nature."*. Unfortunately, C is not like that. BTW this undefined behaviour is specifically described in the C language standards. It is a historical thing that goes waaaay back in the history of the C language and its implementations. It is too engrained to fix. C programmers just need to beware ... and avoid writing daft code like that. – Stephen C Jun 27 '13 at 04:12
  • @StephenC i have got the link in previous comment regarding this undefined nature of c.. thanx. – Shubhanshu Jun 27 '13 at 04:14
  • @StephenC but if i have to calculate on paper for any exam or something then should i go for 11 or 12.. i hope 11. – Shubhanshu Jun 27 '13 at 04:15
  • 1
    @user2526511 - If you get an exam question that asks you what the value is, the CORRECT answer is "The C standards say that the value of `x` and `y` after evaluating that expression are unspecified." – Stephen C Jun 27 '13 at 04:18
  • @Kevin now i got that kevin. thanx. but for any exam what should i mark, 11 or 12. – Shubhanshu Jun 27 '13 at 04:19
  • @StephenC, what should one do when facing such question in multiple-choice question without that choice ? I face it alot:( – VoidPointer Jun 27 '13 at 04:19
  • 1
    @VoidPointer - What do you do in **ANY** multiple choice question where none of the answers is correct? Choose the answer that you think the lecturer expects ... and lodge a formal complaint at the end of the exam. (But you'd better be right ... or your formal complaint will look rather silly.) – Stephen C Jun 27 '13 at 04:21
  • @VoidPointer same problem, what about multiple choice question where only 11 and 12 in option. – Shubhanshu Jun 27 '13 at 04:21
  • my choice would be `option C` always in those cases :) – VoidPointer Jun 27 '13 at 04:25
  • The reason it's left undefined is that on some architectures it might be faster, more efficient, or historically implemented to do it different ways on different architectures. C was standardised to be fast and implementable on (*not* the same as portable to) as many machines as possible, not easy to write bug-free. – Kevin Jun 27 '13 at 04:26
  • " but for any exam what should i mark, 11 or 12" -- Suppose the exam asks "How many letters do words of the English language have?" ... what answer should you give? – Jim Balter Jun 27 '13 at 05:46
  • 1
    " what about multiple choice question where only 11 and 12 in option" -- they're both wrong, so sue the teacher. – Jim Balter Jun 27 '13 at 05:47
  • @StephenC " It is too engrained to fix" -- it isn't broken. C intentionally does not specify the order of operations between sequence points because doing so would impact performance in some circumstances. If you want a language with different design criteria, there are plenty of them available. – Jim Balter Jun 27 '13 at 05:53
  • if I made my code with static int x=4, then my answer is 11, why? – Shubhanshu Jun 27 '13 at 06:23
  • @JimBalter - In this age of clever optimizing compilers, it is not at all obvious that tying down the semantics of expressions *would* impact on performance. And the flip-side of your argument is that a portable C program has to avoid these "undefined" constructs anyway ... negating any hypothetical performance advantages they might provide. – Stephen C Jun 27 '13 at 10:09
  • The history is that the first few incarnations of the C language were designed and implement with *little regard* for portability. The need for portability only became apparent much later (when people tried to port V6/V7 Unix apps from PDP-11 to other architectures. By then the problems were too "rusted on" to be fixable. And many generations of C standards have left the problem alone. There is no doubt that it is a major limitation / problem for people who need to write portable applications in C ... and that includes operating system writers. – Stephen C Jun 27 '13 at 10:15
  • @StephenC *it is not at all obvious that tying down the semantics of expressions would impact on performance* -- fallacy of denial of the antecedent. *a portable C program has to avoid these "undefined" constructs anyway ... negating any hypothetical performance advantages they might provide* -- nonsense; if a specific order of evaluation is desired, it's easy to obtain it. You don't understand what the performance advantages are ... learn about code generation, pipelines, and so on. – Jim Balter Jun 27 '13 at 10:16
  • 1
    @StephenC *By then the problems were too "rusted on" to be fixable.* -- This is potted history that has nothing to do with reality. The C standards committee (of which I was a member) was heavily loaded with representatives of compiler vendors, and many of the decisions about undefined and implementation-defined behavior were explicitly made with performance in mind. – Jim Balter Jun 27 '13 at 10:18
  • "You don't understand what the performance advantages are" -- To explain further: the performance advantages are not for the calculation of expressions with ambiguous values like `++x + ++x` ... since they're ambiguous, it would be useless to optimize them. No, the performance advantages are for expressions with values that *aren't* ambiguous regardless of the order of evaluation. The evaluation rules required to be able to freely (and efficiently) optimize *those* expressions necessitate that expressions like `++x + ++x` are ambiguous and thus declared to have undefined behavior. – Jim Balter Jun 27 '13 at 10:31
  • Also, rules like Java's that the arguments to functions be evaluated left to right mean that some implementations cannot use the most efficient and natural calling sequence defined by the hardware or system conventions ... which is why the C standard doesn't mandate the order of evaluation of function arguments. – Jim Balter Jun 27 '13 at 10:38
  • Define 'should be'. Where does it say that the Java and C operators operate exactly the same way? – user207421 Jun 27 '13 at 12:17
  • IMO, the correct decision would have been to rule that all cases where the expression semantics are "ambiguous" or "unspecified" are in fact *illegal* C code. That has ZERO performance impact ... assuming that programmers don't want or need to write ambiguous programs. The fact that the standards committees wouldn't or couldn't take that obvious step is a **clear example** of the rusted on nature of the problem. – Stephen C Jun 27 '13 at 13:58
  • And if it is actually *necessary* to write ambiguous programs in order to get optimal performance, that says something about either the language itself, or the compilers, or both. And I find it hard to accept that this would be true for modern high quality optimizing C compilers. – Stephen C Jun 27 '13 at 14:04

0 Answers0