#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]);
}
output of this program is
bool is 1
sizeof bool is 1 and size of a is 5
bool is 4
Question :
1> bool store 1 bit then why sizeof(bool) is 1 byte ?
2> if bool has 1 byte then when i assign a[1] = 3 then why it print 1 ?
3> if bool only consider 1 & o value to be store then why b[1] prints value 3 ?
1> Bool store is not 1 bit. Nothing is 1 bit. Everything is at least 1 byte.
sizeof(bool)is platform specific. Onlysizeof(char)is guaranteed to be 1.2> It implicitly converts
inttobool:(bool)3 == 13> the array
bis not initialized, it can have any value. You’re just accessing some memory.