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.

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]

K.maheswari said:   1 decade ago
Any one explain this concept ?

Santosh said:   1 decade ago
Please someone explain this.

Jyoti said:   1 decade ago
Why char scheme:4; is valid ?

Since it is char of 1 byte field.

Jayaram said:   1 decade ago
Explain the concept and how it can be useful in progrm and how the value will get signed or unsigned.


Post your comments here:

Your comments will be displayed after verification.