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

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Booleans


Share

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 one of two values: 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 include 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 can use the %d format specifier to print a boolean value:

Example

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

// Print boolean values
printf("%d\n", isProgrammingFun); // Prints 1 (true)
printf("%d", isFishTasty);        // Prints 0 (false)
Try it Yourself »

However, it is more common to get boolean values by comparing values and variables.



Comparing Values and Variables

Comparing values is useful in programming, because they help 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);  // Prints 1 (true), because 10 is greater than 9
Try it Yourself »

In C, comparisons return 1 (true) or 0 (false).

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\n", 10 == 10); // Prints 1 (true), because 10 is equal to 10
printf("%d\n", 10 == 15); // Prints 0 (false), because 10 is not equal to 15
printf("%d", 5 == 55);    // Prints 0 (false), because 5 is not equal to 55
Try it Yourself »

You are not limited to comparing numbers. You can also compare boolean variables:

Example

bool isHamburgerTasty = true;
bool isPizzaTasty = true;

// Check if both have the same value (both true, or both false)
printf("%d", isHamburgerTasty == isPizzaTasty);
Try it Yourself »

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


Storing Comparison Results

You can also store the result of a comparison in a bool variable:

Example

int x = 10;
int y = 9;

bool isGreater = x > y;

printf("%d", isGreater); // Prints 1 (true)
Try it Yourself »

Note: It is up to you whether you store the result of a comparison in a boolean variable or use the comparison directly.
Storing the result can make your code easier to read, especially if you want to reuse it.


Visualize Booleans

Choose a value below to see what is stored in the boolean variable:

Code


What is stored

    Output

    Explanation




    Free account

    Track your progress

    XP earned 0 Day streak 0 Spaces 0 League —

    New

    Learn in Adventure Mode W3Schools Adventure App

    Coding fundamentals as bite-sized lessons and challenges.

    ×

    Contact Sales

    If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
    sales@w3schools.com

    Report Error

    If you want to report an error, or if you want to make a suggestion, send us an e-mail:
    help@w3schools.com

    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, cookies and privacy policy.

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