C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 3)
3.
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct a
    {
        float category:5;
        char scheme:4;
    };
    printf("size=%d", sizeof(struct a));
    return 0;
}
Error: invalid structure member in printf
Error in this float category:5; statement
No error
None of above
Answer: Option
Explanation:

Bit field type must be signed int or unsigned int.

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

Discussion:
25 comments Page 2 of 3.

Chaitu said:   1 decade ago
@Prema.

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.

Prema said:   1 decade ago
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?

Monisha said:   1 decade ago
What is bit field? explain it in detail.

Professor_x said:   1 decade ago
You can test this.

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

i.e.,

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.

Ritesh_IIIT said:   1 decade ago
@Prashant O_O: Friend I ran this code on gcc compiler after making few changes, here is my code:

#include<stdio.h>

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.

Arun Prasad said:   1 decade ago
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; */
}

Swati said:   1 decade ago
How float category: 5 is wrong? can anyone explain this?

Rashmi said:   1 decade ago
prashant

very nice explaination...!!

Amit said:   1 decade ago
What do you mean by un/reserved bits?

Prashant o_O said:   1 decade ago
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)
[0][0][0][0][1][1][1][1]


Post your comments here:

Your comments will be displayed after verification.