The Output of Cout << 1 && 0;

The output of cout 1 && 0;

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.

Logical operations in cout

The expression cout << i&&j is equivalent to (cout << i) && j. Both operands are evaluated and converted to bool. The statement as a whole has no effect, but the evaluation of the subexpression cout << i has the usual side effects, of course, namely writing something to the standard output.

The && operator is indeed short-circuited and j is only evaluated if cout << i evaluates as true. This condition is equivalent to cout.good(), which is usually the case (unless you somehow managed to close your standard output).

The result of int c=0; coutc++c;

c++ is both an increment and an assignment. When the assignment occurs (before or after other code on that line) is left up to the discretion of the compiler. It can occur after the cout << or before.

This can be found in the C99 standard
http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf

You can find it on page 28 in the pdf or section 5.1.2.3

the actual increment of p can occur at any time between the previous sequence point and the next sequence point

Since someone asked for the C++ standard (as this is a C++ question) it can be found in section 1.9.15 page 10 (or 24 in pdf format)

evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced

It also includes the following code block:

i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // i becomes 9
i = i++ + 1; // the behavior is undefined

I feel that the C99 standard's explanation is clearer, but it is true in both languages.

Question on while (true), while (cin x && x), while (cin x, x)

The >> operator, by convention, when applied to an istream object should return the istream object itself. So the return value of cin >> x is the cin object itself. When used as a Boolean, a stream object returns truthy if there are no errors in input/output.

while (cin >> x && x)

This says "while the loop successfully reads a value without errors and that value is not zero".

The comma operator is one of the more obscure pieces of C++ syntax. a , b evaluates a and then b and returns the result of b, discarding the result of a (this is assuming the comma doesn't do something else; for instance, in a function argument list the comma is part of that syntax, not its own operator). So cin >> x, x says "do the cin part, ignore the return value of that, and then check x".

while (cin >> x, x)

This says "get input and run as long as the input is nonzero". Crucially, errors in the input process are ignored here.


Minor note: Technically, programmers can overload the comma operator to call an arbitrary function. So if a programmer (whose mental health we might call into question) wrote a function like

void operator,(const istream& in, int x) {
// Something really stupid ...
}

Then that function would get called by cin >> x, x. But every C++ style guide I've ever seen recommends against ever overloading the comma operator, so this is pathological at best.

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

What is the output, is it the same on all systems?

The output is as you describe: the second of the two values. It's well defined.

Why is that statement possible?

Because the comma operator allows you to evaluate more than one thing in a single expression.

is there documentation on it?

It's documented in the C++ standard, [expr.comma]. In C++11, that's 5.18.

To summarise, the comma operator:

  • evaluates the expression before the comma;
  • discards that value;
  • evaluates the expression after the comma;
  • yields that value as the overall expression value.

You can see that from your output: in each case, the output is the value after the comma.

It's completely pointless in this case; but is useful if the first expression has side-effects which you want to sequence before the second, in a situation that only allows a single expression. For example:

for (int i = 0, j = n; i < j; ++i, --j)

The operator allows the final expression to do two things, even though you can only put one expression there.

Printing different values for ++i||j++&&++k with printf vs cout

According to C++ Operator Precedence - cppreference.com, the << operator has higher precedence than || operator. Therefore, the statement

cout<<++i||j++&&++k;

means

(cout<<++i) || j++&&++k;

Therefore, the value of i after the increment is printed.

In the other hand, the value of ++i||j++ && ++k will be printed by

printf("%d", ++i||j++ && ++k);

Also note that the execution of cout<<++i||j++&&++k; may affect the result of printf("%d", ++i||j++ && ++k); because the ++ operator has side effects and the values of the variables aren't reset between the statements.



Related Topics



Leave a reply



Submit