数値データ型の値域

C言語のchar型は1バイトであるが、とりうる値の範囲は処理系に任されているそうです。
ですがunsigned charにすることでとりうる値を0〜255にし処理系依存を防げるようです。
とりあえずRedhat8.0ではどうなるのか実行してみる。
ちなみにJavaでは仕様で-128〜127と決まってます。そもそも、処理系依存の概念がない言語です。

環境
# uname -a
Linux localhost.localdomain 2.4.20-28.8 #1 Thu Dec 18 12:53:39 EST 2003 i686 i686 i386 GNU/Linux


サンプルソース
#include <stdio.h>

/* prototype */
void test();
void test2();

int main(int argc, char *argv[]) {
        test();
        test2();
        return 0;
}

void test() {
        int testdata = 2;
        char tc = 0xff;
        printf("char size  \t:%d\n", sizeof(char));
        printf("short size \t:%d\n", sizeof(short));
        printf("int size   \t:%d\n", sizeof(int));
        printf("long size  \t:%d\n", sizeof(long));
        printf("float size \t:%d\n", sizeof(float));
        printf("doublesize \t:%d\n", sizeof(double));

        /* two's complement */
        printf("two's complement:%d\n", tc);
        // 0x01 0000 0000 0000 0000 0000 0000 0000 0001
        tc = 0x80; // -128
        printf("two's complement:%d\n", tc);
        tc = 0x7f; // -128
        printf("two's complement:%d\n", tc);
        printf("hello\n");
}

void test2() {
        printf("test2\n");
        unsigned char uc_data = 0x7f; // 127
        signed char sc_data = 0x7f; // 127
        printf("unsigned char %d\n", uc_data);
        printf("signed char%d\n", sc_data);
        uc_data = 0xff; // 255
        sc_data = 0xff; // -1
        printf("unsigned char %d\n", uc_data);
        printf("signed char%d\n", sc_data);
}

結果
char size       :1
short size      :2
int size        :4
long size       :4
float size      :4
doublesize      :8
two's complement:-1
two's complement:-128
two's complement:127
hello
test2
unsigned char 127
signed char127
unsigned char 255
signed char-1

確かにsigned, unsignedを指定することでとりうる値が指定できるのが確認できました。