|
[Original]
[Print]
[Top]
|
如下的一段简单代码:
#include "stdio.h"
main()
{
char left,right1,right2,base,result1,result2,result3;
base = 255;
left = 1;
right1 = (base+2)&0xff;
right2 = (base+2)%256;
result1 = (left == (base+2)&0xff);
result2 = (left == (base+2)%256);
result3 = (left == base+2);
printf("result1 = 0x%x,result2 = 0x%x, result3 = 0x%x
",result1,result2,result3);
return 0;
}
在x86+winxp+vc++6.0上以及x86+rh8.0+gcc3.2上运行,结果都是:
result1 = 0x1,result2 = 0x1, result3 = 0x1
但是在arm9的一个板子上,用arm-elf-gcc2.95.3编译,在uclinux2.4.17下运行,其结果为:
result1 = 0x0,result2 = 0x1, result3 = 0x0
哪位可以解释这个现象?是arm cpu导致该特性还是arm-elf-gcc编译器有问题?
谢谢!
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
是编译器导致的,不是硬件问题。
但是根本原因有2个:
1. 你的程序写得不好,不规范。
2. ANSIC的语义有二义性:
摘自ISO/IEC 9899:1999
The three types char, signed char, and unsigned char are collectively called
the character types. The implementation shall dene char to have the same range,
representation, and behavior as either signed char or unsigned char.
解决办法:
修改
char left,right1,right2,base,result1,result2,result3;
为
signed char left,right1,right2,base,result1,result2,result3;
你先在arm/x86下分别执行下这段代码:
include <limits.h>
int main()
{
printf("%d
", CHAR_MIN);
}
然后你就会明白怎么回事了
|
|
|
[Original]
[Print]
[Top]
|
|