bitfield and enum, is this legal?

E

Erik Cato

Hi group!

Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;

//Erik
 
B

Ben Pfaff

Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;

It's not portable. Bit-fields can only portably have type signed
int or unsigned int.
 
P

Peter Nilsson

Ben Pfaff said:
It's not portable. Bit-fields can only portably have type signed
int or unsigned int.

Or _Bool in C99.

Note that 'plain' int is also allowed, but it is implementation
defined as to whether this behaves as signed or unsigned int in the
context of bitfields.
 
J

Jack Klein

It's not portable. Bit-fields can only portably have type signed
int or unsigned int.

No, it is undefined behavior under any version of the standard prior
to C99. Semantics section of 6.5.2.1 of C90/95:

<quote>
A bit-field shall have a type that is a qualified or unqualified
version of one of int, unsigned int, or signed int. Whether the
high-order bit position of a (possibly qualified) “plain” int
bit-field is treated as a sign bit is implementation-defined. A
bit-field is interpreted as an integral type consisting of the
specified number of bits.
<unquote>

The result of violating a "shall" outside of a constraint clause is
undefined behavior.
 
J

Jack Klein

Hi group!

Is this code legal?

typedef enum
{
FALSE = 0,
TRUE = 1,
}t_bool;

typedef struct
{
t_bool b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1;
}t_bitfield8;

//Erik

It is undefined behavior prior to the C99 standard update and allowed
as an implementation-defined extension in C99.

The behavior of single bit bit-fields not defines specifically as
unsigned is very much implementation defined. Any field where the bit
is set to 1 might be promoted to the integer value -1, rather than +1,
in expressions.
 
B

Ben Pfaff

Jack Klein said:
No, it is undefined behavior under any version of the standard prior
to C99. Semantics section of 6.5.2.1 of C90/95:

Relying on undefined behavior is nonportable.
 
O

Old Wolf

typedef enum
It is undefined behavior prior to the C99 standard update and allowed
as an implementation-defined extension in C99.

What is the difference between these two cases.
If the standard calls something "undefined" then the implementation
is allowed to define it anyway. And if it is "allowed as an ID extension"
but the extension is not provided, then it is UB.
 
B

Barry Schwarz

What is the difference between these two cases.
If the standard calls something "undefined" then the implementation
is allowed to define it anyway. And if it is "allowed as an ID extension"
but the extension is not provided, then it is UB.

The difference is:

If it is undefined but your compiler accepts it today, there is no
guarantee what will happen tomorrow, if your compiler is updated, or
if you change systems.

If it is implementation defined, then every compliant compiler
must document what it will do (or in this case if the extension is
allowed).


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top