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 3 of 3.

Anvi said:   9 years ago
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:   8 years ago
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.
(1)

Monish kawale said:   7 years ago
#Monisha

The bitfield method is to avoid structure padding.
Eg:

Struct monisha
{
Int a: 4
Char b: 3
};

Here the size of the structure is 4 byte as it takes max size data type as int is having 4 bytes which is more than char hence structure takes 4 bytes and in that 4 bytes int a will take 4 bits and char b will take 3 bits.

This is known as but fled to avoid memory leakage.

Shailaja said:   7 years ago
Here, also we can't define a variable of the struct, so without it how we find sizeof struct a?

Vasam sai prathyusha said:   6 years ago
Can anyone explain what is structure padding? and what is the size structure?


Post your comments here:

Your comments will be displayed after verification.