programming project help c language

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
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
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
> What will be printed as the result of the operation shown below.

Undefined behaviour.

If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined. http://en.cppreference.com/w/c/language/eval_order


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
Topic archived. No new replies allowed.