-2
#include <stdio.h>
void main()
{
    int x= 1;
    printf("%d %d %d ", x, (x = x + 2), (x << 2));
    x << 2;
    printf("%d %d %d \n", x++, ++x, ++x);
}

Tried this in both XCode and gcc:
XCode output: 1 3 12 3 5 6 gcc output : 3 3 4 5 6 6 (Correct output)
Why is the XCode output coming wrong?

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30

1 Answers1

2

The order in which function parameters are evaluated is unspecified behavior in the C language. Both outputs are correct because it is how the particular compiler decided to implement it. Your program is "wrong" since it does not correctly deal with this unspecified behavior by using intermediate values to guarantee an order.

This program yields the same result (3 3 4 4 5 5) with both compilers

#include <stdio.h>

int main() {

    int x = 1;
    int y = x;

    x = x + 2;
    printf("%d %d %d ", x, x, (y << 2));

    int value1 = x++;
    int value2 = ++value1;
    int value3 = ++value2;
    printf("%d %d %d \n", value1, value2, value3);

    return 0;
}

Clang actually provides a helpful warning by default to avoid this:

warning: unsequenced modification and access to 'x' [-Wunsequenced]
printf("%d %d %d ", x, (x = x + 2), (x << 2));
                    ~     ^
allenh
  • 6,582
  • 2
  • 24
  • 40