Dec 2, 2016 at 11:52am
I believe this code is supposed to output 5 7 9 4, im just not sure how to get it to do that.
What will be printed as the result of the operation shown below.
Show the results by writing this code fragment in a complete C program.
1 2 3 4 5 6 7 8 9 10
|
#include <stdio.h>
int main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf("%d%d",x,y);
return 0;
}
|
Last edited on Dec 2, 2016 at 11:53am
Dec 2, 2016 at 12:01pm
I believe this code is supposed to output 5 7 9 4, im just not sure how to get it to do that. |
Sorry, I'm not clear on what you're asking. Do you need to change the code such that it gives you that output?
Last edited on Dec 2, 2016 at 12:05pm
Dec 2, 2016 at 9:51pm
MikeyBoy I think he meant 57 and 94.
anyway the code could be written like this
x=y++ + x++; can be written as:
x = y + x;
x = x + 1
y = y + 1;
y= ++y + ++x; can be written as
y = y + 1
x = x + 1
y = y + x
check pre increment and post increment operator for better understanding of the problem
Dec 3, 2016 at 1:11am
> What will be printed as the result of the operation shown below.
Undefined behaviour.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <stdio.h>
int main()
{
int x=20,y=35;
x=y++ + x++; // *** warning: multiple unsequenced modifications to 'x'
// *** warning: operation on 'x' may be undefined
y= ++y + ++x; // *** warning: multiple unsequenced modifications to 'y'
// *** warning: operation on 'y' may be undefined
printf("%d%d",x,y);
return 0;
}
|
http://coliru.stacked-crooked.com/a/fcd6cd70dcebccf3
Last edited on Dec 3, 2016 at 1:12am