How to represent the Boolean "true" and "false" in C

2 Answers

0 votes
#include <stdio.h>

// 0 -> false
// non‑zero -> true (commonly 1)
// '\0' -> false

#include <stdbool.h>

int main(void) {
    bool a = true;  
    bool b = false;  

    // printf cannot directly print bool as text,
    // so we convert it manually using a conditional expression.
    printf("a is %s\n", a ? "true" : "false");
    printf("b is %s\n", b ? "true" : "false");

    return 0;
}



/*
run:

a is true
b is false

*/

 



answered 6 days ago by avibootz
edited 6 days ago by avibootz
0 votes
#include <stdio.h>

// 0 -> false
// non‑zero -> true (commonly 1)
// '\0' -> false

typedef int bool;
#define true 1
#define false 0

int main(void) {
    bool a = true;   
    bool b = false;  

    // printf cannot directly print bool as text,
    // so we convert it manually using a conditional expression.
    printf("a is %s\n", a ? "true" : "false");
    printf("b is %s\n", b ? "true" : "false");

    return 0;
}


/*
run:

a is true
b is false

*/

 



answered 6 days ago by avibootz
edited 6 days ago by avibootz
...