这是一个简单的测试程序,可以帮助您了解发生了什么。请注意,它使用 C99 长度修饰符hh
,这意味着:
hh
指定后面的d
, i
, o
, u
, x
, 或X
转换说明符应用于
signed char
orunsigned char
参数(该参数将根据整数提升进行提升,但其值应转换为signed char
或unsigned char
在打印之前);或者后面的n
转换说明符适用于指向signed char
参数的指针。
这可以帮助您了解如何处理类型。
#include <stdio.h>
#include <limits.h>
static void print_value(signed char sc, unsigned char uc, /*plain*/ char pc)
{
int j1 = sc;
int j2 = uc;
int j3 = pc;
printf("%-9s %4hhd %4hhu %4d 0x%hhX %10u\n", "Signed:", j1, j1, j1, j1, j1);
printf("%-9s %4hhd %4hhu %4d 0x%hhX %10u\n", "Unsigned:", j2, j2, j2, j2, j2);
printf("%-9s %4hhd %4hhu %4d 0x%hhX %10u\n", "Plain:", j3, j3, j3, j3, j3);
}
static void check_value(int i)
{
signed char sc = i;
unsigned char uc = i;
/*plain*/ char pc = i;
print_value(sc, uc, pc);
}
int main(void)
{
for (int i = 0; i <= 3; i++)
check_value(i);
for (int i = SCHAR_MAX - 3; i <= SCHAR_MAX+3; i++)
check_value(i);
for (int i = UCHAR_MAX - 3; i <= UCHAR_MAX; i++)
check_value(i);
return 0;
}
编译时-fsigned-char
(所以 'plain'char
是有符号类型),输出为:
Signed: 0 0 0 0x0 0
Unsigned: 0 0 0 0x0 0
Plain: 0 0 0 0x0 0
Signed: 1 1 1 0x1 1
Unsigned: 1 1 1 0x1 1
Plain: 1 1 1 0x1 1
Signed: 2 2 2 0x2 2
Unsigned: 2 2 2 0x2 2
Plain: 2 2 2 0x2 2
Signed: 3 3 3 0x3 3
Unsigned: 3 3 3 0x3 3
Plain: 3 3 3 0x3 3
Signed: 124 124 124 0x7C 124
Unsigned: 124 124 124 0x7C 124
Plain: 124 124 124 0x7C 124
Signed: 125 125 125 0x7D 125
Unsigned: 125 125 125 0x7D 125
Plain: 125 125 125 0x7D 125
Signed: 126 126 126 0x7E 126
Unsigned: 126 126 126 0x7E 126
Plain: 126 126 126 0x7E 126
Signed: 127 127 127 0x7F 127
Unsigned: 127 127 127 0x7F 127
Plain: 127 127 127 0x7F 127
Signed: -128 128 -128 0x80 4294967168
Unsigned: -128 128 128 0x80 128
Plain: -128 128 -128 0x80 4294967168
Signed: -127 129 -127 0x81 4294967169
Unsigned: -127 129 129 0x81 129
Plain: -127 129 -127 0x81 4294967169
Signed: -126 130 -126 0x82 4294967170
Unsigned: -126 130 130 0x82 130
Plain: -126 130 -126 0x82 4294967170
Signed: -4 252 -4 0xFC 4294967292
Unsigned: -4 252 252 0xFC 252
Plain: -4 252 -4 0xFC 4294967292
Signed: -3 253 -3 0xFD 4294967293
Unsigned: -3 253 253 0xFD 253
Plain: -3 253 -3 0xFD 4294967293
Signed: -2 254 -2 0xFE 4294967294
Unsigned: -2 254 254 0xFE 254
Plain: -2 254 -2 0xFE 4294967294
Signed: -1 255 -1 0xFF 4294967295
Unsigned: -1 255 255 0xFF 255
Plain: -1 255 -1 0xFF 4294967295
用 -funsigned-char 编译(so 'plain'
char` 是无符号类型),输出为:
Signed: 0 0 0 0x0 0
Unsigned: 0 0 0 0x0 0
Plain: 0 0 0 0x0 0
Signed: 1 1 1 0x1 1
Unsigned: 1 1 1 0x1 1
Plain: 1 1 1 0x1 1
Signed: 2 2 2 0x2 2
Unsigned: 2 2 2 0x2 2
Plain: 2 2 2 0x2 2
Signed: 3 3 3 0x3 3
Unsigned: 3 3 3 0x3 3
Plain: 3 3 3 0x3 3
Signed: 124 124 124 0x7C 124
Unsigned: 124 124 124 0x7C 124
Plain: 124 124 124 0x7C 124
Signed: 125 125 125 0x7D 125
Unsigned: 125 125 125 0x7D 125
Plain: 125 125 125 0x7D 125
Signed: 126 126 126 0x7E 126
Unsigned: 126 126 126 0x7E 126
Plain: 126 126 126 0x7E 126
Signed: 127 127 127 0x7F 127
Unsigned: 127 127 127 0x7F 127
Plain: 127 127 127 0x7F 127
Signed: -128 128 -128 0x80 4294967168
Unsigned: -128 128 128 0x80 128
Plain: -128 128 128 0x80 128
Signed: -127 129 -127 0x81 4294967169
Unsigned: -127 129 129 0x81 129
Plain: -127 129 129 0x81 129
Signed: -126 130 -126 0x82 4294967170
Unsigned: -126 130 130 0x82 130
Plain: -126 130 130 0x82 130
Signed: -4 252 -4 0xFC 4294967292
Unsigned: -4 252 252 0xFC 252
Plain: -4 252 252 0xFC 252
Signed: -3 253 -3 0xFD 4294967293
Unsigned: -3 253 253 0xFD 253
Plain: -3 253 253 0xFD 253
Signed: -2 254 -2 0xFE 4294967294
Unsigned: -2 254 254 0xFE 254
Plain: -2 254 254 0xFE 254
Signed: -1 255 -1 0xFF 4294967295
Unsigned: -1 255 255 0xFF 255
Plain: -1 255 255 0xFF 255
在 Mac OS X 10.7.4 上使用 GCC 4.7.1 编译(但使用平台上的标准 C 库)。