鱼C论坛

 找回密码
 立即注册
查看: 859|回复: 6

[已解决]S1E6布尔类型的字节长度为什么打印出错

[复制链接]
发表于 2022-2-18 15:44:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求大佬指点 谢谢啦

代码如下:
printf("_Bool = %d\n",sizeof(_Bool));

错误信息如下:
[Error] '_Bool' was not declared in this scope
最佳答案
2022-2-18 15:56:17

这是C语言,这么写的前提是有一行这样的代码
  1. #include <stdbool.h>
复制代码

  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void) {
  4.     printf("_Bool = %d\n",sizeof(bool));
  5.     return 0;
  6. }
  7. $ gcc --version
  8. gcc (GCC) 11.1.0
  9. Copyright (C) 2021 Free Software Foundation, Inc.
  10. This is free software; see the source for copying conditions.  There is NO
  11. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  12. $ gcc -g -Wall -o main main.c
  13. main.c: In function ‘main’:
  14. main.c:4:34: error: ‘bool’ undeclared (first use in this function)
  15.     4 |     printf("_Bool = %d\n",sizeof(bool));
  16.       |                                  ^~~~
  17. main.c:2:1: note: ‘bool’ is defined in header ‘<stdbool.h>’; did you forget to ‘#include <stdbool.h>’?
  18.     1 | #include <stdio.h>
  19.   +++ |+#include <stdbool.h>
  20.     2 |
  21. main.c:4:34: note: each undeclared identifier is reported only once for each function it appears in
  22.     4 |     printf("_Bool = %d\n",sizeof(bool));
  23.       |                                  ^~~~
  24. $
复制代码

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. int main(void) {
  5.     printf("_Bool = %d\n",sizeof(bool));
  6.     return 0;
  7. }
  8. $ gcc -g -Wall -o main main.c
  9. main.c: In function ‘main’:
  10. main.c:5:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat ]
  11.     5 |     printf("_Bool = %d\n",sizeof(bool));
  12.       |                     ~^    ~~~~~~~~~~~~
  13.       |                      |    |
  14.       |                      int  long unsigned int
  15.       |                     %ld
  16. $ ./main
  17. _Bool = 1
  18. $
复制代码


应该这样才正确
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. int main(void) {
  5.     //printf("_Bool = %d\n",sizeof(bool));
  6.     printf("_Bool = %lu\n",sizeof(bool));
  7.     return 0;
  8. }
  9. $ gcc -g -Wall -o main main.c
  10. $ ./main
  11. _Bool = 1
  12. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-18 15:45:45 | 显示全部楼层
  1. printf("_Bool = %d\n",sizeof(bool));
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-2-18 15:49:03 | 显示全部楼层
你的编译环境不支持 _Bool
请升级你的编译环境(因为我猜是因为编译环境太老的原因)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-18 15:56:17 | 显示全部楼层    本楼为最佳答案   

这是C语言,这么写的前提是有一行这样的代码
  1. #include <stdbool.h>
复制代码

  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void) {
  4.     printf("_Bool = %d\n",sizeof(bool));
  5.     return 0;
  6. }
  7. $ gcc --version
  8. gcc (GCC) 11.1.0
  9. Copyright (C) 2021 Free Software Foundation, Inc.
  10. This is free software; see the source for copying conditions.  There is NO
  11. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  12. $ gcc -g -Wall -o main main.c
  13. main.c: In function ‘main’:
  14. main.c:4:34: error: ‘bool’ undeclared (first use in this function)
  15.     4 |     printf("_Bool = %d\n",sizeof(bool));
  16.       |                                  ^~~~
  17. main.c:2:1: note: ‘bool’ is defined in header ‘<stdbool.h>’; did you forget to ‘#include <stdbool.h>’?
  18.     1 | #include <stdio.h>
  19.   +++ |+#include <stdbool.h>
  20.     2 |
  21. main.c:4:34: note: each undeclared identifier is reported only once for each function it appears in
  22.     4 |     printf("_Bool = %d\n",sizeof(bool));
  23.       |                                  ^~~~
  24. $
复制代码

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. int main(void) {
  5.     printf("_Bool = %d\n",sizeof(bool));
  6.     return 0;
  7. }
  8. $ gcc -g -Wall -o main main.c
  9. main.c: In function ‘main’:
  10. main.c:5:22: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat ]
  11.     5 |     printf("_Bool = %d\n",sizeof(bool));
  12.       |                     ~^    ~~~~~~~~~~~~
  13.       |                      |    |
  14.       |                      int  long unsigned int
  15.       |                     %ld
  16. $ ./main
  17. _Bool = 1
  18. $
复制代码


应该这样才正确
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. int main(void) {
  5.     //printf("_Bool = %d\n",sizeof(bool));
  6.     printf("_Bool = %lu\n",sizeof(bool));
  7.     return 0;
  8. }
  9. $ gcc -g -Wall -o main main.c
  10. $ ./main
  11. _Bool = 1
  12. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-2-18 16:04:55 | 显示全部楼层
人造人 发表于 2022-2-18 15:56
这是C语言,这么写的前提是有一行这样的代码

加了你这一行果然可以了 谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-18 16:10:23 | 显示全部楼层

谢谢兄弟 问题解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-18 19:26:08 | 显示全部楼层
人造人 发表于 2022-2-18 15:56
这是C语言,这么写的前提是有一行这样的代码

好的,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表