Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning when using bitfield with unsigned char

Tags:

c

This is my bitfield

struct {
    unsigned char v64 : 1;
    unsigned char leg : 7;
} valid;

Then I get the warning:

main.c:17:3: warning: type of bit-field ‘v64’ is a GCC extension [-pedantic]
main.c:18:3: warning: type of bit-field ‘leg’ is a GCC extension [-pedantic]

If I change to int there is no warning. But I want a bitfield of a byte (unsigned char).

How?

like image 295
Fabricio Avatar asked Jun 05 '12 23:06

Fabricio


1 Answers

Remove the gcc -pedantic option if you don't want to get the warning.

In C99, gcc issues a warning with -pedantic but it is permitted to have an implementation defined type for the bit-field (like unsigned char).

(C99, 6.7.2.1p4) "A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type."

In C90, only int, unsigned int and signed int are permitted.

(C90, 6.5.2.1) "A bit-field shall have a type that is a qualified or unqualified version of one of int, unsigned int, or signed int"

Actually in both C90 and C99 the warning is not required by C (it is undefined behavior in C90 only but C doesn't not require a warning for undefined behavior). The warning is added by gcc with -pedantic for information only.

like image 71
ouah Avatar answered Sep 26 '22 06:09

ouah