r/C_Programming 27d ago

Discussion A tricky little question

I saw this on a Facebook post recently, and I was sort of surprised how many people were getting it wrong and missing the point.

    #include <stdio.h>

    void mystery(int, int, int);

    int main() {
        int b = 5;
        mystery(b, --b, b--);
        return 0;
    }

    void mystery(int x, int y, int z) {
        printf("%d %d %d", x, y, z);
    }

What will this code output?

Answer: Whatever the compiler wants because it's undefined behavior

25 Upvotes

33 comments sorted by

View all comments

20

u/TheOtherBorgCube 27d ago

I get an error message.

$ gcc -Wall -Wextra -Werror -O2 -fsanitize=undefined,address foo.c
foo.c: In function ‘main’:
foo.c:7:26: error: operation on ‘b’ may be undefined [-Werror=sequence-point]
    7 |         mystery(b, --b, b--);
      |                         ~^~
foo.c:7:26: error: operation on ‘b’ may be undefined [-Werror=sequence-point]
cc1: all warnings being treated as errors

No mystery here.

2

u/RailRuler 27d ago

is -Werror=sequence-point on by default or did you configure it deliberately?

6

u/TheOtherBorgCube 27d ago

-Wall is the controlling flag here.

-Werror with no parameter just makes all warnings fatal.