Here is my code snippet:
int a;
if(a=8)
cout<<"Its right";
else
cout<<"Not at all";
getch();
return(0);
I’m getting the output Its right while I’ve not give any input there its just a assignment to the a=8.
**Borland Compiler** gives the following 2 warnings and executes the code.
1: Possible incorrect assignment (a=8)
2: ‘a’ is assigned a value that is never used.
When you write:
You’re assigning 8 to the variable a, and returning a, so you’re effectively writing:
This is non-zero, and hence treated as “true”.
I (and the compiler) suspect you intended to write (note
==instead of=):This is why you get the warnings. (Note that, in this case,
ais still uninitialized, so the behavior is undefined. You will still get a warning, and may still return true…)