I just saw this here
#include <stdio.h>
int main(int argc, char *argv[printf("Hello, world!\n")]) {}
What this does is print “Hello World!”
But what’s actually going on here?
The best I can guess is that it gets compiled and thrown at the top of the execution stack, but the syntax doesn’t even look legal to me …
The code makes use of C99’s variable-length array feature, which lets you declare arrays whose size is known only at run-time.
printfreturns an integer equal to the number of characters that were actually printed, so the code prints “Hello, world!” first and uses the return value as the size ofargv. Themainfunction itself does nothing.The actual call toprintfitself probably goes into the startup code generated by the compiler, which in turn callsmain.Edit: I just checked the disassembly of the code generated by
gccand it appears that the call toprintfgoes insidemainitself, before any other code.