Learn C - C Logical Operators






These operators can be used to determine the logic between variables or values.

Operator Meaning
&&and
||or
!not

The AND Operator &&

The logical AND operator, &&, is a binary operator that combines two logical expressions-that is, two expressions that evaluate to true or false.

Consider this expression:

test1 && test2

This expression evaluates to true if both expressions test1 and test2 evaluate to true.

If either or both of the operands are false, the result of the operation is false.

The obvious place to use the && operator is in an if expression. Here's an example:

if(age > 12 && age < 20) {
  printf("You are a teenager.");
}

The printf() statement will be executed only if age has a value from 13 to 19 inclusive.

Of course, the operands of the && operator can be bool variables.

You could replace the previous statement with the following:

bool test1 = age > 12;
bool test2 = age < 20;

if(test1 && test2) {
  printf("You are a teenager.");
}

Truth Table For The And Operator

xyResult
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

use the logical AND operator


#include <stdio.h>
#define PERIOD '.'
int main(void)
{/* w w  w .  java2 s . c om*/
    char ch;
    int charcount = 0;
    
    while ((ch = getchar()) != PERIOD)
    {
        if (ch != '"' && ch != '\'')
            charcount++;
    }
    printf("There are %d non-quote characters.\n", charcount);
 
    return 0;
}

The code above generates the following result.





OR Operator ||

The logical OR operator, ||, checkes for any of two or more conditions being true.

If either or both operands of the || operator are true, the result is true.

The result is false only when both operands are false.

Here's an example of using this operator:

if(a < 10 || b > c || c > 50) {
  printf("At least one of the conditions is true.");
}

The printf() will be executed only if at least one of the three conditions, a<10, b>c, or c>50, is true.

You can use the && and || logical operators in combination, as in the following code fragment:

if((age > 12 && age < 20) || savings > 5000) {
   printf ("hi.");
}

You could replace the previous statement with the following:

bool over_12 = age > 12;
bool undere_20 = age < 20;

bool age_check = over_12 && under_20;
bool savings_check = savings > 50;

if(age_check || savings_check) {
   printf ("Hi.");
}

Truth Table For The Or Operator

xyResult
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse




NOT Operator !

The logical NOT operator, represented by !.

The ! operator is a unary operator, since it applies to just one operand.

The logical NOT operator reverses the value of a logical expression: true becomes false, and false becomes true.

if( !(age <= 12) )
{
  printf("Hi.");
}

TRUTH TABLE FOR THE NOT OPERATOR

xResult
truefalse
falsetrue

Example

To illustrate how to use logical operators in C code, you can write this code.


  #include <stdio.h> 
//  w  w  w .  j a v  a  2  s.co m
  int main() { 
     int a,b; 
     a = 5; 
     b = 8; 
      
     printf("%d \n",a>b && a!=b); 
     printf("%d \n",!(a>=b)); 
     printf("%d \n",a==b  || a>b); 

     return 0; 
  } 

The code above generates the following result.

Example 2

The following code shows how to test letters case.


#include <stdio.h>
int main(void)
{// w ww  .  ja va 2 s. c om
  char letter = 0;                               // Stores an input character

  printf("Enter an upper case letter:");         // Prompt for input
  scanf(" %c", &letter);                         // Read the input character

  if((letter >= 'A') && (letter <= 'Z'))         // Verify uppercase letter
  {
    letter += 'a'-'A';                           // Convert to lowercase
    printf("You entered an uppercase %c.\n", letter);
  }
  else
    printf("You did not enter an uppercase letter.\n");
  return 0;
}

The code above generates the following result.

Example 3

The following code uses function from ctype.h :


    #include <stdio.h>
    #include <ctype.h>
/*w w w . j  a v  a  2 s .  c om*/
    int main(void)
    {
        char letter = 0;                          // Stores a character
        printf("Enter an uppercase letter:");     // Prompt for input
        scanf("%c", &letter);                     // Read a character
        if(isalpha(letter) && isupper(letter))
           printf("You entered an uppercase %c.\n", tolower(letter));
        else
           printf("You did not enter an uppercase letter.\n");
      return 0;
    }

The code above generates the following result.