LoginSignup
1
1

More than 3 years have passed since last update.

c言語の基本データ型の範囲(網羅性は微妙)

Last updated at Posted at 2020-02-08

はじめに

型の範囲に悩まされることがあるので、「OSによって型の範囲が変わる」という部分も含め、ここに忘備録も兼ねてまとめます。ソースは大学の講義といくつかのサイトです。

OSの種類と型の範囲(基本データ型)

C言語の規格では、short ≤ int ≤ long ≤ long long という大小関係が定められているのみで、ビット幅は処理系に依存します。ほとんどのパソコンはここに挙げているもののどれかに当てはまるはずです。

16bit OS

char short int long long long ポインタ
8 16 16 32 - 16

32bit OS

char short int long long long ポインタ コメント(推測)
ILP32 8 16 32 32 64 32 IntとLongとPointerが32bit
LP32 8 16 16 32 64 32 LongとPointerが32bit

64bit OS

char short int long long long ポインタ コメント(推測)
LLP64 8 16 32 32 64 64 Long LongとPointerが64bit ※Windows64bit
LP64 8 16 32 64 64 64 Long以降とPointerが64bit ※Unix
IP64 8 16 64 64 64 64 Int以降とPointerが64bit

基本データ型モデルの判定

実行環境

cygwin 3.0.7-1
Surface Pro4

ソースコード

#include <stdio.h>

int main(void){
  if(sizeof(char*)==4){
    if(sizeof(int)==sizeof(long)){
      printf("I");
    }
    printf("LP32\n");
  }else if(sizeof(char*)==8){
  if(sizeof(long)!=sizeof(long long)){
      printf("LL");
    }else if(sizeof(int)!=sizeof(long)){
      printf("L");
    }else{
      printf("I");
    }
    printf("P64\n");
  }else{
    printf("Error!\n");
  }
}

実行結果(例)

$ ./a.exe
LP64

型の範囲

ビット幅 計算式 範囲
8 $$ -2^7~2^7-1 $$ -128〜127
16 $$ -2^{15}~2^{15}-1 $$ -32768〜32767
32 $$ -2^{31}~2^{31}-1 $$ -2147483648〜2147483647
64 $$ -2^{63}~2^{63}-1 $$ -9223372036854775808〜9223372036854775807

16bitOS

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 16 -32768〜32767 %d
long 32 -2147483648〜2147483647 %ld
long long - - %lld
pointer 16 -32768〜32767 %n or %p

ILP32(多くの32bit処理系)

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 32 -2147483648〜2147483647 %d
long 32 -2147483648〜2147483647 %ld
long long 64 -9223372036854775808〜9223372036854775807 %lld
pointer 32 -2147483648〜2147483647 %n or %p

LP32

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 16 -32768〜32767 %d
long 32 -2147483648〜2147483647 %ld
long long 64 -9223372036854775808〜9223372036854775807 %lld
pointer 32 -2147483648〜2147483647 %n or %p

LLP64(Windows64bit)

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 32 -2147483648〜2147483647 %d
long 32 -2147483648〜2147483647 %ld
long long 64 -9223372036854775808〜9223372036854775807 %lld
pointer 64 -9223372036854775808〜9223372036854775807 %n or %p

LP64(Unix系OS)

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 32 -2147483648〜2147483647 %d
long 64 -9223372036854775808〜9223372036854775807 %ld
long long 64 -9223372036854775808〜9223372036854775807 %lld
pointer 64 -9223372036854775808〜9223372036854775807 %n or %p

IP64

ビット幅 範囲 指定子
char 8 -128〜127 %c
short 16 -32768〜32767 %d
int 64 -9223372036854775808〜9223372036854775807 %d
long 64 -9223372036854775808〜9223372036854775807 %ld
long long 64 -9223372036854775808〜9223372036854775807 %lld
pointer 64 -9223372036854775808〜9223372036854775807 %n or %p

参考サイト

1
1
3

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
1
1