What is it that makes value in a variable of type _Bool
have the value 1
, even when we assign a value greater than 1
to it?
For example:
#include <stdio.h>
int main(void) {
_Bool tmp = 10;
printf("%x, %lu\n", tmp, sizeof(tmp));
return 0;
}
This would print 1, 1
. I am trying to understand what makes a variable of size byte act as a single bit and when assigned a value greater than 1 which has its LSB 0 still get converted to 1
.
What is it that makes value in a variable of type _Bool 1 , even when we assign a value greater than 1 to it. The compiler does.
For example on ARM (arm-none-eabi-gcc):
#include "stdio.h"
#include "stdbool.h"
int main()
{
_Bool tmp = 10;
printf("%x , %lu", tmp, sizeof(tmp));
return 0;
}
compiles to:
.LC0:
.ascii "%x , %lu\000"
main:
stmfd sp!, {fp, lr}
add fp, sp, #4
sub sp, sp, #8
mov r3, #1
strb r3, [fp, #-5]
ldrb r3, [fp, #-5] @ zero_extendqisi2
mov r2, #1
mov r1, r3
ldr r0, .L3
bl printf
mov r3, #0
mov r0, r3
sub sp, fp, #4
ldmfd sp!, {fp, lr}
bx lr
.L3:
.word .LC0
you can see in the instruction mov r3, #1
that the compiler directly converts the initialisation value 10
to 1
as specified by the standard .