开发者

c programming query regarding assignment operator Please specifythe output with reason in windows and linux [closed]

开发者 https://www.devze.com 2023-01-18 05:21 出处:网络
It's difficult to tell what is being asked her开发者_StackOverflowe. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current
It's difficult to tell what is being asked her开发者_StackOverflowe. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
int main()
{
   int x=-1, y=-1;
   if(++x=++y)
     printf("pppppppp");
   else
    printf("cccccccc");
}


In C your code won't compile [you cannot assign to rvalues]

In C++ if(++x=++y) invokes Undefined Behaviour.


You are assigning -1 to x and y. In the comparison, you are incrementing both variables before evaluation, so both x and y are 0. This means x=0. So x is assigned 0 and this is the result of the evaluation. Because this is regarded as false in logic operations, "ccccccccccc" is printed to the screen.

Please note that people don't usually put an assignment into an if/while/ect... and when you see this in code, it is usually an error. The comparison operator is ==.


The code tries to assign a value to something that's not a lvalue.

Pass the code eaxample to a compiler before posting.

0

精彩评论

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