Reputation:
According to C89 standard the following code is legit:
A_Function_that_returns_true();
But the following is false (Since the returned value is not used-This can be solved simply by declaring a bool variable)
(5>3) && (7>6);
But what’s the difference between both examples? In both I didn’t use the returned value so why one is true and one is false that doesn’t sound right
Upvotes: 0
Views: 63
Reputation: 47923
It is not a violation of the C Standard to discard the result of an expression. Neither of the two expression statements in the question violates any rule or constraint.
Whether a compiler chooses to emit helpful warnings about various things is a quality-of-implementation issue, not governed by the Standard.
Calling a function but then not using its return value usually isn't a problem, because many (though certainly not all) functions do something other than just compute and return a value. (The formal term for this is that many functions have side effects.)
The expression
(5>3) && (7>6);
is, on the other hand, perfectly useless. It's clear that it doesn't do anything (it has no side effects), so the compiler is well advised to warn against it.
If a compiler writer decided to also helpfully warn about function return values that were ignored, there would be an immediate problem: virtually every C program contains multiple calls to printf
, for which the return value is not checked. So there would be so many false positives that the warning would be useless.
Upvotes: 2