Not able to interpret printf output [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.
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.

Related

what will be output of the following code and why ? can anyone explain? [duplicate]

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.

how is this post increment working? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.

Getting strange result while using increment operator(++) in printf fuction [duplicate]

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.

Printing order in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
int x=10;
printf("%d %d %d\n",x,++x,x++);
printf("%d %d %d",x,x+20,x+30);
It is printing output as
12 12 10
12 32 42
Why the order in first printf is in reverse order and why not in second printf statement?
i found in a book that C uses reverse printing order.
Your code has undefined behavior ("UB"). Thus, anything can happen.
Specifically, the rule violated is that one must not read and write the same variable without sequence-point except to determine the value to write.

Output changes with respect to the compiler [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 9 years ago.
int main()
{
int i=2;
printf("%d, %d\n", ++i, ++i);
return 0;
}
can some one explain me.
when it compile on turbo c its output is 4,3
and use GCC then it is 4,4;
why this output changes with respect to the compiler
This is undefined behavior, the compiler can do whatever they want.

Resources