开发者

Assignments in C

Can you write something like this in开发者_JAVA百科 C with multiple assignment operations?

int a = 0, b = 0, c = 0, d = 0;
(((a = b) = c) = d);

I've read somewhere that C standard states that the result of this won't be lvalue? is this undefined?


You can do

a = b = c = d;

which is the same as

a = (b = (c = d));

As you say, the (sub-)expression (a = b) is not an lvalue and can't be assigned a value.


     a=b=c=d;

is same as

    (a = (b = (c = d))); 

because '=' operator assign right to left..


a=b returns value of b which won't be lvalue, and that's the reason why the value of c can't be assigned to that expression. With parentheses you are changing usual order of assignments.

a = b = c = d;

in this case value of d is assigned to c, then value of c to b, then value of b to a.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜