3
3

More than 3 years have passed since last update.

C言語 データ型モデルを意識した型の情報一覧

Last updated at Posted at 2020-11-16

データ型モデル

型の最大値、最小値の一覧等まとめているサイトは多いですが、データ型モデルのことに触れずに書いているサイトが多いのでまとめて見ました。

OSではデータ型モデルと呼ばれる環境の定義規約によって基本型のサイズが定められています。
ここで64bitの主な環境について述べます。

LLP64 LP64
char 8 8
short 16 16
int 32 32
long 32 64
long long 64 64
void * 64 64

LLP64

ポインターのみを64ビット化したもの。
Microsoft Windowsの64ビット版(Win64)で採用されたデータ型モデル

ソースの互換性が高く64ビット化において最も問題が少ないと考えられている。

long long と pointer が64bit。

LP64

64ビット実装モデルの二つ目。ポインターとlongを64ビット化したもの。

64ビット版のLinux、FreeBSD/OpenBSD等BSD、その他一般にUNIXと解釈されているOS全般で採用されたデータ型モデル。

intとlongの長さが違うため、漠然と同じであることを想定したようなプログラムで問題が生じる恐れがある。

long と pointer が64bit。

LP64のデータ型一覧

整数型

説明 bit byte max min
short int 符号付整数 16bit 2byte 32767 -32768
int 符号付整数 32bit 4byte 2147483647 -2147483648
long int 符号付整数 64bit 8byte 9223372036854775807 -9223372036854775808
long long int 符号付整数 64bit 8byte 9223372036854775807 -9223372036854775808
char 文字(1文字) 8bit 1byte 127 -128
unsigned short int 符号なし整数 16bit 2byte 65535 0
unsigned int 符号なし整数 32bit 4byte 4294967295 0
unsigned long int 符号なし整数 64bit 8byte 18446744073709551615 0
unsigned long long int 符号なし整数 64bit 8byte 18446744073709551615 0
unsigned char 符号なしで文字 8bit 1byte 255 0

※ C言語の内部では文字は整数として扱っているためunsigned char型も存在する。
charは文字型のため、unsigned charの値幅がデフォルトになっているシステムもある。
Compiler User Guide: Basic data types in ARM C and C++

浮動小数点型

説明 bit byte max min
float 単精度実数 32bit 4byte 3.402823 * (10の38乗) (表現可能な最小の正の実数)1.175494 * (10の-38乗)
(表現可能な最小の実数)-3.402823 * (10の38乗)
double 倍精度実数 64bit 8byte 1.797693 * (10の308乗) (表現可能な最小の正の実数)2.225074 * (10の-308乗)
(表現可能な最小の実数)-1.797693 * (10の308乗)

stdint.hを使用して型を制御する

環境によって上限が変わってしまう型を使うのは危険なので、
stdint.hをインクルードして使っていくのが適切かと思いました。

標準ライブラリヘッダ - cppreference.com

参考

データ型モデル ‐ 通信用語の基礎知識

基本型 - cppreference.com

stdint.h

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3