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;
精彩评论