防爆pressions" J = ++(I | I)且j = + +(I和ⅰ);应该是一个左值错误? [英] Expressions "j = ++(i | i); and j = ++(i & i); should be a lvalue error?

查看:218
本文介绍了防爆pressions" J = ++(I | I)且j = + +(I和ⅰ);应该是一个左值错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在我的下面code:

I was expecting that in my following code:

#include<stdio.h> 
int main(){
    int i = 10; 
    int j = 10;

    j = ++(i | i);
    printf("%d %d\n", j, i);

    j = ++(i & i);
    printf("%d %d\n", j, i);

    return 1;
}

EX pressions J = ++(I | I); J = ++(I和我); 将产生左值误差如下:

expressions j = ++(i | i); and j = ++(i & i); will produce lvalue errors as below:

x.c: In function ‘main’:
x.c:6: error: lvalue required as increment operand
x.c:9: error: lvalue required as increment operand   

但我惊讶的是上面code编译成功,如下:

But I surprised that above code compiled successfully, as below:

~$ gcc x.c -Wall
~$ ./a.out 
11 11
12 12   

上述code工作正确。

虽然其他运营商产生错误(据我所知)。 //$c$cpad.org/:(检查其他的 J = ++(I ^ I)中,即使位运算符XOR原因g08e8GkH相对=nofollow>运营商产生在编译时左值误差)。

While other operators produce error (as I understand). Even bitwise operator XOR causes of an error j = ++(i ^ i); (check other operators produce an lvalue error at compilation time).

这是什么原因?这是未指定的或不确定的?或按位或和经营者有什么不同?

What is the reason? Is this is unspecified or undefined ? or bitwise OR AND operators are different?

编译器版本:

gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

<子>但我相信编译器版本不应该理性非均匀行为。如果 ^ 未编译那么 | &安培; 也没有。否则应该为所有工作

But I believe compiler version shouldn't reason for non-uniform behavior. If ^ not compiled then | and & also not. otherwise should work for all

它不是这个编译器C99模式下的错误: GCC x.c -Wall -std = C99

Its not an error with this compiler in c99 mode: gcc x.c -Wall -std=c99.

推荐答案

您是正确的,它不应该编译,并在大多数编译器,它不编译。结果
<罢工>的(请准确指定的编译器/版本不是给你一个编译器错误)

You are right that it should not compile, and on most compilers, it does not compile.
(Please specify exactly which compiler/version is NOT giving you a compiler error)

我只能假设,编译器知道身份的(I | I)== I (I和我)==我并使用这些身份优化掉除权pression,正要离开变量 I

I can only hypothesize that the compiler knows the identities that (i | i) == i and (i & i) == i and is using those identities to optimize away the expression, just leaving behind the variable i.

这只是一个猜测,但它使一个很大的意义对我来说。

This is just a guess, but it makes a lot of sense to me.

这篇关于防爆pressions&QUOT; J = ++(I | I)且j = + +(I和ⅰ);应该是一个左值错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆