C
Programming and Technical
Programming
Technical
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%dn", sizeof(bit));
return 0;
}
A. 1 B. 2
C. 4 D. 9
Read Solution (Total 5)
-
- ans: C // integer storage size is 4 bytes
- 9 years agoHelpfull: Yes(5) No(0)
- D.9b bcoz it is allocating bitwize so 1+4+4=9
- 9 years agoHelpfull: Yes(2) No(2)
- answer =4// integer size
- 8 years agoHelpfull: Yes(1) No(1)
- Can any1 explain this properly??
- 8 years agoHelpfull: Yes(0) No(1)
- bit1,bit3,bit4 are called bitfields. We may not want to use entire 4 bytes of an integer to store a small number. These variables allow u to define thw width to make use of only the necessary bytes and share the remaining space.
bit1 is allocated 1 bits
bit3 4 bits
bit4 4 bits
So total 9 bits
The first int variable is allocated only 1 bit of 32-bits. the remaining space is allocated for other two variables. Hence all the variables share the same space of the int, whose size is 4 bytes - 8 years agoHelpfull: Yes(0) No(0)
C Other Question
#include
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit={1, 2, 13};
printf("%d, %d, %dn", bit.bit1, bit.bit3, bit.bit4);
return 0;
}
A. 1, 2, 13 B. 1, 4, 4
C. -1, 2, -3 D. -1, -2, -13
#include
int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%dn", var.one);
return 0;
}
A. 1 B. -1
C. 0 D. Error