亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 請(qǐng)教整型比較時(shí)候的類型提升問題 [打印本頁]

作者: demilich    時(shí)間: 2014-10-12 18:47
標(biāo)題: 請(qǐng)教整型比較時(shí)候的類型提升問題
代碼如下,請(qǐng)教為何short和int在比較時(shí)候類型提升的結(jié)果不一致 ....
==========
user@user:~/Documents/CStudy$ cat typeUp.c
#include <stdio.h>

int main()
{
    short a1 = -1;
    unsigned short a2 = 1;
    if(a1 < a2)
        printf("-1 < 1\n");
    else
        printf("-1 > 1\n");

    int b1 = -1;
    unsigned int b2 = 1;
    if(b1 < b2)
        printf("-1 < 1\n");
    else
        printf("-1 > 1\n");


    return 0;
}
user@user:~/Documents/CStudy$ gcc typeUp.c -o typeUp
user@user:~/Documents/CStudy$ ./typeUp
-1 < 1
-1 > 1

作者: hellioncu    時(shí)間: 2014-10-13 08:16
大概是第一個(gè)都提升到int,第二個(gè)都提升到unsigned int吧
作者: bruceteen    時(shí)間: 2014-10-13 08:22
第一個(gè),都提升到 int,即 if( -1 < 1 )
第二個(gè),都提升到 unsigned int,即 if( -1u < 1u )
作者: 爻易    時(shí)間: 2014-10-13 09:12
為什么提升后的類型不同,是因?yàn)榘葱杼嵘?br />
第1例提升到int就足以裝下signed/unsigned short,所以提升到int就夠了,需擴(kuò)展數(shù)位,使用有帶符號(hào)比較指令。

第2例由于有unsigned int 的存在,int就需要進(jìn)一步提升到 unsigned int。但因?yàn)閿?shù)據(jù)位數(shù)不變,并不會(huì)實(shí)際擴(kuò)展,僅表現(xiàn)在比較指令改成無符號(hào)比較。
作者: 井蛙夏蟲    時(shí)間: 2014-10-13 10:58
c語言標(biāo)準(zhǔn)中有兩個(gè)概念:integer promotion和arithmetic conversion,你可以google一下
作者: socay2    時(shí)間: 2014-10-14 00:38
http://www.idryman.org/blog/2012/11/21/integer-promotion/
這里講得好詳細(xì)
c99 規(guī)定: 所有長(zhǎng)度小于 int  的 都轉(zhuǎn)成 int 比較
所以 unsigned char x = 0xFF ;    char y = 0xFF;
x != y
作者: mymtom_cu    時(shí)間: 2014-10-16 10:19
按照標(biāo)準(zhǔn)
b1 < b2
會(huì)轉(zhuǎn)成unsigned int后進(jìn)行比較

3.2.1.5 Usual arithmetic conversions

   Many binary operators that expect operands of arithmetic type cause
conversions and yield result types in a similar way.  The purpose is
to yield a common type, which is also the type of the result.  This
pattern is called the usual arithmetic conversions: First, if either
operand has type long double, the other operand is converted to long
double .  Otherwise, if either operand has type double, the other
operand is converted to double.  Otherwise, if either operand has
type float, the other operand is converted to float.  Otherwise, the
integral promotions are performed on both operands.  Then the
following rules are applied: If either operand has type unsigned long
int, the other operand is converted to unsigned long int.
Otherwise, if one operand has type long int and the other has type
unsigned int, if a long int can represent all values of an unsigned
int, the operand of type unsigned int is converted to long int ; if a
long int cannot represent all the values of an unsigned int, both
operands are converted to unsigned long int.  Otherwise, if either
operand has type long int, the other operand is converted to long int.
Otherwise, if either operand has type unsigned int, the other
operand is converted to unsigned int.
  Otherwise, both operands have
type int.





歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2