C言語の小ネタ集
処理系で型のサイズが異なる場合(特に long、long long など)や値の範囲が変わる場合(char)があるため、各型のサイズと範囲を確認するコードをこちらに置いてあります。
なお、構造体では要素の順序や pack を行うか否かなどにも依存して、そのサイズが変わるため、詳細につきましてはこちらをご参照下さい。
以下は、
gcc (Ubuntu 9.3.0-17ubuntu1~20.04)
5.4.72-microsoft-standard-WSL2
の場合の例:
Type :Size [octet] MIN MAX Note
----------------------------------------------------------------------------
char: 1 -128 127 1
signed char: 1 -128 127
unsigned char: 1 0 255
(signed) short int: 2 -32768 32767
unsigned short int: 2 0 65535
(signed) int: 4 -2147483648 2147483647
unsigned int: 4 0 4294967295
(signed) long int: 8 -9223372036854775808 9223372036854775807
unsigned long int: 8 0 18446744073709551615
(signed) long long int: 8 -9223372036854775808 9223372036854775807
unsigned long long int: 8 0 18446744073709551615
(IEEE754-2008 binary16):( 2) 0.000061035156 65504 3,4
(bfloat16):( 2) 1.175494350822e-38 3.389531389252e+38 2,3,4
float: 4 1.175494350822e-38 3.402823466385e+38 2,3
double: 8 2.225073858507e-308 1.797693134862e+308 2,3
long double: 16 3.362103143112e-4932 1.189731495357e+4932 2,3
pintptr_t: 8 -9223372036854775808 9223372036854775807
upintptr_t: 8 0 18446744073709551615
pointer (ptr): 8
int32_t *ptr : 4
void *void_ptr : 1
int32_t array[9]: 36
(signed) ssize_t: 8
(unsigned) size_t: 8
time_t: 8
struct timespec: 16
Note:
1. 'char' might be 'unsigned char' in some processing systems, though
types without 'signed' are the same as 'signed' ones in 'int' types.
2. MIN and MAX here are rounded.
3. MIN here is the minimum positive normal value and the smallest value
is given by -MAX.
4. Will be updated after implemented in C.
gcc version 9.3-win32 20200320 (GCC)
Target: i686-w64-mingw32
の場合の例:
Type :Size [octet] MIN MAX Note
----------------------------------------------------------------------------
char: 1 -128 127 1
signed char: 1 -128 127
unsigned char: 1 0 255
(signed) short int: 2 -32768 32767
unsigned short int: 2 0 65535
(signed) int: 4 -2147483648 2147483647
unsigned int: 4 0 4294967295
(signed) long int: 4 -2147483648 2147483647
unsigned long int: 4 0 4294967295
(signed) long long int: 8 -9223372036854775808 9223372036854775807
unsigned long long int: 8 0 18446744073709551615
float: 4 1.175494350822e-38 3.402823466385e+38 2,3
double: 8 2.225073858507e-308 1.797693134862e+308 2,3
long double: 12 3.362103143112e-4932 1.189731495357e+4932 2,3
pintptr_t: 4 -2147483648 2147483647
upintptr_t: 4 0 4294967295
pointer (ptr): 4
int32_t *ptr : 4
void *void_ptr : 1
int32_t array[9]: 36
(signed) ssize_t: 4
(unsigned) size_t: 4
time_t: 4
struct timespec: 8
gcc version 9.3-win32 20200320 (GCC)
Target: x86_64-w64-mingw32
の場合の例:
Type :Size [octet] MIN MAX Note
----------------------------------------------------------------------------
char: 1 -128 127 1
signed char: 1 -128 127
unsigned char: 1 0 255
(signed) short int: 2 -32768 32767
unsigned short int: 2 0 65535
(signed) int: 4 -2147483648 2147483647
unsigned int: 4 0 4294967295
(signed) long int: 4 -2147483648 2147483647
unsigned long int: 4 0 4294967295
(signed) long long int: 8 -9223372036854775808 9223372036854775807
unsigned long long int: 8 0 18446744073709551615
float: 4 1.175494350822e-38 3.402823466385e+38 2,3
double: 8 2.225073858507e-308 1.797693134862e+308 2,3
long double: 16 3.362103143112e-4932 1.189731495357e+4932 2,3
pintptr_t: 8 -9223372036854775808 9223372036854775807
upintptr_t: 8 0 18446744073709551615
pointer (ptr): 8
int32_t *ptr : 4
void *void_ptr : 1
int32_t array[9]: 36
(signed) ssize_t: 8
(unsigned) size_t: 8
time_t: 8
struct timespec: 16
sizeof()の引数に何を指定するかにより、得られるサイズが異なるため注意が必要:
int
などだが、ビット幅を指定する場合には int32_t
など。uint8_t
など。