C Code: How does these even work?

后端 未结 4 1930
既然无缘
既然无缘 2021-02-04 01:02

I just saw this here

#include 

int main(int argc, char *argv[printf(\"Hello, world!\\n\")]) {}

What this does is print \"Hello

4条回答
  •  广开言路
    2021-02-04 01:42

    The code makes use of C99's variable-length array feature, which lets you declare arrays whose size is known only at run-time. printf returns 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 of argv. The main function itself does nothing. The actual call to printf itself probably goes into the startup code generated by the compiler, which in turn calls main.

    Edit: I just checked the disassembly of the code generated by gcc and it appears that the call to printf goes inside main itself, before any other code.

提交回复
热议问题