C Programming

How to Use True and False Values in C

True and false values are essential pieces of programming in C and they are generally declared as either 0 or 1 and can be used conditionally to create branching logic. Mastering their use is key to developing effective and efficient programs in the C language.

Learn how to utilize true and false values in C by implementing the guidelines in this tutorial.

Use of True and False in C

The C users can use true and false in:

1: Using of True and False in C with if Statement

The most basic use of true and false is when evaluating an expression in the context of an if statement. The code included within the if statement is performed if a value is evaluated to be true. The if statement’s code is skipped if the value evaluates to false. Let’s follow it with a simple example shown below:

#include <stdbool.h>

#include <stdio.h>

int main() {
    bool flag = false;
    if (flag) {
printf("flag is true");
    }
    else {
printf("flag is false");
    }
    return 0;
}

In the above code, we have given the ‘flag’ variable the bool value of false. And then checked the value in an if condition. If the value is false, we will print flag is false and vice versa.

Output

Text Description automatically generated

2: Using of True and False in C with Loops

Loops can also use Boolean values to control the flow of the loop. For example, a while loop would execute its body of code if the condition provided is true. Like the if-else statement, the condition in a while loop can be a Boolean expression which evaluates to either true or false. This same concept applies to other types of loops like for and do-while.

This can be seen in the example:

#include <stdbool.h>

#include <stdio.h>

int main() {
    bool flag = true;
    int a = 0;
    while (flag) {
printf("a is %d\n", a);
        a++;
        if (a > 5) {
        flag = false;
        }
    }
    return 0;
}

In the above example, the while loop will run and print values from 0 to 5, and when the loop reaches 5, the flag will turn to false, which in turn will not print more values of a.

Output

3: Using of True and False in C with Function Return Type

Finally, Boolean values can be used as return types for functions. A Boolean return type allows the function to return either true or false, depending on the logic within the function and the values passed in as parameters. Consider this example of a function that checks if a number is odd:

#include <stdbool.h>

#include <stdio.h>

bool is_even(int number) {
    if (number % 2 == 0) {
        return true;
    }
    else {
        return false;
    }
    }
    int main() {
    int num = 7;
    if (is_even(num)) {
printf("%d is even\n", num);
    }
    else {
printf("%d is odd\n", num);
    }
    return 0;
}

In this case, the function is checking the remainder of x divided by 2. If it is equal to zero, then the number is even, and the function returns false. Otherwise, the number is odd, and the function returns true.

Output

Conclusion

In C, every expression, whether its type is numeric, or pointer, is consider true if the value of that expression is non-zero (i.e., has any bits turned on). The users can choose any true and false method according to the choice. Overall, Boolean values are quite useful when programming in C language. They allow the programmer to make decisions in the form of if-else statements and create loops that iterate based on a Boolean comparison. Boolean values can also be used as function return types, allowing the function to return true or false based on the logic within that function.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.