神秘与布尔数据类型 [英] Mystery with bool data type

查看:84
本文介绍了神秘与布尔数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdbool.h>

int main()
{
bool a[5]={0,1,0,0,0};
a[1]=3;
printf("\n bool is %d",a[1]);
printf("\n sizeof bool is %d and size of a is %d",sizeof(bool),sizeof(a));

bool b[10];
printf("\n bool is %d",b[1]);

}

这个程序的输出

 bool is 1
 sizeof bool is 1 and size of a is 5
 bool is 4

问:

1> BOOL存储1位,那么为什么的sizeof(布尔)为1字节?

1> bool store 1 bit then why sizeof(bool) is 1 byte ?

2>如果布尔有一个字节,然后当我分配[1] = 3,那么为什么它打印1?

2> if bool has 1 byte then when i assign a[1] = 3 then why it print 1 ?

3>如果布尔只考虑1安培; O值是店里那为什么B〔1〕输出值3?

3> if bool only consider 1 & o value to be store then why b[1] prints value 3 ?

推荐答案

1>布尔店不是1位。没有什么是1位。一切是至少1个字节。 的sizeof(布尔)是特定于平台。只有的sizeof(char)的是保证1。

1> Bool store is not 1 bit. Nothing is 1 bit. Everything is at least 1 byte. sizeof(bool) is platform specific. Only sizeof(char) is guaranteed to be 1.

2>它隐式转换 INT 布尔(布尔)3 == 1

2> It implicitly converts int to bool : (bool)3 == 1

3>阵列 B 没有初始化,它可以有任何值。你只是在访问一些内存。

3> the array b is not initialized, it can have any value. You're just accessing some memory.

这篇关于神秘与布尔数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆