Output changes with respect to the compiler [duplicate] - c

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.

Related

How do ++ and -- operators work in this example? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
In the following piece of C code, I would expect the printed output to be 5794. But when I compile it with GCC (7.5.0) the output is 5693. Why?
int main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf("%d%d",x,y);
}
this program has undefined behavior.which means you can't predict what will happen.
look for more information here
Sequence_point

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.

Confused of the result of printf("(%d,a=%d,a+b=%d)",a=a+3,a,a+b); [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
Given the following code:
int a=0,b=2;
printf("(%d,a=%d,a+b=%d)",a=a+3,a,a+b);
why the result is (3,a=3,a+b=5)
You are printing the result of an assignment (a=a+3) which is the assigned value (3).
But the evaluation order of the parameters depends on the compiler (undefined behaviour). it could print:
(3,a=3,a+b=5)
or:
(3,a=0,a+b=2)
or even:
(3,a=0,a+b=5)

Not able to interpret printf output [duplicate]

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.

Behaviour of macros and increment operator in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I compile the following code
#include<stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
It gives 27 , 6
But shouldn't the expression a=b++*b++*b++; be calculated as a=3*4*5 and should give 60?
Your expression causes undefined behaviour, so you could get any answer. Trying to modify the same value more than twice between sequence points is bad news.

Resources