开发者

gcc(v4.1.2 cross-compiler) integer promotion problem

开发者 https://www.devze.com 2023-01-29 15:32 出处:网络
This is the test code. char ch = 0xff; int i = ch; printf(\"%d\\n\", i); In i386 gcc-4.4.5, the output is -1.

This is the test code.

char ch = 0xff;
int i = ch;
printf("%d\n", i);

In i386 gcc-4.4.5, the output is -1. But in powerpc-e300c3-linux-gnu-gcc-4.1.2(MPC8315 cross-compiler), the output is 255.

What is wrong? Why gcc-4.1.2 output is 255?

Thank开发者_开发问答s for your answer...


It is implementation-defined whether char is signed or unsigned.

Apparently it is signed on your x86 compiler and unsigned on your PowerPC compiler.

For portability, use unsigned char or signed char wherever you care about the signedness.

0

精彩评论

暂无评论...
验证码 换一张
取 消