Can bit-fields only be fields of a structure/union, never “normal”, “stand-alone” variables?

后端 未结 3 1737
闹比i
闹比i 2020-12-21 09:40

The field part of bit-fields seems to suggest that they can only be fields inside a structure or union.

Can a bit-field be a typical \"stand-alone\" variable, outsid

3条回答
  •  爱一瞬间的悲伤
    2020-12-21 10:23

    Bit-fields are only part of structs or unions because that's what the C standard allows. It would have been possible to decide differently. Why the committee decided to write the standard the way it is, you would have to ask them.

    To find the information in the C99 standard:

    From the table of contents:

    6.7 Declarations

    6.7.2 Type specifiers (okay, this one is a little bit obscure)

    6.7.2.1 Structure and union specifiers.

    One bit-field is the declarator_opt : constant-expression part of the syntax. This syntax is only allowed here, and a consequence is that one cannot declare bit-fields elsewhere.

    The syntax tells you that the name of a bit-field can optionally be omitted, if you were curious for this sort of information. Clause 6.7.2.1:3 goes further and prohibits names for 0-length bit-fields.

提交回复
热议问题