Skip to content

Instantly share code, notes, and snippets.

@englishm
Last active July 20, 2023 22:42
Show Gist options
  • Save englishm/f12ef22ace5e719e8ac9f4d62a485356 to your computer and use it in GitHub Desktop.
Save englishm/f12ef22ace5e719e8ac9f4d62a485356 to your computer and use it in GitHub Desktop.

OK, I'm puzzled, but maybe this warnings is a clue...?

Why do I get 9 here, but a friend gets 10?

macro-puzzle.c :

#include <stdio.h>


#define my_macro(a) (a) + (a)

int main() {
  int x = 3;
  x = my_macro(++x);
  printf("x: %d\n", x);
  return 0;
}
$ gcc macro-puzzle.c -o macro-puzzle && ./macro-puzzle 
macro-puzzle.c:8:16: warning: multiple unsequenced modifications to 'x' [-Wunsequenced]
  x = my_macro(++x);
               ^~
macro-puzzle.c:4:22: note: expanded from macro 'my_macro'
#define my_macro(a) (a) + (a)
                     ^     ~
1 warning generated.
x: 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment