0
Output of C++ code
Can someone explain why a=1 in the following code? How does a become true? #include <iostream> using namespace std; int main() { int a=7, b=1; if (a=8 || b==5) cout << a; return 0; }
5 odpowiedzi
+ 2
Go inside the if
a = 8 || (b==5)
=> a = (8 || 1) // because b==5 is true
=> a = 1 // because 8 || 1 is true, they are constant
This happens because of preference of operators.
https://www.sololearn.com/discuss/2386968/?ref=app
+ 1
Ah that makes sense, thanks Li-Gle
0
Actually this depends on ide may be some Compiler will give you error
0
A.S. you mean stricter ones like IBM?
0
In if condition u can write conditions only if u will assign any values it will give u warnings if u will write like this
If(8||5 ) this will work bez its non zero number and non zero will give true result 5||4 this will be true but in your case u assigning values to a variable thats why it will give warnings may be depend on ide u can try in different 3-4 compilers