Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22268

How does C compiler `-O0` make code go so crazy?

$
0
0

Please take a look at: https://godbolt.org/z/WGBP8D

Code:

    size_t n;
    scanf("%d", &n);
    fprintf(stderr, "(1 <= %d) == %d\n", n, 1 <= n);
    fprintf(stderr, "(%d <= 1000) == %d\n", n, n <= 1000);
    fprintf(stderr, "%d\n", 2 <= 1000);
    ASSERT(1 <= n && n <= 1000);

Input is set to be:

2
1
2

Output:

(1 <= 2) == 1
(2 <= 1000) == 0
1
Assertion failed: 1 <= n && n <= 1000

Commenting out further part of code changes behavior of the snippet above. How is that?

Funny thing is, any optimization enabled (-O1) works nicely, but no optimization (-O0 or not specified) makes it... Well, I don't even know how to understand it.


Viewing all articles
Browse latest Browse all 22268

Latest Images

Trending Articles



Latest Images

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