尝试用%s输出空指针

尝试用%s输出空指针:

#include 
int main (void)
{
    printf("%s\n", NULL);
    return 0;
}

clang:

cuihao@cuihao-arch /tmp $ clang test.c
cuihao@cuihao-arch /tmp $ ./a.out
(null)

pathcc:

cuihao@cuihao-arch /tmp $ pathcc test.c 
cuihao@cuihao-arch /tmp $ ./a.out
(null)

gcc:

cuihao@cuihao-arch /tmp $ gcc test.c 
test.c: 在函数‘main’中:
test.c:5:5: 警告:对空指针指向内容的读操作(实参 2) [-Wformat]
test.c:5:5: 警告:格式 ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘void *’ [-Wformat]
cuihao@cuihao-arch /tmp $ ./a.out
[1]    5428 segmentation fault  ./a.out

g++:

cuihao@cuihao-arch /tmp $ g++ test.c 
test.c: 在函数‘int main()’中:
test.c:5:24: 警告:格式 ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘long int’ [-Wformat]
cuihao@cuihao-arch /tmp $ ./a.out
(null)

你可能感兴趣的:(尝试用%s输出空指针)