- 论坛徽章:
- 0
|
本帖最后由 churchmice 于 2010-06-14 13:58 编辑
回复 1# glq2000
char和short都有Integer promotion,都会被promote成int,所以是无法判断unsigned short,unsigned char的符号性的,看下面的代码就明白了
Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to an int; otherwise, it is converted to an unsigned int. Integer promotions are applied as part of the usual arithmetic conversions to certain argument expressions; operands of the unary +, -, and ~ operators, and operands of the shift operators.
- #include <stdio.h>
- #define ISUNSIGNED(x) ( x>=0 && ~x >=0 )
- int main(void){
- signed char a =1;
- unsigned char b =1;
- signed int c = 1;
- unsigned int d =1;
- signed short e =1;
- unsigned short f=1;
- signed long g =1;
- unsigned long h =1;
- if ( ISUNSIGNED(a) ){
- printf("a is unsigned\n");
- }
- if ( ISUNSIGNED(b) ){
- printf("b is unsigned\n");
- }
- if ( ISUNSIGNED(c) ){
- printf("c is unsigned\n");
- }
- if ( ISUNSIGNED(d) ){
- printf("d is unsigned\n");
- }
- if ( ISUNSIGNED(e) ){
- printf("e is unsigned\n");
- }
- if ( ISUNSIGNED(f) ){
- printf("f is unsigned\n");
- }
- if ( ISUNSIGNED(g) ){
- printf("g is unsigned\n");
- }
- if ( ISUNSIGNED(h) ){
- printf("h is unsigned\n");
- }
- return 0;
- }
复制代码 编译的时候有提示
sign.c:15: warning: comparison is always true due to limited range of data type
sign.c:27: warning: comparison is always true due to limited range of data type
运行的结果
xxx:~> ./sign
d is unsigned
h is unsigned
可见short和char类型是无法判断出来的
汇编代码里面也可以看到有两句
movzbl movsbl |
|