Semantics of `printf(“…”) || printf(“…”) || printf(“…”)`

后端 未结 3 1510
太阳男子
太阳男子 2021-01-29 11:39

I\'m wondering what the following statement will print in C?

printf(\"hello\\n\") || (printf(\"goodbye\\n\") || printf(\"world\\n\"));

I\'m usu

相关标签:
3条回答
  • 2021-01-29 12:29

    It will print:

    hello\n (that is, hello and a newline, not the literal "\n".)

    printf returns the number of characters printed to the console. The || is a short-circuiting "or", which means: do the first thing, then if the first thing returned "false", do the next thing. At the end, return back whether any of the things you did returned "true".

    In c, an int is considered "true" if it is any value other than 0, and all three of those printf calls print more than 0 characters, so it will run the first one, which returns (a value logically equivalent to) true, so it will stop execution of that line and go onto the next.

    Of course, there's no reason to ever write code like that... there are sometimes reasons to use short-circuiting boolean operators with functions that have side-effects (like printing to the console), but I can't think of a reason you'd ever need to short-circuit where the functions you were calling were being passed constants and you always knew exactly what result you would get from them.

    Also, yes, as written there's a compile error because of an extra open-parenthesis before your second printf. But ignoring that.

    0 讨论(0)
  • 2021-01-29 12:40

    It prints "hello" only!

    http://www.compileonline.com/compile_c_online.php

    #include <stdio.h>
    #include <string.h>
    
    main()
    {
      printf("hello\n") || (printf("goodbye\n") || printf("world\n"));
    }
    
    0 讨论(0)
  • 2021-01-29 12:42

    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 <stdio.h>
    int printf(const char * restrict format, ...);
    

    Description
    2 The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf.
    Returns
    3 The printf 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 type int.
    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.

    0 讨论(0)
提交回复
热议问题