开发者

Why does this code snippet compile?

Why does the following code compile开发者_运维百科?

#include <stdio.h>

int main(void) {
    getchar;
}


Because function names are aliases to function pointers to those functions, which are themselves values much like integers.. This is semantically very similar to

#include <stdio.h>

int main(void) {
    42;
}

It is valid but pointless.


The same reason 1; would compile, getchar is just an address to a function. The result is evaluated, then discarded. In the language specification, it's called an "expression statement";


C is weird, this code compiles too, but it segfaults which for the record, is the smallest segfault in C history.

main;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜