开发者

Precedence of parentheses between post-fix and pre-fix in C

The C Operator Preference Table notes the higher precedence of ().

Code:

# include <stdio.h>
int main()
{
    int temp=2;
    (temp += 23)++;    //Statement 1
    ++(temp += 23);    //Statement 2
    printf("%d",temp);
    return 0;
}

My question is while parentheses has higher precedence than pre-fix开发者_JS百科 operator in Statement 2 why there's an error. In Statement 1 both has same precedence but order of evaluation is from left to right. Still the same error. Third doubt: operator += has much lower precedence, then why it's causing error.

error: lvalue required as increment operand


An lvalue is a value that some other value can be assigned to (because it is on the left side of the assignment operator). (temp += 23) is a rvalue. Nothing can be assigned to it.


Something else I'd like to add, is that it looks like you're trying to modify a value more than once in an expression. That's undefined behavior according to C99 standard 6.5(2).

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

And footnote 71) shows the example:

This paragraph renders undefined statement expressions such as

i = ++i + 1;

a[i++] = i;

while allowing

i = i + 1;

a[i] = i;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜