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

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2962 | 回復(fù): 6
打印 上一主題 下一主題

[C] 請教整型比較時候的類型提升問題 [復(fù)制鏈接]

論壇徽章:
7
IT運維版塊每日發(fā)帖之星
日期:2015-08-29 06:20:00IT運維版塊每日發(fā)帖之星
日期:2015-08-29 06:20:00平安夜徽章
日期:2015-12-26 00:06:30技術(shù)圖書徽章
日期:2016-02-03 16:35:252016猴年福章徽章
日期:2016-02-18 15:30:34fulanqi
日期:2016-06-17 17:54:25C
日期:2016-10-25 16:08:32
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2014-10-12 18:47 |只看該作者 |倒序瀏覽
代碼如下,請教為何short和int在比較時候類型提升的結(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

論壇徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52雙子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午馬
日期:2013-10-18 21:43:38
2 [報告]
發(fā)表于 2014-10-13 08:16 |只看該作者
大概是第一個都提升到int,第二個都提升到unsigned int吧

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期:2015-07-22 08:09:472015年辭舊歲徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39獅子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技術(shù)圖書徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
3 [報告]
發(fā)表于 2014-10-13 08:22 |只看該作者
第一個,都提升到 int,即 if( -1 < 1 )
第二個,都提升到 unsigned int,即 if( -1u < 1u )

論壇徽章:
6
2015年辭舊歲徽章
日期:2015-03-05 16:13:092015年迎新春徽章
日期:2015-03-05 16:13:092015小元宵徽章
日期:2015-03-06 15:58:1815-16賽季CBA聯(lián)賽之浙江
日期:2016-11-05 14:38:4115-16賽季CBA聯(lián)賽之新疆
日期:2016-11-11 18:38:06
4 [報告]
發(fā)表于 2014-10-13 09:12 |只看該作者
為什么提升后的類型不同,是因為按需提升。

第1例提升到int就足以裝下signed/unsigned short,所以提升到int就夠了,需擴(kuò)展數(shù)位,使用有帶符號比較指令。

第2例由于有unsigned int 的存在,int就需要進(jìn)一步提升到 unsigned int。但因為數(shù)據(jù)位數(shù)不變,并不會實際擴(kuò)展,僅表現(xiàn)在比較指令改成無符號比較。

論壇徽章:
4
白羊座
日期:2013-09-17 21:59:30技術(shù)圖書徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40雙子座
日期:2013-12-17 18:26:39
5 [報告]
發(fā)表于 2014-10-13 10:58 |只看該作者
c語言標(biāo)準(zhǔn)中有兩個概念:integer promotion和arithmetic conversion,你可以google一下

論壇徽章:
1
巨蟹座
日期:2014-03-18 23:44:30
6 [報告]
發(fā)表于 2014-10-14 00:38 |只看該作者
http://www.idryman.org/blog/2012/11/21/integer-promotion/
這里講得好詳細(xì)
c99 規(guī)定: 所有長度小于 int  的 都轉(zhuǎn)成 int 比較
所以 unsigned char x = 0xFF ;    char y = 0xFF;
x != y

論壇徽章:
0
7 [報告]
發(fā)表于 2014-10-16 10:19 |只看該作者
按照標(biāo)準(zhǔn)
b1 < b2
會轉(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.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP