Quantcast
Viewing all articles
Browse latest Browse all 22016

Unexpected behavior with inline assignment in GCC [duplicate]

This question already has an answer here:

I was messing around with some C code for my compilers class, and ran into some Inconsistent/unexpected behavior, as shown below.

This code:

#include <stdio.h>

int main()
{
    int x, y;

    x = 2;
    y = x + (++x) + x;
    printf("%d\n", y);
    x = 2;
    y = x +  x + (++x) + x;
    printf("%d\n", y);
    return 0;
}

returns:

9
10

In the first assignment of y, the (++x) appears to be executed prior to the evaluation of the first instance of x, while in the second example, (++x) is executed in what appears to be the correct order (if I were calculating it by hand).

What is the cause of the the difference of the order of execution?


Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>