Why doesn’t putchar require a header?

后端 未结 2 1018
一生所求
一生所求 2021-01-20 19:53

Reading this answer which explains the polyglot program on page not found on Stack Overflow I was surprised to read putchar was used because you don\'t need a

2条回答
  •  时光说笑
    2021-01-20 20:16

    Some compilers do weird, non-standard things such as automatically including various common headers. It is possible that the code was compiled on one such compiler.

    Otherwise, in the old obsolete C90 standard, you didn't need to have a function prototype visible: if you had not, the compiler would start to assume that the return type was int. Which doesn't make any sense. This nonsense was removed from the C language with the C99 standard.

    So the reason the code compiled, was because you used a crappy compiler. There are no guarantees that the code will compile/link or work as predicted.

    For example:

    int main ()
    {
      putchar('a');
    }
    

    This compiles with gcc as well as gcc -std=c90. But if you compile it as standard C,

    gcc -std=c99 -pedantic-errors

    you'll get error: implicit declaration of function 'putchar'.

提交回复
热议问题