# C Programming - Structures, Unions, Enums - Discussion

### Discussion :: Structures, Unions, Enums - True / False Questions (Q.No.3)

3.

Bit fields CANNOT be used in union.

 [A]. True [B]. False

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
``````

 Sumanth said: (Feb 13, 2011) I have read in a text book that bit fields are allowed in unions.

 Prabhu said: (Aug 20, 2011) Yes, I also read that bitfields are allowed in union.

 Khushnood said: (Sep 13, 2011) Yes yes, I also read in book that bit are allowed in unoin.

 Sathuragiri said: (Oct 27, 2011) What you mean by bit field? explain well?

 Sanjul said: (Aug 18, 2012) What is bit field?

 Soumya Roy said: (Feb 2, 2013) 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.

 Madanreddy said: (Mar 27, 2013) 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.

 Preeti said: (Jul 23, 2013) Yes. @Soumya Roy you are right.

 Ranjana said: (Nov 25, 2013) Why we made union. What is its purpose?

 Sirisha said: (Jun 30, 2014) What is bit field?

 Researcher said: (Jul 24, 2014) struct { type [member_name] : width ; }; Below the description of variable elements of a bit field: Type An integer type that determines how the bit-field's value is interpreted. The type may be int, signed int, unsigned int. Member_name The name of the bit-field. Width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type. The variables defined with a predefined width are called bit fields.

 Kavin said: (Apr 13, 2015) Please explain bit field?

 Kaustubh said: (Jul 26, 2015) Assigning of values to a memory location is bit field?

 Rijo said: (Sep 24, 2016) What is a bit field?

 Sankari said: (Sep 29, 2016) Give any two difference between union and structure.

 Pranali said: (Apr 1, 2017) In union, which datatype has highest size is equal to that union size. ex. union demo { int i; char c; }; sizeof(demo);// 4(bcoz size of int is greater than size of char) the addition of all data elements or data members is equal to the size of the structure. ex. struct demo { int i; char c; }; sizeof(demo);// 5(addition of int and char).

 Saltwind said: (Mar 8, 2018) What is meant by bitfields?

 Ishwarya said: (Jul 7, 2018) What is bit field?

 Javqui said: (Aug 11, 2018) The questions is CAN USE?, The answer is YES, and can be very useful if used with a struct. To be more specific, a struct of bitfields inside a union is a nice way to have two or more interpretations of the same data. It is very useful in low-level programming with hardware. union Point{ struct { unsigned int x:4; unsigned int y:4; }; int res; }; int main(){ union Point pt; pt.x = 2; pt.y = 3; printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y ); pt.res = pt.y; printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y ); return 0; }