Boolean values

Zen C inherits C's flexible semantics for boolean evaluation, meaning any value equal to 0 is considered false, and any non-zero value is considered true. This applies to integers, floating-point numbers, characters (where '\0' is false), and pointers (where a null pointer is false).

However, unlike older C standards that require including <stdbool.h>, Zen C modernizes this by providing a native bool data type and built-in true and false keywords directly in the language.

fn main() {
    // Native boolean type and keywords
    let is_valid: bool = true;
    let is_empty: bool = false;
    
    println "Native true evaluates to: {is_valid}";
    println "Native false evaluates to: {is_empty}\n";

    // C-style truthiness evaluation
    let zero_int = 0;
    let non_zero_int = 42;
    let zero_float = 0.0;
    
    if zero_int {
        println "0 is true";
    } else {
        println "0 evaluates to false";
    }
    
    if non_zero_int {
        println "42 evaluates to true";
    }
    
    if zero_float {
        println "0.0 is true";
    } else {
        println "0.0 evaluates to false";
    }
}

Output:

Native true evaluates to: true
Native false evaluates to: false

0 evaluates to false
42 evaluates to true
0.0 evaluates to false

Attribution: This is a community solution for the Rosetta Code task Boolean values in Zen C.

This article uses material from the Rosetta Code article Boolean values, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.