yohhoyの日記

技術的メモをしていきたい日記

C/C++ char型のビット幅定義

C23/C++23現在のプログラミング言語仕様では、char型のビット幅CHAR_BITは8bit以上の処理系定義(implementation-defined)と規定される。POSIX規格では厳密にCHAR_BIT==8と定義する。

POSIX

IEEE Std 1003.1, 2004, limits.hヘッダ仕様より一部引用。

Numerical Limits
{CHAR_BIT}
Number of bits in a type char.
 Value: 8

CHANGE HISTORY
Issue 6
The values for the limits {CHAR_BIT}, {SCHAR_MAX}, and {UCHAR_MAX} are now required to be 8, +127, and 255, respectively.

C++

C++標準ライブラリ仕様定義はC標準規格を規範的(normative)に参照している。C++23 17.3.6より一部引用。*1

#define CHAR_BIT see below

The header <climits> defines all macros the same as the C standard library header <limits.h>.
(snip)
See also: ISO C 5.2.4.2.1

C++2c(C++26)向けにCHAR_BIT==8を規定する提案文書P3477R5が検討されていたが、複数の強い反対意見(PDF)P3633R0, (PDF)P3635R0も提出され、2025年2月会合の投票では合意に至らず(not consensus)同提案は破棄された。*2

C

C23 5.2.5.3.2より一部引用(下線部は強調)。C17 5.2.4.2.1も実質的に同一内容。

The values given subsequently shall be replaced by constant expressions suitable for use in conditional expression inclusion preprocessing directives. Their implementation-defined values shall be equal or greater to those shown.

  • number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT  8

(snip)

関連URL

*1:C++23標準規格はC17(ISO/IEC 9899:2018)を引用規格(normative reference)と定める。

*2:https://github.com/cplusplus/papers/issues/2131