Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by vzaliva on Friday January 05, 2024 @06:20PM
(#38886431)
"true is a #define for 1" is bad idea. Because when `x` is, say, `2` then `if x` is not the same as `if x==true`.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by BenjiWiebe on Friday January 05, 2024 @07:08PM
(#38886824)
That's just the way C is. If you want to check "truthiness" (as much as you can in C), just do 'if x'.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by trealira on Saturday January 06, 2024 @03:18AM
(#38889579)
Something nice in C is that "if (x)" is always equivalent to "if (x != 0)". NULL is a macro for 0, and so is false. Boolean expressions evaluate to 0 if they don't hold. This isn't true in C++, though.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by ahoka on Saturday January 06, 2024 @04:45AM
(#38890007)
Then what is _Bool, if not a built in boolean type?
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by nicklecompte on Saturday January 06, 2024 @07:17AM
(#38890829)
I genuinely forgot stdbool.h existed, I don't actually write that much C myself.
My point was more around the conditionals being weakly typed around unsigned ints rather than a specific lack of built-ins. A lot of commenters were going into arithmetic mod 2 or philosophical issues, neither of which actually apply here.
Re:Dennis Ritchie on the priorities of && || vs. == etc. (1982)
by kevin_thibedeau on Saturday January 06, 2024 @09:14AM
(#38891590)
_Bool isn't part of stdbool. It's a language keyword. C23 introduces full support for bool with true and false as keywords.
a+b != 0 <=> a!=0 or b!=0
a*b != 0 <=> a!=0 and b!=0
Of course this intuition also reveals the pitfall behind this correspondence! You'd better make sure those are unsigned ints or #defined booleans, so you're not using general C expressions. 1 || -1 is true but 1 + (-1) is false.
Edit: forgot to mention: INT_MAX || 1 is true, but what about INT_MAX + 1 :)