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.
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 1 of 2.
Sumanth said:
1 decade ago
I have read in a text book that bit fields are allowed in unions.
Prabhu said:
1 decade ago
Yes, I also read that bitfields are allowed in union.
Khushnood said:
1 decade ago
Yes yes, I also read in book that bit are allowed in unoin.
Sathuragiri said:
1 decade ago
What you mean by bit field? explain well?
Sanjul said:
1 decade ago
What is bit field?
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.
So that sentence is FALSE. Therefore the answer given here is correct and as per your text books. So just chill.
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.
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:
1 decade ago
Yes. @Soumya Roy you are right.
Ranjana said:
1 decade ago
Why we made union. What is its purpose?
Sirisha said:
1 decade ago
What is bit field?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers