C program different Output on different Compilers? [duplicate] - c

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
Here is the C program which is giving different output depending on Compiler used:
#include<stdio.h>
int main()
{
int i = 5,j;
j = ++i + i++ + ++i + i++;
printf("%d",j);
return 0;
}
Check the output on the following link.
http://imgur.com/z9aMSwj,Vwx3P9S
http://imgur.com/z9aMSwj,Vwx3P9S#1
My question is what is the technical reason that output is different?

The technical reason is that there is no defined behaviour for such an operation, allowing compilers to handle this kind of an order as they wish. Such cases are generally referred as Undefined Behaviour.

According to C language, expressions like ++i + i++ + ++i + i++ have undefined behavior.

In a i++ expression, two things happen. First, the expression is evaluated to i, then i is incremented later. The behaviour of 'later' is undefined. If there are 4 such expressions in a statement, there are countless possible orders to evaluate the increments and decrements.

Related

Rewriting a piece of code to become more readable [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
sequence points in c
(4 answers)
Closed 1 year ago.
val = n++ + arr[n];
How can I rewrite the line of code above to become more readable?
How is this code evaluated by a compiler?
This code is invalid (reason) so you should bin it.
It is better to write more lines to keep the code readable and correct than to write "hacky" complex expressions.
val = n + arr[n];
n++;
It's not a matter of readability. That's undefined behavior.
In C + is not a sequence point, therefore you can't know if n++ will be executed before or after arr[n]
Sequence points in the C standard
See the section relative to Program execution
The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B. (A summary of the sequence points is given in annex C.)
It depend on the case, You can do like this.
val = n + arr[n + 1];
n++;

C compiler's pre/post incrementation evaluation in expressions [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
Today I found something, that made me very anxious about my C++ or basic programming skills. The problem is C++ expression evaluation with post/pre incrementation.
Let's check this, let me say that, trivial example of code:
int a = 5;
int d = a++ + a;
As far as I expected, left and right operands of '=' sign would be calucalted independently, and the final result is (a++) 5 + (a) 5, where post-incremented 'a' has value of 6after 'd' is computed.
But, here's what I got under two popular C compilers:
MinGW: d == 11;
MSVC: d == 10;
Same situation is with:
int a = 5;
int d = a-- + a;
where compilers gave:
MinGW: d == 9; // 5 + 4 , a=4 after 'a--', before '+a'?
MSVC: d == 10; // 5 + 5 , a=4 after 'a-- + a'?
MSVC out is exact as what I expected. Question is what is really happening here? Which compiler is closer to the behaviour defined as standard?
Funny that you should ask about the "behaviour defined as standard"; in fact, both compilers adhere perfectly to the standard, since your programs invoke undefined behaviour.
In a nutshell, the operands to + (and most other binary operators) are unsequenced relative to each other: they can be evaluated in either order, and depending on a particular order (via side effects) invokes undefined behaviour.
With undefined behaviour, of course, a conforming compiler can choose to do anything, legally.
The order of execution for the expression a++ + a is unspecified by the C++ standard, so each compiler is free to evaluate the expression however it wants. Since both are compilers are correct, you need to rewrite your expression into two separate statements to get the particular behavior that you want.

Increment and decrement operator [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
Here is a code
#include<stdio.h>
int main()
{
int i=5;
printf("%d%d",++i,++i);
}
I am not understand, why the output is 77?
++i and i++ are expressions that have side effects. Using two of these in the same expression results in undefined behavior. Basically, anything goes.
Specifically, I'm guessing the compiler says you want to increment i twice and then use the result, so it evaluates ++i twice, resulting in 7, and then sends that to printf.

Postfix and prefix operators as function arguments - why is this happening? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
I came across a very interesting fact while executing the following a simple c program:
#include<stdio.h>
int main( )
{
int k=0;
printf("%d%d%d", k++, k, ++k);
return 0;
}
in windows it was showing output as: 1 1 1
but in linux(ubuntu) it was showing as: 1 2 2
why is it so?
It's undefined behaviour. When there are no / ambiguous sequence points. See this wikipedia article:
http://en.wikipedia.org/wiki/Sequence_point
There are two problems. First is that the order in which the expressions k++, k, and ++k in the printf call are evaluated is not specified; the compiler is free to evaluate them in any order it sees fit. Second is that an object may not have its stored value updated more than once between sequence points by the evaluation of an expression. Both k++ and ++k attempt to update the value stored at k, and there is no sequence point between those expressions, so the behavior is undefined; any result is permitted.
The standard does not specify an ordering in which the arguments of a routine are evaluated.
Writing code that depends on the ordering is not portable.

how it's answer is 36? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
main()
{
int a=5;
a= a++ + ++a + ++a + a++ + a++;
printf("%d",a);
}
This is not defined.
You can find the Committee Draft from May 6, 2005 of the C-standard here (pdf)
See section 6.5 Expressions:
2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
and the example:
71) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
The answer is actually undefined.
Answer in undefined because you've got some situations in which the parser doesn't know how to parse the code..
is a+++b: a + ++b or a++ + b?
Think the fact that usually white space is just ignored when lexing the source code. It may depends upon implementation of the compiler (and some other languages with same ++ operators may choose to give priority to one instead of another) but in general this is not safe.
For example in Java your code line gives 37 as the answer, because it chooses to bind ++ operators in a specific way according to precedence, but it's just a choice..

Resources