分类: C/C++
2009-05-16 10:34:29
之前感觉这不可思议啊!明明都没错啊,呵呵后来终于发现问题出在格式控制符上了。unsigned short 对应的应该是%hu 。之前我自以为,这里应该是%ud,这有点定式思维了。所以只需将程序中红色字体部分的%d改为%hu,一切皆可搞定!!! 这可是让我烦恼了好几天啊!!
下面给出一张表这个表要好好看啊.....
关键字 | 位长 | 范围 | printf chars |
---|---|---|---|
char |
1 | -128..127(或0..255,与体系结构相关) | %c |
unsigned char |
1 | 0..255 | |
signed char |
1 | -128..127 | |
int |
2 or 4 |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
unsigned int |
2 or 4 |
0..65535 or 0..4294967295 |
%u |
signed int |
2 or 4 |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
short int |
2 | -32768..32767 | %hi |
unsigned short |
2 | 0..65535 | %hu |
signed short |
2 | -32768..32767 | |
long int |
4 | -2147483648..2147483647 | %li, %ld |
unsigned long |
4 | 0..4294967295 | %lu |
signed long |
4 | -2147483648..2147483647 | |
long long |
8 | -9223372036854775808..9223372036854775807 | %lli |
unsigned long long |
8 | 0..18446744073709551615 | %llu |
float |
4 | 3.4x10-38..3.4x10+38 (7 sf) | %f, %e, %g |
double |
8 | 1.7x10-308..1.7x10+308 (15 sf) | %f, %e, %g |
long double |
8 或以上 | 编译器相关 | %Lf, %Le, %Lg |