Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Bit fields==== C also provides a special type of member known as a [[bit field]], which is an integer with an explicitly specified number of bits. A bit field is declared as a structure (or union) member of type {{code|int}}, {{code|signed int}}, {{code|unsigned int}}, or {{code|_Bool}}<!-- Add bool and _BitInt(N) when updating the page for C23 -->,<ref group="note">Other implementation-defined types are also allowed. C++ allows using all integral and enumerated types and a lot of C compilers do the same.</ref> following the member name by a colon ({{code|:}}) and the number of bits it should occupy. The total number of bits in a single bit field must not exceed the total number of bits in its declared type (this is allowed in C++ however, where the extra bits are used for padding). As a special exception to the usual C syntax rules, it is implementation-defined whether a bit field declared as type {{code|int}}, without specifying {{code|signed}} or {{code|unsigned}}, is signed or unsigned. Thus, it is recommended to explicitly specify {{code|signed}} or {{code|unsigned}} on all structure members for portability. Unnamed fields consisting of just a colon followed by a number of bits are also allowed; these indicate [[data padding|padding]]. Specifying a width of zero for an unnamed field is used to force [[data structure alignment|alignment]] to a new word.<ref>Kernighan & Richie</ref> Since all members of a union occupy the same memory, unnamed bit-fields of width zero do nothing in unions, however unnamed bit-fields of non zero width can change the size of the union since they have to fit in it. The members of bit fields do not have addresses, and as such cannot be used with the address-of ({{code|&}}) unary operator. The {{code|sizeof}} operator may not be applied to bit fields. The following declaration declares a new structure type known as {{code|f}} and an instance of it known as {{code|g}}. Comments provide a description of each of the members: <syntaxhighlight lang=C> struct f { unsigned int flag : 1; /* a bit flag: can either be on (1) or off (0) */ signed int num : 4; /* a signed 4-bit field; range -7...7 or -8...7 */ signed int : 3; /* 3 bits of padding to round out to 8 bits */ } g; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)