3 ms·
Personally I think the problem is right here: int x = 0, y = 1, z = 0; int s = x & (y == z); // 0 Other languages like Go dont allow this: packag
by svnpenn 7y ago
Personally I think the problem is right here:
int x = 0, y = 1, z = 0;
int s = x & (y == z); // 0
Other languages like Go dont allow this:
package main
func main() {
n1, n2, n3 := 10, 11, 12
n4 := n1 & (n2 == n3)
println(n4)
}
Result:
invalid operation: n1 & (n2 == n3) (mismatched types int and bool)
- dahfizz 7y agoTFA directly addresses this. The problem you demonstrate is that C does not have a bool type, and the == operator instead returns an int.
- binarycrusader 7y agoAh, but C99 has a bool type: _Bool. Also available as 'bool' via stdbool.h: https://en.cppreference.com/w/c/keyword/_Bool https://en.cppreference.com/w/c/keyword/_Bool But I otherwise agree with you.
- codebje 7y agoIt also has silent type coercion, so it will happily promote your _Bool to an int, neatly voiding any benefits the type might have had to offer.