4 ms·
Can you explain why?
by thrwaway69 7y ago
Can you explain why?
- Tervis 7y agoOn C language an expression which evaluates to any non-zero value is considered as True. So for example this kind of statement would not likely behave as intended: if (TRUE == expression) { ... }
- Koshkin 7y agoI have refused to hire people who did this. (FALSE, too, even though it is safer.)
- shakna 7y agoTo emphasise: This is about Bournegol. Nothing to do with this post. > Can you explain why? Because it isn't how most C libraries expect true/false to be defined. stdbool in C99 standardized things a bit, but before then what was generally accepted was: > true is 1 > false is !true (Often 0 in practice). Which means that any trivial: if(true) { ... } Won't work under Bournegol. Instead you _need_ to compare when doing an if statement. So the C-programmer is easily tripped up. False will work as expected, but True won't... All the time. There may be times it does work. Leaving the programmer throwing their hands in the air.
- thrwaway69 7y agoAh. Thanks for the explanation.
- kaetemi 7y agoMost cases I've seen accept anything as true as long as the LSB is 1, and quite strictly 0 as false. Everything else is up in the air. The value -1 is definitely true. :)
- ufo 7y agoOne could say that -1 is the truest true, because its two's-complement representation has the most 1s.
- kaetemi 7y agoTrue.
- shakna 7y ago> The value -1 is definitely true Except when being used as a comparison to any of the std utilities.
- syockit 7y agoAs far as I know, std library specifically uses the word "nonzero" in the documentation to denote non-false value. Therefore you should be using !, &&, || for any boolean ops, but never == nor !=
- wbl 7y agoAny nonzero integer is considered true in that expression, so if(TRUE) still works.
- Hello71 7y agothis is utter nonsense. in C, since before C89, all non-zero values are considered true in conditional contexts (if, for, while, ?:). http://port70.net/~nsz/c/c89/c89-draft.html#3.6.4.1 http://port70.net/~nsz/c/c89/c89-draft.html#3.6.4.1. false must therefore be exactly zero. !1 is also always 0, as it must be. http://port70.net/~nsz/c/c89/c89-draft.html#3.3.3.3 http://port70.net/~nsz/c/c89/c89-draft.html#3.3.3.3.