S1E6布尔类型的字节长度为什么打印出错
求大佬指点 谢谢啦代码如下:
printf("_Bool = %d\n",sizeof(_Bool));
错误信息如下:
'_Bool' was not declared in this scope printf("_Bool = %d\n",sizeof(bool)); 你的编译环境不支持 _Bool
请升级你的编译环境(因为我猜是因为编译环境太老的原因)
isdkz 发表于 2022-2-18 15:45
这是C语言,这么写的前提是有一行这样的代码
#include <stdbool.h>
$ 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));
| ~^ ~~~~~~~~~~~~
| | |
| intlong 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
$ 人造人 发表于 2022-2-18 15:56
这是C语言,这么写的前提是有一行这样的代码
加了你这一行果然可以了 谢谢大佬 isdkz 发表于 2022-2-18 15:45
谢谢兄弟 问题解决了 人造人 发表于 2022-2-18 15:56
这是C语言,这么写的前提是有一行这样的代码
好的,谢谢大佬{:5_109:}
页:
[1]