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; }

3rd Apr 2021, 8:35 PM
Edward Finkelstein
Edward Finkelstein - avatar
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
3rd Apr 2021, 8:45 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
Ah that makes sense, thanks Li-Gle
3rd Apr 2021, 10:52 PM
Edward Finkelstein
Edward Finkelstein - avatar
0
Actually this depends on ide may be some Compiler will give you error
4th Apr 2021, 4:14 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
A.S. you mean stricter ones like IBM?
4th Apr 2021, 5:38 AM
Edward Finkelstein
Edward Finkelstein - avatar
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
4th Apr 2021, 6:52 AM
A S Raghuvanshi
A S Raghuvanshi - avatar