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

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

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?

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.

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)

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.

Vishalakshi said:   9 years ago
@Professor_X.

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.

Chinni said:   9 years ago
@Professor_X.

You said that sizeof struc is the biggest datatype.

Then what is the size if UNION?

Sudha said:   1 decade ago
Nice explanation friends. Thanks.

Priya said:   1 decade ago
What is the use of number after category:5 and scheme:4?

Rahul said:   1 decade ago
What is signed int or unsigned int?


Post your comments here:

Your comments will be displayed after verification.