鱼C论坛

 找回密码
 立即注册
查看: 696|回复: 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语言,这么写的前提是有一行这样的代码
#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));
      |                     ~^    ~~~~~~~~~~~~
      |                      |    |
      |                      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
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-18 15:45:45 | 显示全部楼层
printf("_Bool = %d\n",sizeof(bool));
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-2-18 15:49:03 | 显示全部楼层
你的编译环境不支持 _Bool
请升级你的编译环境(因为我猜是因为编译环境太老的原因)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

这是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));
      |                     ~^    ~~~~~~~~~~~~
      |                      |    |
      |                      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
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

加了你这一行果然可以了 谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢兄弟 问题解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

好的,谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-30 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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