This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Postfix and prefix operators as function arguments - why is this happening? [duplicate]
(3 answers)
Closed 9 years ago.
When I compile following code it gives different output in different environment.
int a=4;
a = ++a + ++a;
printf("%d",a);
compiling this in Dev-C++ gives 12 while in xcode LLVM compiler it gives 11 as output.
Also when I compile following code
int a=4;
a = ++a + ++a + ++a;
printf("%d",a);
it gives 19 in Dev-C++ and 18 in xcode LLVM compiler.
Can anyone explain me more about this?
The following code:
a = ++a + ++a;
and
a = ++a + ++a + ++a;
are both examples of Undefined Behaviour, so the result is dependent on compiler, platform etc.
Please look at "The C Programming Language" by K&R, Section 2.12
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
Explain the output
. Program has been compiled on gnu (gcc 4.9.2 compiler)
#include <stdio.h>
main()
{
int i=1;
int arr[]={2,3,4,5};
int x=(i++<3);
printf("%d %d %d %d %d",i,x,++i[arr],i++[arr],i);
}
output :3 1 6 4 3
What you have here is a "side effect". Depending on the underlying architecture, the parameters to printf() are evaluated from left-to-right or from right-to-left (f.e. HPUX).
So you cannot say, what is "THE" result. You can only specify the result on your operating system with your compiling chain.
This behaviour is called Undefined behavior and sequence points
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.
This question already has answers here:
How #define work? Strange result for CUBE(y) y*(y*y)
(4 answers)
Closed 8 years ago.
I was going through my C learning by writing small pieces of code and one new question came up
I have written a small piece of code
#include<stdio.h>
#include<conio.h>
#define SUM(a) (a+a)
main()
{
int w,b=5;
w=SUM(b++);
printf("%d\n",w);
printf("%d\n",b);
getch();
}
What i was thinking that it will display the output as
10
6
but it is showing
10
7
Can someone explain why ,i am using Visual Studio 2008
because when you do
w=SUM(b++);
the macro will be replaced by:
w= b++ + b++;
now, if b=5 then you do twice b++ and get b=7
Edit
after reading MSalters comment i did some searching and found out that as he said this code couse UB.
as says here:
If a side effect on a scalar object is unsequenced relative to either
a different side effect on the same scalar object or a value
computation using the value of the same scalar object, the behavior is
undefined. If there are multiple allowable orderings of the
subexpressions of an expression, the behavior is undefined if such an
unsequenced side effect occurs in any of the orderings.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
What will the value of x in following line of code:-
x = a++ + ++a + a++
actually compiler is giving x = 3..but how????
This is undefined behavior. Any answer is valid, up to and including crashing. It is unlikely that any particular answer will be consistent between platforms. Don't do this.
Undefined behavior, as the value of a is modified multiple time in the same statement.