Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The output of cout << 1 && 0; [closed]

Tags:

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; } 
like image 840
Sabz Avatar asked Mar 09 '18 14:03

Sabz


People also ask

What is Cout in C++ with example?

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.

What is the prototype of Cout in C++?

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.

What is Cout in iostream?

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".

What is the syntax of the cout object?

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".


1 Answers

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.

like image 57
Smit Ycyken Avatar answered Sep 20 '22 18:09

Smit Ycyken