This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
the output of the programme
#include<stdio.h>
main()
{
int i = 10;
printf("%d %d %d\n", ++i, i++, ++i);
}
is 13 11 13. Can someone please explain this ?
It's the oldest question ever. Why do people find this so fascinating?
This is undefined behavior; you're relying on side-effects without a sequence point between modifications.
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
#include <stdio.h>
int main()
{
int i = 6;
printf("%d %d", ++i, i++);//printing
return 0;
}
What will be output of the following code and why?
printf("%d %d",++i ,i++);//printing
Is undefined behavior. The order of argument processing is not specifically defined in the C standard, it is not possible to predict exactly what the output will be. It could be anything at all according to this.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I am executing this c program on gcc compiler and getting strange results.
So how is it possible
code:
#include<stdio.h>
int main()
{
int i;
i =10;
printf(" %d %d %d ",i++,i++,i); //output : 11 10 12
return 0;
}
as per me result should be 10 11 12 but I am getting 11 10 12.
How is it possible?
In C++, the order of evaluation of function arguments is undefined so if you use the increment operator multiple times in the argument to a particular function there is no 'correct' answer, they may be evaluated in any arbitrary order.
Please familiarize yourself with the concept of Sequence points. Only at such defined sequence points is guaranteed that all side effects of previous evaluations are performed. There are no sequence points between the list of arguments of a function. So, it leads to undefined behavior.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I wrote the following simple program
#include<stdio.h>
int main()
{
int i;
i=1;
printf("%d %d %d",i,i++,++i);
return 0;
}
The above program gave 3 2 3 as output which I am not able to interpret the output. I am using gcc-4.8.1
You have undefined behavior here!!
When there are multiple increments to the same variable in the printf() you can't predict the output.
The order of execution within the printf() is not defined.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
i tried this simple C program in GCC
#include<stdio.h>
int main(){
int x=5;
printf("%d,%d,%d,%d,%d",++x,x++,x,x++,++x);
return 0;
}
and the output was 9,7,7,6,6
i traced it and assumed that it will print 6,6,7,7,9 but i found my assumption in reverse order, how come!
Because your program has undefined behaviour. There is no sequence point between the evaluations of function arguments, and it is undefined behaviour to mutate the same object more than once without intervening sequence point.
The program is simply ill-formed. It is not a valid C program.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
#include<stdio.h>
int main(){
int i=5,j=5,y,x;
int m=++i;
int n=++i;
x=m+n;
y=++j + ++j ;
printf("%d %d ",x,y);
return 0;
}
OUTPUT : 13 14
Can any one plz explain why 'y' value is 14 and not 13.
Most compilers will increment j twice before performing the addition and attributing the value to y, that is why the result in your case is 14.
The C standard doesn't specify the order of evaluation of that expression, though, so on another compiler the result could be 13 indeed.
In other words, this is undefined behavior and should be not be used other than in obfuscation contests and puzzles.