Relational and Logical Operators in C

CProgrammingServer Side Programming

Relational Operators

Relational operators are used to compare two values in C language. It checks the relationship between two values. If relation is true, it returns 1. However, if the relation is false, it returns 0.

Here is the table of relational operators in C language

OperatorsOperator Name
==Equal to
>Greater than
<Less than
!=Not equal to
>=Greater than or equal to
<=Less than or equal to

Here is an example of relational operator in C language

Example

 Live Demo

#include <stdio.h>
int main() {
   int x = 10;
   int y = 28;
   if(x==y)
   printf("Both variables are equal\n");
   if(x>y)
   printf("x is greater than y \n");
   if(x<y)
   printf("x is less than y \n");
   if(x!=y)
   printf("x is not equal to y \n");
   if(x<=y)
   printf("x is lesser or equal to y\n");
   if(x>=y)
   printf("x is greater or equal to y \n");
   return 0;
}

Output

x is less than y
x is not equal to y
x is lesser or equal to y

Logical Operators

Logical operators are used to perform logical operations. It returns 0 or 1 based on the result of condition, whether its true or false. These operators are used for decision making in C language.

Here is the table of logical operators in C language,

OperatorsMeaning of OperatorsResults
&&Logical ANDTrue when all operands are true
||Logical ORTrue only if either one operand is true
!Logical NOTTrue when operand is zero

Here is an example of logical operators in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int x = 10;
   int y = 28;
   int a = 15;
   int b = 20;
   if(x<y && a==b)
   printf("x is less than y AND a is equal to b\n");
   if(x<y || a==b)
   printf("x is less than y OR a is equal to b\n");
   if(!x)
   printf("x is zero\n");
   return 0;
}

Output

x is less than y OR a is equal to b
raja
Published on 08-Oct-2018 06:02:22
Advertisements