Currently I am working on c and facing a confusion regarding signed int in structure and here I have given the example:
#include <stdio.h>
#include <string.h>
struct {
signed int age : 4;
} Age;
int main( ) {
Age.age = -8;
printf("Age.age : %d\n", Age.age );
return 0;
}
Here, I already have described the size of bits which int will occupy while storing the value. here I am assigning value -8 to age. so it will store value like 1000 for 8 and for -8 it should store like 11000 where left 1st bit is known as sign bit. So if int age have to store -8 it must have 5 bits but while I compile the given example it is not giving error and displays an output.
Please help me with my issues.
Probably, integers are stored using 2's complement representation on your system.
In 2's complement, a 4-bit bitfield can hold a range of -8
through to +7
. The bit representation 1000
will mean -8
, and it is not possible to store +8
.
Assigning an out-of-range value (such as +8
in this case) causes implementation-defined behaviour.