5 ms·
parameter reg is int, return value is bool.
by testrun 5y ago
parameter reg is int, return value is bool.
- NavinF 5y ago“reg == LTC_REG_CONFIG” is a bool. There’s no conversion going on here.
- zargon 5y agoIn the C language, the result of the equality operator is an int, not a bool.
- carapace 5y agoAh, whale oil beef hooked, you're right. > The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true. https://port70.net/~nsz/c/c11/n1570.html#6.5.9p3 https://port70.net/~nsz/c/c11/n1570.html#6.5.9p3 Thanks for clearing that up. (Although I feel a bit foolish now.)
- xorcist 5y agoIt's not really an explanation though. It's how the language should work. That ternary operator "?" is just an if-statement which should have the same "problem" with an int argument. It is a broken compiler that would complain about an if statement but an identical ?-statement. If the int nature of equals-to was a problem, you'd have to use limit its use to switch statements.
- zargon 5y agoThere's no problem with the "int argument". (Not sure if you mean the function argument ("reg") or the operand of the conditional operator, but either way not a problem.) The conflict is between the declared return type of the function and the type of the returned expression.
- xorcist 5y agoRight, but the same could be said for the ternary operator.
- zargon 5y agoI don't follow. What could be said about it?
- xorcist 5y agoNot until quoting the above message did I realize I misread it this whole time. There is nothing to understand. Sorry for being a bit dense.
- vlovich123 5y agoThe return type of the ternary is bool because of the explicit true/false.
- LeifCarrotson 5y agoNo, it could not. The controlling expression of any kind of conditional statement in C can be an integer. The return type of a function which returns boolean, though, should be boolean. The strict requirement in the standard is just a 'scalar type': every conditional expression in C, even `if (true)`, technically evaluates on unequal comparison to zero. See section 6.8.4.1 "The if statement" of the standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf This applies to ternary operators, if statements, do/while loops, for loop termination conditions, etc. However, when defining a function that returns boolean, you've asked the compiler to make your program more strict than just returning an integer that's zero or nonzero, you're not allowed to return anything but 'true' or 'false' - no, you're not allowed to pack in error codes after the fact!
- zargon 5y agoIt's not possible to remember all the details of C at one time. I feel like my low-level understanding shifts over different areas over time depending on the type of work I have been doing. Then there's living in C++ land, which is 1000 times worse.