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

  免費注冊 查看新帖 |

Chinaunix

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

關(guān)于i++ 和 ++i [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-10-12 23:48 |只看該作者 |倒序瀏覽
說實話,我一直都不是很清楚對于i++ 這種,具體是什么時候才實行自增。

因為我不清楚表達(dá)式的嚴(yán)格定義。。。

對于語句:
int i = 1;
int j = i++;

地球人都知道 j 等于 1,但是 i 的自增是什么時候發(fā)生的呢? 如果說 i 在表達(dá)式求值 之后才自增,也就是說:
先求表達(dá)式 i++ 的值,然后自增 i,并把表達(dá)式的值賦給 j 。毫無疑問,這可以行得通,但是對于下面的語句:

int i = 1;
int j = (i++) + (i++);// 不要說我寫了爛代碼-_-,只是用來說明問題。。。。

如果按照上面的思路,先求表達(dá)式i++的值,為1,然后遞增i,再求另一個表達(dá)式i++的值,為2,自增i,再相加兩個表達(dá)式,最后的結(jié)果是j =  
1 + 2 = 3;但GCC實際上會搞成j = 2,它是這樣做的,求一個表達(dá)式 i++ 的值,為1,再求另一個表達(dá)式i++的值,也為1,得出j = 1+1 = 2,最后在自增 i 兩次。這里我們可以把 (i++) + (i++) 看成一個大的表達(dá)式,這樣的話,”i 在表達(dá)式求值 之后才自增“ 這個結(jié)論又可以成立了,也可以看做“i 在一條語句執(zhí)行完之后才自增”,同樣也是成立的。

如果是
int j = (i++) + (i++) + (i++);

那么究竟是作為3個“i++”表達(dá)式來考慮,還是作為“(i++) + (i++) ”和“i++"這兩個表達(dá)式來考慮,或者作為”(i++) + (i++) + (i++)“這一個大的表達(dá)式來考慮,或者直接作為一條語句來考慮。。。。呃,順便說句,gcc下結(jié)果為1+1+1 = 3,是作為最大的那個表達(dá)式來考慮的,也可以是作為一條語句來考慮的。

雖然這看上去像是編譯器自己的事,但是還是希望有人能把關(guān)于i++ 和 ++i 在C標(biāo)準(zhǔn)中的定義貼出來看看,我手頭沒有。雖然我們都知道它們的用法,而且都用的很好,但就我個人而言還是希望能徹底弄清楚,大概我精力過旺。。。另希望有人能明確告知表達(dá)式的嚴(yán)格定義,編譯原理課還沒開始上。。。

論壇徽章:
0
2 [報告]
發(fā)表于 2008-10-12 23:59 |只看該作者
原帖由 kiffa 于 2008-10-12 23:48 發(fā)表
說實話,我一直都不是很清楚對于i++ 這種,具體是什么時候才實行自增。

因為我不清楚表達(dá)式的嚴(yán)格定義。。。

對于語句:
int i = 1;
int j = i++;

地球人都知道 j 等于 1,但是 i 的自增是什么時候 ...

6.5.2.4  Postfix increment and decrement operators
Constraints
1      The  operand  of  the  postfix  increment  or  decrement  operator  shall  have  qualified  or
unqualified real or pointer type and shall be a modifiable lvalue.
Semantics
2      The  result  of  the  postfix ++ operator  is  the  value  of  the  operand.  After  the  result  is
obtained, the value of the operand is incremented.  (That is, the value 1 of the appropriate
type is added to it.)  See the discussions of additive operators and compound assignment
for  information  on  constraints,  types,  and  conversions  and  the  effects  of  operations  on
pointers.  The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.
3      The postfix -- operator is analogous to the postfix ++ operator, except that the value of
the operand is decremented (that is, the value 1 of the appropriate type is subtracted from
it).

其他的 標(biāo)準(zhǔn)未定義

論壇徽章:
0
3 [報告]
發(fā)表于 2008-10-13 00:09 |只看該作者
首先感謝一下樓上的熱心。

The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.

看到了序列點,不過我忘記具體定義了,我先去查查看。另外麻煩把++i 的也貼出來吧。

論壇徽章:
0
4 [報告]
發(fā)表于 2008-10-13 00:23 |只看該作者
原帖由 kiffa 于 2008-10-13 00:09 發(fā)表
首先感謝一下樓上的熱心。

The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.

看到了序列點,不過我忘記具體定義了,我先去 ...

6.5.3.1  Prefix increment and decrement operators
Constraints
1      The  operand  of  the  prefix  increment  or  decrement  operator  shall  have  qualified  or
unqualified real or pointer type and shall be a modifiable lvalue.
Semantics
2      The value of the operand of the prefix ++ operator is incremented.  The result is the new
value of the operand after incrementation.  The expression ++Eis equivalent to (E+=1).
See the discussions  of  additive  operators and compound assignment for information on
constraints, types, side effects, and conversions and the effects of operations on pointers.
3      The prefix --operator is analogous to the prefix ++operator, except that the value of the
operand is decremented.
Forward references:  additive operators (6.5.6), compound assignment (6.5.16.2).

論壇徽章:
0
5 [報告]
發(fā)表于 2008-10-13 08:55 |只看該作者
blizzard213 兄弄的是不是C標(biāo)準(zhǔn)啊?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(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