I don't understand why the below code prints 1
.
1 && 0
is not the same as true && false
-> false
?
Why doesn't this print 0
?
#include <iostream> using namespace std; int main(){ cout << 1 && 0; return 0; }
cout in C++. The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout.
The prototype of cout as defined in the iostream header file is: The cout object in C++ is an object of class ostream. It is associated with the standard C output stream stdout.
It is defined in the iostream header file. The syntax of the cout object is: var_name is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc. The "c" in cout refers to "character" and "out" means "output".
The syntax of the cout object is: var_name is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc. The "c" in cout refers to "character" and "out" means "output".
It's all about Operator Precedence.
The Overloaded Bitwise Left Shift Operator operator<<(std::basic_ostream)
has a higher priority than the Logical AND Operator &&
.
#include <iostream> int main() { std::cout << (1 && 0); return 0; }
If you are not 146% sure about the priority of an operator, do not hesitate to use brackets. Most modern IDEs will tell you if you don't need to use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With