关于ARM的C语言数据类型
2013-01-28 08:49
465 查看
C语言的程序优化与编译器和硬件系统都有关系,设置某些编译器选项是最直接最简单的优化方式。在默认的情况下,armcc是全部优化功能有效的,而GNU编译器的默认状态下优化都是关闭的。ARM C编译器中定义的char类型是8位无符号的,有别于一般流行的编译器默认的char是8位有符号的。所以循环中用char变量和条件 i ≥ 0时,就会出现死循环。为此,可以用fsigned - char(for gcc)或者-zc(for armcc)把char改成signed。
其他的变量类型如下:
char 无符号8位字节数据(和X86下的不同)
short 有符号16位半字节数据
int 有符号32位字数据
long 有符号32位字数据
long long 有符号64位双字数据
====================================另一篇文章===========================
C标准表示char类型可以带符号也可以不带符号,由具体的编译器、处理器或由它们两者共同决定到底char是带符号合适还是不带符号合适。
大部分体系结构上,char默认是带符号的,它可以自-128到127之间取值。而也有一些例外,比如ARM体系结构上,char就是不带符号的,它的取值范围是0~255
举例来说,在默认char不带符号,下面的代码实际会把255而不是-1赋予i:
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 listrecently:
The above code is actually buggy in that it assumes that the type "char" isequivalent to "signed char". The C standards do say that "char" may eitherbe a "signed char" or "unsigned char" and it is up to the compilersimplementation 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, sinceGCC does not issue any warnings. The only way to detect it is either byvisual examination of the code, or
by actually running it and finding aproblem.
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 thisproblem.
However, dispite the lack of warning for the above case, GCC does warn withthe following code:
Code like the above will generate a compiler warning, which will be one ofthe following depending on the actual test used:
Please note however that the above warnings are not issued if "char"is "signed" and therefore can be difficult to pick up when compiling insuch an environment.
The following table lists the four types of code which cause problemswhen "c" is declared as just "char", and the most likely correct methodof fixing the code.
其他的变量类型如下:
char 无符号8位字节数据(和X86下的不同)
short 有符号16位半字节数据
int 有符号32位字数据
long 有符号32位字数据
long long 有符号64位双字数据
====================================另一篇文章===========================
C标准表示char类型可以带符号也可以不带符号,由具体的编译器、处理器或由它们两者共同决定到底char是带符号合适还是不带符号合适。
大部分体系结构上,char默认是带符号的,它可以自-128到127之间取值。而也有一些例外,比如ARM体系结构上,char就是不带符号的,它的取值范围是0~255
举例来说,在默认char不带符号,下面的代码实际会把255而不是-1赋予i:
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 listrecently:
> 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" isequivalent to "signed char". The C standards do say that "char" may eitherbe a "signed char" or "unsigned char" and it is up to the compilersimplementation 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, sinceGCC does not issue any warnings. The only way to detect it is either byvisual examination of the code, or
by actually running it and finding aproblem.
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 thisproblem.
However, dispite the lack of warning for the above case, GCC does warn withthe following code:
{ char foo; foo = bar(); if (foo == -1) { ... } }
Code like the above will generate a compiler warning, which will be one ofthe 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 insuch an environment.
The following table lists the four types of code which cause problemswhen "c" is declared as just "char", and the most likely correct methodof fixing the code.
相关文章推荐
- 笔记--关于c语言中各种数据类型所占字节。
- <关于数据类型的一些看法---C语言版本>
- ARM的C语言编程忠告之C数据类型的有效用法
- 关于c语言中的uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
- 关于一个c语言中的小陷阱(数据类型之间的转换)
- arm体系中c语言的数据类型
- 关于C语言中数据类型的转化方式
- VC常用的Windows数据类型与ARM嵌入式C语言的数据类型
- C语言中关于指针的数据类型和指针运算的小结
- 关于C语言的数据类型需要了解的基本概念
- 关于C语言的隐式类型转换
- 关于结构体包含结构体的类型数据大小
- 关于int型整数按逆序存入数组中的问题(也即十进制数和其他类型数据的转换问题)
- C语言数据类型的修饰符
- Microsoft visual C++ 2010学习版中C语言基本数据类型所占字节大小(系统为64位win8.1)
- C语言与java语言中数据类型的差别总结
- js中关于数据类型的转换
- C语言-数据类型
- 在C语言中,double、long、unsigned、int、char类型数据所占字节数
- C语言数据类型总结