C Programming - Structures, Unions, Enums - Discussion


Point out the error in the program?


int main()
    struct a
        float category:5;
        char scheme:4;
    printf("size=%d", sizeof(struct a));
    return 0;

[A]. Error: invalid structure member in printf
[B]. Error in this float category:5; statement
[C]. No error
[D]. None of above

Answer: Option B


Bit field type must be signed int or unsigned int.

The char type: char scheme:4; is also a valid statement.

Ashwani said: (Jan 15, 2011)  
Can anyone explain this please ?

Jayaram said: (Jan 16, 2011)  
Explain the concept and how it can be useful in progrm and how the value will get signed or unsigned.

Jyoti said: (Aug 5, 2011)  
Why char scheme:4; is valid ?

Since it is char of 1 byte field.

Santosh said: (Oct 3, 2011)  
Please someone explain this.

K.Maheswari said: (Jan 24, 2012)  
Any one explain this concept ?

Prashant O_O said: (Feb 10, 2012)  
Bit field can not be used for floating values
it can be used with => signed int ,unsigned int and char
the meaning of "char scheme:4;"
is only 4-bit are reserved for "scheme" not 4-byte(people always make that confusion) here is one example
[1][1][1][1][1][1][1][1] -->8-bit field for char
but for char scheme:4;
the first 4 are not reserved and the last 4 will be reserved for "scheme".
like this (assume 0 for unreserved and 1 for reserved)

Amit said: (Mar 30, 2012)  
What do you mean by un/reserved bits?

Rashmi said: (Aug 26, 2012)  

very nice explaination...!!

Swati said: (Oct 14, 2012)  
How float category: 5 is wrong? can anyone explain this?

Arun Prasad said: (Nov 20, 2012)  
When we run this program it will show an error that "Bit Field must be an Integral Type". This means that bit fields must be of type signed int or unsigned int and char. We cannot define float data type. Therefore,

int a:3; /* valid */

float b:3; /* Invalid */

One more thing, If we do not mention the type then also it will show an error.

struct a
{ /*
category: 5; Invalid code
scheme: 4; */

Ritesh_Iiit said: (Jul 19, 2013)  
@Prashant O_O: Friend I ran this code on gcc compiler after making few changes, here is my code:


int main()
struct a
int category:5;
char scheme:4;

printf("size=%d", sizeof(struct a));
return 0;

And according to your suggestion output must be 9 but it is 8 even take any values in the bit field still the output will be 8. Can you please explain me this.

Professor_X said: (Sep 4, 2013)  
You can test this.

The minimum size of a structure variable will be the size of the biggest data type in the structure.


In this example (*replace float with int).

sizeof (struct a) will be a minimum of 4 in 32-bit turboc.

Please correct me if I'm wrong.

Monisha said: (Aug 2, 2014)  
What is bit field? explain it in detail.

Prema said: (Aug 2, 2014)  
What is the purpose of reserving the bits for a variable?

As the size of integer is 2 bytes or 4 bytes depending on the compiler why we should reserve 5 bits in this code?

Chaitu said: (Aug 7, 2014)  

The main purpose of reserving bits in declaring them is the memory allocation for the variables can be used for what we want.

So by allocating required number of bits, we can save the memory.

Rahul said: (Oct 13, 2014)  
What is signed int or unsigned int?

Priya said: (Oct 30, 2014)  
What is the use of number after category:5 and scheme:4?

Sudha said: (Mar 25, 2015)  
Nice explanation friends. Thanks.

Chinni said: (Jul 4, 2016)  

You said that sizeof struc is the biggest datatype.

Then what is the size if UNION?

Vishalakshi said: (Dec 9, 2016)  

As you said, The minimum size of a structure variable will be the size of the biggest data type in the structure.

This sentence is valid only for union not structure because the structure will allocate different memory for different data types. But union allocates memory for the largest data type.

Please correct if I'm wrong. Thank you.

Anvi said: (Jan 11, 2017)  
1. The amount of memory required to store a structure variable is the sum of the size of all the members.

On the other hand, in the case of unions, the amount of memory required is always equal to that required by its largest member.

2. In the case of structure, each member has their own memory space but In union, one block is used by all the member of the union.

Girish Adhvaitha said: (Jul 19, 2017)  
Float and double will fallow the IEEE-754 standard to store the values into the memory.

If we use that as a bit-field member in a structure. It won't able to follow the method so in big field concept float I avoided to use and also for this reason if you store the same value in float and double, they won't be equal. Because the storage method is different for float and double
Float x=1.1f,
Double y=1.1,
X n't equal to Y.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.