Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C Booleans


Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, C has a bool data type, which is known as booleans.

Booleans represent values that are either true or false.


Boolean Variables

In C, the bool type is not a built-in data type, like int or char.

It was introduced in C99, and you must import the following header file to use it:

#include <stdbool.h>

A boolean variable is declared with the bool keyword and can take the values true or false:

bool isProgrammingFun = true;
bool isFishTasty = false;

Before trying to print the boolean variables, you should know that boolean values are returned as integers:

  • 1 (or any other number that is not 0) represents true
  • 0 represents false

Therefore, you must use the %d format specifier to print a boolean value:

Example

// Create boolean variables
bool isProgrammingFun = true;
bool isFishTasty = false;

// Return boolean values
printf("%d", isProgrammingFun);   // Returns 1 (true)
printf("%d", isFishTasty);        // Returns 0 (false)
Try it Yourself »

However, it is more common to return a boolean value by comparing values and variables.


Comparing Values and Variables

Comparing values are useful in programming, because it helps us to find answers and make decisions.

For example, you can use a comparison operator, such as the greater than (>) operator, to compare two values:

Example

printf("%d", 10 > 9);  // Returns 1 (true) because 10 is greater than 9
Try it Yourself »

From the example above, you can see that the return value is a boolean value (1).

You can also compare two variables:

Example

int x = 10;
int y = 9;
printf("%d", x > y);
Try it Yourself »

In the example below, we use the equal to (==) operator to compare different values:

Example

printf("%d", 10 == 10); // Returns 1 (true), because 10 is equal to 10
printf("%d", 10 == 15); // Returns 0 (false), because 10 is not equal to 15
printf("%d", 5 == 55);  // Returns 0 (false) because 5 is not equal to 55
Try it Yourself »

You are not limited to only compare numbers. You can also compare boolean variables, or even special structures, like arrays (which you will learn more about in a later chapter):

Example

bool isHamburgerTasty = true;
bool isPizzaTasty = true;

// Find out if both hamburger and pizza is tasty
printf("%d", isHamburgerTasty == isPizzaTasty);
Try it Yourself »

Remember to include the <stdbool.h> header file when working with bool variables.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.