Sail E0 Webinar
Question

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);
}


Options:
A .  Compile Error
B .  1 2 7 12 size = 2 bytes
C .  1 2 7 12 size = 4 bytes
D .  None of these
Answer: Option B

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

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers