int a=-1; cout<<(a++&&1); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int a=-1; cout<<(a++&&1);

what is the output of this code? I need an explanation about this?

19th Jul 2018, 3:38 AM
Jagadesh Ramesh
Jagadesh Ramesh - avatar
7 Answers
+ 4
Jagadesh Ramesh, the other side is not zero. There is a postfic ++, this code increments a after calculating the expression in brackets. So it is actually (-1&&1), which is 1 (true), because all ints, except 0 are true. And if we change this code to(++a&&1) this will be 0, because prefix ++ will first increment a, and then use the incremented value to calculate the expression.
19th Jul 2018, 4:30 AM
Дмитро Іванов
Дмитро Іванов - avatar
+ 3
Jagadesh Ramesh in c++ for booleans 1 ( or any number other than 0) is true 0 is false https://code.sololearn.com/cBhiuiXQRo53/?ref=app
19th Jul 2018, 4:05 AM
Manual
Manual - avatar
+ 2
Jagadesh Ramesh well && operators check if true or false. unless there is one or two false values it will be true.
19th Jul 2018, 3:55 AM
Manual
Manual - avatar
+ 2
thank you so much guys. I got it.
19th Jul 2018, 4:19 AM
Jagadesh Ramesh
Jagadesh Ramesh - avatar
+ 1
Output: 1
19th Jul 2018, 3:40 AM
Satyam
+ 1
but using this && operator both the conditions should be satisfied right? one side is 1 that is okay. but other side may come zero. so how it returns 1.
19th Jul 2018, 4:03 AM
Jagadesh Ramesh
Jagadesh Ramesh - avatar
0
can you please explain how it works
19th Jul 2018, 3:41 AM
Jagadesh Ramesh
Jagadesh Ramesh - avatar