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

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

Related

Why, the output is 6 and not 7? [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 3 years ago.
The following C code executes correctly but not as expected. Post increment operator here in z=z++ is creating confusion here. I may not be able to figure out silly mistake/concept, Can I have a brief explanation or some helpful link please.
#include<stdio.h>
int main()
{
int x=5,y=6,z=7;
if(x-y)
z=z++;
z=--z;
printf("%d",z);
}
You are not allowed to do z=z++; because between 2 sequence points you are not allowed to assign a variable 2 times.
This one is a full expression in which you assign z 2 times. So it can be interpreted ambigously and the result of the C abstract machine is undefined behavior.
The same for z=--z.

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.

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.

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