C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - True / False Questions (Q.No. 3)
3.
Bit fields CANNOT be used in union.
True
False
Answer: Option
Explanation:

The following is the example program to explain "using bit fields inside an union".

#include<stdio.h>

union Point
{
  unsigned int x:4;
  unsigned int y:4;
  int res;
};

int main()
{
   union  Point pt;

   pt.x = 2;
   pt.y = 3;
   pt.res = pt.y;

   printf("\n The value of res = %d" , pt.res);

   return 0;
}
// Output: The value of res = 3

Discussion:
20 comments Page 2 of 2.

Sirisha said:   1 decade ago
What is bit field?

Ranjana said:   1 decade ago
Why we made union. What is its purpose?

Preeti said:   1 decade ago
Yes. @Soumya Roy you are right.

MadanReddy said:   1 decade ago
Hai,sathuragiri.. i think,this explanation will be helpful for u..

union Point
{
unsigned int x:4;
unsigned int y:4;
int res;
};


In general unsigned integer limit is represented with 2bytes(16bits).
consider a simple example,

104 binary equivalent is(1101000)

Here only 7bits are used to store 104. remaining 9bits will be unused bits.

These unused bits will occupy extra space on memory.this can be avoided by using "bit field" concept.

Soumya Roy said:   1 decade ago
Yes of course, bit fields are allowed in Unions. But read the question carefully, its given bit field are not allowed in unioin.

So that sentence is FALSE. Therefore the answer given here is correct and as per your text books. So just chill.

Sanjul said:   1 decade ago
What is bit field?

Sathuragiri said:   1 decade ago
What you mean by bit field? explain well?

Khushnood said:   1 decade ago
Yes yes, I also read in book that bit are allowed in unoin.

Prabhu said:   1 decade ago
Yes, I also read that bitfields are allowed in union.

Sumanth said:   1 decade ago
I have read in a text book that bit fields are allowed in unions.


Post your comments here:

Your comments will be displayed after verification.