1 /* $NetBSD: d_c99_bool.c,v 1.4 2021/01/10 12:46:38 rillig Exp $ */ 2 # 3 "d_bool.c" 3 4 /* 5 * C99 6.3.1.2 says: "When any scalar value is converted to _Bool, the result 6 * is 0 if the value compares equal to 0; otherwise the result is 1." 7 * 8 * This is different from the other integer types, which get truncated or 9 * invoke undefined behavior. 10 */ 11 12 /* Below, the wrong assertions produce warning 20. */ 13 14 int int_0_converts_to_false[(_Bool)0 ? -1 : 1]; 15 int int_0_converts_to_true_[(_Bool)0 ? 1 : -1]; 16 17 int int_1_converts_to_false[(_Bool)1 ? -1 : 1]; 18 int int_1_converts_to_true_[(_Bool)1 ? 1 : -1]; 19 20 int int_2_converts_to_false[(_Bool)2 ? -1 : 1]; 21 int int_2_converts_to_true_[(_Bool)2 ? 1 : -1]; 22 23 int int_256_converts_to_false[(_Bool)256 ? -1 : 1]; 24 int int_256_converts_to_true_[(_Bool)256 ? 1 : -1]; 25 26 int null_pointer_converts_to_false[(_Bool)(void *)0 ? -1 : 1]; 27 int null_pointer_converts_to_true_[(_Bool)(void *)0 ? 1 : -1]; 28 29 int nonnull_pointer_converts_to_false[(_Bool)"not null" ? -1 : 1]; 30 int nonnull_pointer_converts_to_true_[(_Bool)"not null" ? 1 : -1]; 31 32 int double_minus_1_0_converts_to_false[(_Bool)-1.0 ? -1 : 1]; 33 int double_minus_1_0_converts_to_true_[(_Bool)-1.0 ? 1 : -1]; 34 35 int double_minus_0_5_converts_to_false[(_Bool)-0.5 ? -1 : 1]; 36 int double_minus_0_5_converts_to_true_[(_Bool)-0.5 ? 1 : -1]; 37 38 int double_minus_0_0_converts_to_false[(_Bool)-0.0 ? -1 : 1]; 39 int double_minus_0_0_converts_to_true_[(_Bool)-0.0 ? 1 : -1]; 40 41 int double_0_0_converts_to_false[(_Bool)0.0 ? -1 : 1]; 42 int double_0_0_converts_to_true_[(_Bool)0.0 ? 1 : -1]; 43 44 /* The C99 rationale explains in 6.3.1.2 why (_Bool)0.5 is true. */ 45 int double_0_5_converts_to_false[(_Bool)0.5 ? -1 : 1]; 46 int double_0_5_converts_to_true_[(_Bool)0.5 ? 1 : -1]; 47 48 int double_1_0_converts_to_false[(_Bool)1.0 ? -1 : 1]; 49 int double_1_0_converts_to_true_[(_Bool)1.0 ? 1 : -1]; 50 51 _Bool 52 bool_to_bool(_Bool b) 53 { 54 return b; 55 } 56 57 _Bool 58 char_to_bool(char c) 59 { 60 return c; 61 } 62 63 _Bool 64 int_to_bool(int i) 65 { 66 return i; 67 } 68 69 _Bool 70 double_to_bool(double d) 71 { 72 return d; 73 } 74 75 enum color { 76 RED 77 }; 78 79 _Bool 80 enum_to_bool(enum color e) 81 { 82 return e; 83 } 84 85 _Bool 86 pointer_to_bool(const char *p) 87 { 88 return p; 89 } 90 91 _Bool 92 function_pointer_to_bool(void (*f)(void)) 93 { 94 return f; 95 } 96 97 _Bool 98 complex_to_bool(double _Complex c) 99 { 100 return c; 101 } 102