I found the following construct, where a variable is assigned to what seems to be a compound statement, foo
, in a driver. For comparison, bar
yields undefined behaviour be treating the same code as a proper function. It doesn't seem to conform with my understanding of the C language and its preprocessor, so I suspect that it is a GCC extension. What is the logic executed here? (See output here.)
#include <stdio.h>
#define foo(c) ({ int x = c; x; })
int bar(char c) {
int x = c;
x;
}
int main(void) {
int x = foo('0');
int y = bar('A');
printf("%d\n%d\n", x, y);
return 0;
}
Output:
48
0