Sail E0 Webinar

MCQs

Total Questions : 72 | Page 8 of 8 pages
Question 71.

What are the largest values that can be assigned to each of the bit fields defined in this 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);
}


  1.    a=0 b=2 c=3 d=4
  2.    a=1 b=2 c=7 d=15
  3.    a=1 b=3 c=7 d=15
  4.    None of these
 Discuss Question
Answer: Option C. -> a=1 b=3 c=7 d=15

a=1  (1 bit: 0 or 1)
   b=3  (2 bits: 00 or 01 or 10 or 11),
   c=7  (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
   d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000 or
      1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)



Question 72.

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


  1.    Compile Error
  2.    1 2 7 12 size = 2 bytes
  3.    1 2 7 12 size = 4 bytes
  4.    None of these
 Discuss Question
Answer: Option B. -> 1 2 7 12 size = 2 bytes

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).



Latest Videos

Latest Test Papers