An expression-statement like
printf("x=%d\n", x);
is similar to
(void) printf("x=%d\n", x);
the value given by printf is computed (number of characters successfully output, or -1 on error), then discarded and ignored.
BTW, it is the same for an assignment like x=5;
: the value (5) of that assignment expression x=5
is discarded and ignored; of course its side-effect is done, of changing the value in the location for variable x
. If you coded printf("x=%d\n", (x=5))
the value of that assignment would be used and passed to printf
.
An optimizing compiler might (or not) generate slightly different machine code (see as-if rule, at least for C++). But the C11 language specification (read n1570) defines in English wording the observable behavior of your program, i.e. its semantics.
Notice that GCC (and some other compilers, e.g. Clang), provides, as an extension, the ability to declare a function with the warn_unused_result
function attribute. You'll then get a warning if the result of that function is not explicitly used.
Notice that some programming languages (Ocaml, Scheme, Haskell, ....) don't have statements, and everything there is an expression, some of them having a side-effect.