I\'m wondering what the following statement will print in C?
printf(\"hello\\n\") || (printf(\"goodbye\\n\") || printf(\"world\\n\"));
I\'m usu
First, cout
is a C++ invention, never made it back to C, and never will.
Next, printf
returns the number of printed characters, so the first call returns non-zero.
As ||
is short-circuiting boolean-or, none of the following printf
-calls will be done.
(|
is bitwise-or, and thus not short-circuiting. Added because you are talking about single pipes and @Leeor linked such a question.)
Endresult: hello\n
is printed: 5 characters+newline (will be translated, as stdin
is text-mode (identity-transformation on Unixoids)).
7.21.6.3 The printf function
Synopsis
#include
int printf(const char * restrict format, ...); Description
2 Theprintf
function is equivalent tofprintf
with the argumentstdout
interposed before the arguments toprintf
.
Returns
3 Theprintf
function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.
6.5.12 Bitwise inclusive OR operator
Synopsis
[...]
Constraints
2 Each of the operands shall have integer type.
Semantics
3 The usual arithmetic conversions are performed on the operands.
4 The result of the|
operator is the bitwise inclusive OR of the operands (that is, each bit in the result is set if and only if at least one of the corresponding bits in the converted operands is set).
6.5.14 Logical OR operator
Synopsis
[...]
Constraints
2 Each of the operands shall have scalar type.
Semantics
3 The||
operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has typeint
.
4 Unlike the bitwise|
operator, the||
operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.