gcc giving different result for windows and linux [duplicate] - c

This question already has answers here:
Why is this Undefined Behavior?
(3 answers)
Closed 7 years ago.
#include<stdio.h>
int main()
{
int x=2;
x=x++;
printf("%d",x);
return 0;
}
Output according to my logic :
2
Output on windows :
3
Output on Linux:
2
Why is windows giving 3 as output.
From what I understand x++ increments 2 to 3 but return back 2. So x should have 2.Is windows evaluating this anything different.
Similarly:
#include<stdio.h>
int main()
{
int x=2,y=4;
x=x++ + ++y;
printf("%d %d",x,y);
return 0;
}
Output according to me :
7 5
Output in windows :
8 5
Output on Linux :
7 5
Again the same situation.
Please help.....

x = x++ is undefined behavior so the two compilers are generating two different pieces of code.
Just x++ should suffice for your first piece of code.
Here is a question with your exact problem for the second piece of code

Related

What should be the result of "c -= --c - c++;" in C Language? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
I am trying the following code:
#include <stdio.h>
int main()
{
int c =0;
c -= --c - c++;
printf("%d \n",c);
return 0;
}
When I compiled and run it using a online c complier (https://www.tutorialspoint.com/compile_c_online.php) the result is -1. But I expected it to be 0.
So, I try it on my local Dev C++ (Windows) and the result is 0.
Should the result be 0 ?
If so, why 2 gcc compilers (ok they are in different plataform) gives me 2 different results ?
I ve been looking for some kind of automatic flag otimization which could produce a different result but I had no success.
THIS IS UNDEFINED BEHAVIOR (3 modifications without sequence points inbetween to the same variable)

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.

output of a function which has passByName parameter passing paradigm [duplicate]

This question already has an answer here:
Output of a c code like with call by reference [closed]
(1 answer)
Closed 9 years ago.
I've tried to figure out what is the output of a code like this .By the way ,It is not a real question, kind of therical question, i mean it is not an original c code, it is kind of a PL having c-code syntax and passed by name parameter paradigm.
int x=12,y=10;
void tswap(int pa, int pb) {
int tmp;
tmp=pa;
pa=pb;
pb=tmp;
x=x+pa;
x=x-pb;
y++;
printf("%d %d %d %d\n",pa,pb,x,y);
}
int main() {
int a=4;
tswap(x,a);
printf("%d %d %d\n",x,y,a);
tswap(++x,++y);
printf("%d %d %d\n",x,y,a);
return 0;
}
I think the output of first part should be :
-4 12 -4 11
-4 11 12
But i could find a logical solution for the part tswap (++x, ++y)
Is there anyone who can know how can I handle with this part ?
Thanks in advance !
tswap(++x,++y)
is the same as:
++x;
++y;
tswap(x,y);
making your output:
4 12 4 11
4 11 4
12 5 12 13
12 13 4

Post and Pre Increment in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
There is a code following below and i ma facing a very serious problem in understanding the logic for the code.
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i = 1 ;
printf("\n%d %d %d %d\n",++i,i++,i++,++i) ;
return 0 ;
}
I am using gcc compiler under the linux distro named Mandriva. In the above mentioned i have used pre and post increment with a variable in the printf statement.
The output that i am supposed to get is 2 2 3 5, but i am getting a different output.
Please help me in this code.
I am feeling much difficult in this code.
It's undefined behavior. There's no sequence points between the increments of i.
Any result is a correct result (including your hard drive being formatted).

Function calls order, is it a GCC bug? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Undefined Behavior and Sequence Points
Order of function call
I have found a problem in the following code when I compile it with GCC 4.5.2 (x86 32 bits).
# include <stdio.h>
int function(int x){
printf("%d\n", x);
return 2*x + 1;
}
int main(){
int x = 3*function(1) + 4*function(2) + 5*function(3) + 6*function(4) + 7*function(5) + 8*function(6);
printf("%d\n", x);
return 0;
}
Expected output:
1
2
3
4
5
6
299
Actual GCC output:
1
2
4
3
6
5
299
I compiled the same code with clang and the output is the expected one.
No. The order the calls take place is not specified. They could take place in any order, but for each pair of function calls made here, one will see either all of the side-effects of the other, or none (i.e. they cannot run in parallel unless they have no side effects, and in this example they have a big side effect, printf).

Resources