Question
#include<stdio.h>
void main()
{
static struct my_struct
{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf("%d %d %d %d",v.a,v.b,v.c,v.d);
printf("\nSize=%d bytes",sizeof v);
}
What will be output if you compile following c code ?
#include<stdio.h>
void main()
{
static struct my_struct
{
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf("%d %d %d %d",v.a,v.b,v.c,v.d);
printf("\nSize=%d bytes",sizeof v);
}
Answer: Option B
Was this answer helpful ?
The four fields within 'v' require a total of 10 bits and these bits can be accomodated within the first word(16 bits). Unnamed fields can be used to control the alignment of bit fields within a word of memory. Such fields provide padding within the word.
[NOTE : Some compilers order bit-fields from righ-to-left (i.e. from lower-order bits to high-
order bits) within a word, whereas other compilers order the fields from left-to-right (high-
order to low-order bits).
Was this answer helpful ?
Submit Solution