tags:

views:

41

answers:

3
int main()
{
   int x=-1, y=-1;
   if(++x=++y)
     printf("pppppppp");
   else
    printf("cccccccc");
}
+2  A: 

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

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

Prasoon Saurav
thiscodegivesoutput pppppppp
but wecant assign constant to a constant so it shouldgiveerror
@User : Are you compiling your code as a `*.cpp` file? In C assignment to `++x` is not valid.
Prasoon Saurav
C++ was not tagged, so you can omit C++. The code just dont pass the C compiler.
harper
A: 

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 ==.

Alexander Rafferty
so what isthecorrect output will it givean error or it will print
A: 

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

Pass the code eaxample to a compiler before posting.

harper
this prints but actually it shouldnot because howcan weassign a contant to anotherconstant
@user448302: So why asked you the question?
harper