gnu linux 上ARM默认char是不带符号的。微软wince上char又默认是signed。
来源:互联网 发布:伊巴卡生涯数据 编辑:程序博客网 时间:2024/05/16 10:08
char i = -1;
而另一种机器上,默认char带符号,就会确切地把-1赋予i。如果程序员本意是把-1保
存在i中,那么前面的代码就该修改成:
signed char i = -1;另外,如果程序员确实希望存储255,那么代码应该如下:
unsigned char = 255;
如果你在自己的代码中使用了char类型,那么你要保证在带符号和不带符号的情况下
代码都没问题。如果你能明确要用的是哪一个,那么就直接声明它。
The following email fragment appeared on the linux-arm mailing list recently: consider this simple program:
int main(void){ char i = -1; printf("%d\n", i); return 0;}
The print out is 255 in stead of -1, unless I define i as signed char i;then I get the "-1" print out. The above code is actually buggy in that it assumes that the type "char" is equivalent to "signed char". The C standards do say that "char" may either be a "signed char" or "unsigned char" and it is up to the compilers implementation or the platform which is followed. As the poster points out, the above code does not work as expected if "char" is "unsigned". It is difficult to detect this code at compile time, since GCC does not issue any warnings. The only way to detect it is either by visual examination of the code, or by actually running it and finding a problem.This causes problems on ARM based machines since "char" is of the "unsigned" variety, which allows the compiler to generate faster, more efficient code. ARM is not alone in this - SGI Mips running IRIX also encounters this problem.However, dispite the lack of warning for the above case, GCC does warn with the following code:
{ char foo; foo = bar(); if (foo == -1) { ... }}
Code like the above will generate a compiler warning, which will be one of the following depending on the actual test used:
warning: comparison is always 0 due to limited range of data type
warning: comparison is always 1 due to limited range of data type
Please note however that the above warnings are not issued if "char" is "signed" and therefore can be difficult to pick up when compiling in such an environment. The following table lists the four types of code which cause problems when "c" is declared as just "char", and the most likely correct method of fixing the code.
- gnu linux 上ARM默认char是不带符号的。微软wince上char又默认是signed。
- gnu linux 上ARM默认char是不带符号.
- char类型默认是signed还是unsigned?
- arm-linux-gcc char类型默认是unsigned char
- arm-linux-gcc char与signed char和unsigned char
- C陷阱篇之char的默认符号
- ARM上char类型问题
- char, signed char, unsigned char的区别
- char、signed char、unsigned char的区别
- 【test1】char、int之间编译器是如何默认转换的?
- signed char & unsigned char
- char、signed char、unsigned char
- char & unsigned char & signed char
- linux上设置oracle默认的编辑器
- 卸载Linux上默认的JDK
- unsigned char 、signed char 和 char的不同
- char与signed char, unsigned char的区别
- C - char与signed char, unsigned char的区别
- RMI报错:java.rmi.UnmarshalException: Error unmarshaling return;
- 上传文件中的中文乱码问题
- 直接从google中引入jquery.js
- Android 的网络编程
- table control里面的f4
- gnu linux 上ARM默认char是不带符号的。微软wince上char又默认是signed。
- ini文件读取
- python正则表达式指南
- Linux下的多线程编程
- Ext.grid.CheckColumn()与 Ext.grid.CheckboxSelectionModel() 的应用
- smarty使用总结
- MySQL 5.1新特性之事件调度器(Event Scheduler)
- 搜索页中调用arclist或者自定义模型
- 我很“营养”?你真的“营养”吗?