5 ms·
({_:&&_;}); ({}); ({;}); All of these rely on a gcc extension ('statement expressions'). ________________________________ a = '-'-'-' You can also d
by moonchild 7y ago
({_:&&_;});
({});
({;});
All of these rely on a gcc extension ('statement expressions').
________________________________
a = '-'-'-'
You can also do:
a = '/'/'/'
To generate a 1 instead of a 0.
________________________________
printf("%d %d\n", 0 == sizeof(count = 2, count++), count);
This works because:
1. There are two forms of sizeof; sizeof(T) where T is a type, and sizeof x where x is an expression.
2. There are 'comma expressions'; if you have (x, y) where x and y are expressions, then x is executed and the expression evaluates to y.
3. Parameters to sizeof are not evaluated (which is important because otherwise the value of the 3rd argument would be undefined, since there's no sequence point between evaluation of function parameters).
- saagarjha 7y ago> Parameters to sizeof are not evaluated Oh, but they are ;) Try running sizeof on a VLA sometime.
- _kst_ 7y agoThe argument to sizeof is evaluated if and only if it's of a variable length array type. (If it's a parenthesized type name, it's unclear what it means to "evaluate" it.)
- userbinator 7y agoNumber 24 on the list shows exactly this.
- sigjuice 7y ago$ cat sizeof.c #include <stdio.h> int main(int argc, char *argv[]) { printf("%zu\n", sizeof(int[argc - 2])); } $ make -B sizeof cc sizeof.c -o sizeof $ ./sizeof foo 0 $ ./sizeof 17179869180
- saagarjha 7y agoThis can yield surprising results even without undefined behavior. For example: https://godbolt.org/z/fwhLH4 https://godbolt.org/z/fwhLH4
- sigjuice 7y agoThis is what I see visit your link. I think this is easily explained. What might I be missing? ASM generation compiler returned: 0 Execution build compiler returned: 0 Program returned: 44 ./output.s PS: I did not examine the assembly code
- saagarjha 7y agoIt calls printf. Perhaps this is a better example: https://godbolt.org/z/fwhLH4 https://godbolt.org/z/fwhLH4. (I updated the link above, too.)
- entelechy0 7y agoThat is because sizeof() is not a function: it is a macro! :)