2.2 データ型とサイズ, 演習2-1 K&R プログラミング言語C

2.2 データ型とサイズ

C言語の基本的なデータ型

型名 型の内容
char 1byteの文字1つを保持できる。
int 整数、計算機が最も自然に扱える整数のサイズ
float 単精度浮動小数点数
double 倍精度浮動小数点数

int には shortlong の修飾子を適用できる。

short int si;
long int li;
/* int は省略可能 */
short sj;
long lj;

演習2-1

#include <stdio.h>
#include <limits.h>

long signed_min(long byte);
long signed_max(long byte);

int main(int argc, char *argv[])
{
    printf("%15s: %ld byte, min: %20d, max: %20u\n", "unsigned int", sizeof(unsigned int), 0, UINT_MAX);
    printf("%15s: %ld byte, min: %20d, max: %20d\n", "signed int", sizeof(signed int), INT_MIN, INT_MAX);

    printf("%15s: %ld byte, min: %20d, max: %20u\n", "unsigned char", sizeof(unsigned char), 0, UCHAR_MAX);
    printf("%15s: %ld byte, min: %20d, max: %20d\n", "signed char", sizeof(signed char), SCHAR_MIN, SCHAR_MAX);

    printf("%15s: %ld byte, min: %20d, max: %20u\n", "unsigned short", sizeof(unsigned short), 0, USHRT_MAX);
    printf("%15s: %ld byte, min: %20d, max: %20d\n", "signed short", sizeof(signed short), SHRT_MIN, SHRT_MAX);

    printf("%15s: %ld byte, min: %20d, max: %20lu\n", "unsigned long", sizeof(unsigned long), 0, ULONG_MAX);
    printf("%15s: %ld byte, min: %20ld, max: %20ld\n", "signed long", sizeof(signed long), LONG_MIN, LONG_MAX);

    printf("----- calculate -----\n"); /* 以下は直接計算した結果 */
    printf("%15s: %ld byte, min: %20ld, max: %20ld\n", "signed int", sizeof(signed int), signed_min(sizeof(signed int)), signed_max(sizeof(signed int)));
    printf("%15s: %ld byte, min: %20ld, max: %20ld\n", "signed char", sizeof(signed char), signed_min(sizeof(signed char)), signed_max(sizeof(signed char)));
    printf("%15s: %ld byte, min: %20ld, max: %20ld\n", "signed short", sizeof(signed short), signed_min(sizeof(signed short)), signed_max(sizeof(signed short)));

    return 0;
}

long signed_min(long byte)
{
    int i;
    long p, bit;
    bit = byte * 8;

    p = 1;
    for (i = 0; i < bit; ++i) {
        p = p * 2;
    }
    p = ((p + 1) / 2) * -1;

    return p;
}

long signed_max(long byte)
{
    int i;
    long p, bit;
    bit = byte * 8;

    p = 1;
    for (i = 0; i < bit; ++i) {
        p = p * 2;
    }
    p = ((p - 1) / 2);

    return p;
}

実行結果

$ ./ex2-1
   unsigned int: 4 byte, min:                    0, max:           4294967295
     signed int: 4 byte, min:          -2147483648, max:           2147483647
  unsigned char: 1 byte, min:                    0, max:                  255
    signed char: 1 byte, min:                 -128, max:                  127
 unsigned short: 2 byte, min:                    0, max:                65535
   signed short: 2 byte, min:               -32768, max:                32767
  unsigned long: 8 byte, min:                    0, max: 18446744073709551615
    signed long: 8 byte, min: -9223372036854775808, max:  9223372036854775807
----- calculate -----
     signed int: 4 byte, min:          -2147483648, max:           2147483647
    signed char: 1 byte, min:                 -128, max:                  127
   signed short: 2 byte, min:               -32768, max:                32767
プログラミング言語C 第2版 ANSI規格準拠
B.W. カーニハン D.M. リッチー
共立出版
売り上げランキング: 9726
«
»