C Tutorials: Booleans in C programming

 


A boolean is a data type in the C Standard Library which can store true or false. Every non-zero value corresponds to true while 0 corresponds to false.

C doesn’t natively support boolean type. In C, zero means false. And other values are true. For example, all the numbers which are not zero are true.

The boolean works as it does in C++. However, if you don’t include the header file​ stdbool.h, the program will not compile.

svg viewer
 

Declaration

To declare a variable as a boolean use:

bool variable_name = true;

Example:

#include  
#include 
int main() 
{ 
bool a = true;
  if(a)
    printf("Its ture");
return 0; 
} 

Bool with Logical Operators

We can use logical operators with boolean.

Types of logical operators:

  • && (AND): takes 2 booleans; returns true only if both operands are true else false
  • || (OR): returns true if either or both of the operands are true else false
  • ! (NOT): takes 1 operand; return true if operand is false and false if operand is true

Example:

#include 
#include 
 
int main(void)
{
    bool a=true, b=false;
    printf("%dn", a&&b);
    printf("%dn", a||b);
    printf("%dn", !b);
}

Output:

0
1
1

Bool Array

#include  
int main() 
{ 
  bool arr[2] = {true, false}; 
  printf("Value at index 1 of array is %d",arr[1]);
  return 0; 
} 

Output:

Value at index 1 of array is 0

How to convert a boolean to integer? (type casting)

A type cast is basically a conversion from one type to another.
An object declared as type Bool is large enough to store the values 0 and 1.

Example:

#include 
#include 

int main() {
	int n = 1;
	bool x = true;
	n = (bool)true;;
	printf("%d",n);        //Output: 1
	return 0;
}

There's no need to cast to bool for built-in types because that conversion is implicit. On converting to other integral types, a true bool will become 1 and a false bool will become 0.

Important Points:

  • A true boolean data type could be used for storing logical values, and would only have two legal values - "true", and "false".
  • C does not have boolean data types, and normally uses integers for boolean testing.
    • Zero is used to represent false, and One is used to represent true.
    • For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
    • To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.
      • In the old days, this was done using #define:
        • #define true 1
        • #define false 0
      • Today it is better to use const int instead of #define:
        • const int true = 1;
        • const int false = 0;
      • C99 provides a new header, stdbool.h, that defines some new helpful types and constants:
        • The type "bool" is the same as a newly defined type "_Bool"
          • _Bool is an unsigned integer, that can only be assigned the values 0 or 1
          • Attempting to store anything else in a _Bool stores a 1. ( Recall that C interprets any non-zero as true, i.e. 1 )
          • Variables can now be declared of type "bool".
        • stdbool.h also defines constants "true" and "false", with value 1 and 0 respectively.
        • Use these new features using #include at the top of your program.

However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Tutorial. Here you will get all the topics of the C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Booleans in C Language.

Thanks for Reading !! Keep Learning !!

 
 
 
 

Post a Comment

0 Comments