这是C语言,这么写的前提是有一行这样的代码$ cat main.c
#include <stdio.h>
int main(void) {
printf("_Bool = %d\n",sizeof(bool));
return 0;
}
$ gcc --version
gcc (GCC) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:4:34: error: ‘bool’ undeclared (first use in this function)
4 | printf("_Bool = %d\n",sizeof(bool));
| ^~~~
main.c:2:1: note: ‘bool’ is defined in header ‘<stdbool.h>’; did you forget to ‘#include <stdbool.h>’?
1 | #include <stdio.h>
+++ |+#include <stdbool.h>
2 |
main.c:4:34: note: each undeclared identifier is reported only once for each function it appears in
4 | printf("_Bool = %d\n",sizeof(bool));
| ^~~~
$
$ cat main.c
#include <stdio.h>
#include <stdbool.h>
int main(void) {
printf("_Bool = %d\n",sizeof(bool));
return 0;
}
$ gcc -g -Wall -o main main.c
main.c: In function ‘main’:
main.c:5:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat ]
5 | printf("_Bool = %d\n",sizeof(bool));
| ~^ ~~~~~~~~~~~~
| | |
| int long unsigned int
| %ld
$ ./main
_Bool = 1
$
应该这样才正确$ cat main.c
#include <stdio.h>
#include <stdbool.h>
int main(void) {
//printf("_Bool = %d\n",sizeof(bool));
printf("_Bool = %lu\n",sizeof(bool));
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
_Bool = 1
$
|