Maximum size of a bit field in C or C++? [duplicat

2019-02-21 16:13发布

问题:

Possible Duplicate:
struct bitfield max size (C99, C++)

Is there a limit to the number of bits that I can specify in a bit field in C or C++? For example, could I do this:

struct HugeInt {
    int myInt: 1000;
};

I'm asking about both C and C++, since I know that the language specs sometimes differ and wanted to see if the above example was guaranteed to work / not work in C or C++.

回答1:

In C11, section 6.7.2.1, clause 4:

The expression that specifies the width of a bit-field shall be an integer constant expression with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted. If the value is zero, the declaration shall have no declarator.

So in short, it must be between zero and the size of the type if it had not had a bit-field part.



回答2:

size of bit-field 'myInt' (1000 bits) cannot exceed size of its type (int, usually 32 bits)