开发者

Why does this code snippet compile?

开发者 https://www.devze.com 2022-12-16 07:15 出处:网络
Why does the following code compile开发者_运维百科? #include <stdio.h> int main(void) { getchar;

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

精彩评论

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