Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the output of "cout << (a, b)" and why?

Tags:

c++

comma

It is possible to combine boolean expressions with a comma separator. I have seen it in a code and i am not sure what this resolves to. I wrote some sample code.

int BoolStatement(void)
{
    using std::cout;
    using std::endl;

    cout << "(0, 0) => " << (0, 0) << endl;
    cout << "(0, 1) => " << (0, 1) << endl;
    cout << "(1, 0) => " << (1, 0) << endl;
    cout << "(1, 1) => " << (1, 1) << endl;
    cout << "(0, 0) => " << (0, 0) << endl;
    cout << "(0, 3) => " << (0, 3) << endl;
    cout << "(5, 0) => " << (5, 0) << endl;
    cout << "(7, 1) => " << (7, 1) << endl;

    cout << endl;
    return 0;
}

The output of this is:

(0, 0) => 0
(0, 1) => 1
(1, 0) => 0
(1, 1) => 1
(0, 0) => 0
(0, 3) => 3
(5, 0) => 0
(7, 1) => 1

I am not sure if that is only true for my system and if this call is actually the same as a boolean combination of statements.

What is the output, is it the same on all systems? Why is that statement possible and is there documentation on it?

like image 677
Johannes Avatar asked Feb 04 '15 16:02

Johannes


People also ask

Why << is used with cout?

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 data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

What does cout << mean in C++?

The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output".

Why does << mean in C++?

<< have two meaning one is left shift operator used in most of programming language which shift the bit left in specified number of time. and other << is use C++ for output insertion operator with cout object this mechanism called operator overloading in C++.

What is the output of below program int main (){ int a 10 cout << A ++; return 0?

From the instruction the output put of the program is 10. Hence option 'a' is correct.


1 Answers

The comma operator returns the righthand side.

Wikipedia:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

like image 165
djechlin Avatar answered Sep 30 '22 05:09

djechlin