C Booleans

C Booleans

C- booleans are a data type in the C Standard Library which can store true or false. Boolean contains two types of values, i.e., 0 and 1. , the bool type value represents two types of behavior, is true or false.  ‘0’ represents false value, while ‘1’ is represents the true value. In C Boolean, ‘0’ is stored as 0, and another integer is stored as 1. 

C boolean Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
bool variable_name;
bool variable_name;
bool variable_name;  

Example of C booleans

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include<stdbool.h>
int main()
{
bool x=false; // variable initialization.
if(x==true) // conditional statements
{
printf("The value of x is true");
}
else
printf("The value of x is FALSE");
return 0;
}
#include <stdio.h> #include<stdbool.h> int main() { bool x=false; // variable initialization. if(x==true) // conditional statements { printf("The value of x is true"); } else printf("The value of x is FALSE"); return 0; }
#include <stdio.h>  
#include<stdbool.h>  
int main()  
{  
bool x=false; // variable initialization.  
if(x==true) // conditional statements  
{  
printf("The value of x is true");  
}  
else  
printf("The value of x is FALSE");  
return 0;  
}

Print boolean c

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The value of x is FALSE
The value of x is FALSE
The value of x is FALSE

Boolean Expressions

The expression which returns boolean values: 1 (true) or 0 (false) value is called boolean expression. In the following example, using the comparison operator in the boolean expression which returns 1 when left operand is greater than right operand else returns 0.

Program for boolean expressions

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main (){
int x = 10;
int y = 25;
printf("%i\n", (x > y)); //returns 0 (false)
return 0;
}
#include <stdio.h> int main (){ int x = 10; int y = 25; printf("%i\n", (x > y)); //returns 0 (false) return 0; }
#include <stdio.h>

int main (){
  int x = 10;
  int y = 25;

  printf("%i\n", (x > y)); //returns 0 (false)
  return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

 

 

Also read, C-Strings

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *