Question discussion

What is the output of the following C program?#include int main() { int x = 5, y = 3; x ^= y; y ^= x; x ^= y; printf("%d %d ", x, y); int z…

1346Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int main() {
    int x = 5, y = 3;
    x ^= y; y ^= x; x ^= y;
    printf("%d %d ", x, y);
    int z = (x++, y++, x + y);
    printf("%d %d %d", z, x, y);
    return 0;
}
  • A3 5 10 4 6
  • B5 3 8 6 4
  • C3 5 8 4 6
  • D5 3 10 4 6
Correct answer: A

Explanation

The three XOR statements swap x and y (x = 3, y = 5); in the comma expression both increments are completed before x + y is evaluated, so z = 4 + 6 = 10, x = 4 and y = 6.

Discussion 0

Nobody has replied yet. Start the discussion!