C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Logical Operators

C Programming

In this tutorial we will learn about logical operators in C programming language.

We use the logical operators to test more than one condition.

Logical expressions yields either non-zero (true) or zero (false) value.

There are three logical operators in C.

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT

Click here to learn about Boolean Algebra.

Logical AND

The logical AND && operator will give non-zero (true) value if both the operands are non-zero (true). Otherwise, it will return zero (false).

Truth table of logical AND operator.

ABA && B
000
010
100
111

In the following example we are checking if the two logical expressions are both true. If they are then we will execute the if-block otherwise, the else-block.

#include <stdio.h>
int main(void)
{
  int logical_expression_1 = 10 > 0;	//this will give non-zero (true) value
  int logical_expression_2 = 20 <= 100;	//this will give non-zero (true) value
    
  if (logical_expression_1 && logical_expression_2) {
    printf("Success\n");
  }
  else {
    printf("No!!!\n");
  }
  return 0;
}

Output

Success

Logical OR

The logical OR || operator will give non-zero (true) value if any one of the operand is non-zero (true). If both are zero then it will return zero (false).

Truth table of logical OR operator.

ABA || B
000
011
101
111

In the following example we are checking if any one of the two logical expressions is non-zero (true). If yes, then we will execute the if-block otherwise, the else-block.

#include <stdio.h>
int main(void)
{
  int logical_expression_1 = 10 > 0;	//this will give non-zero (true) value
  int logical_expression_2 = 20 >= 100;	//this will give zero (false) value
    
  if (logical_expression_1 || logical_expression_2) {
    printf("Success\n");
  }
  else {
    printf("No!!!\n");
  }
  return 0;
}

Output

Success

Logical NOT

The logical NOT ! operator will give non-zero (true) value if the operand is zero (false). And it will return zero (false) value if the operand is non-zero (true).

Logical NOT operator works with only one operand.

Truth table of logical NOT operator.

A!A
01
10

In the following example we are checking if the logical expression is zero (false). If yes, then we will execute the if-block otherwise, the else-block.

#include <stdio.h>
int main(void)
{
  int logical_expression = 10 < 0;	//this will give zero (false) value
    
  if (!logical_expression) {
    printf("Success\n");
  }
  else {
    printf("No!!!\n");
  }
  return 0;
}

Output

Success

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT