Operators in C | Set 2 (Relational and Logical Operators)

23 Aug, 2021
We have discussed Introduction to Operators in C where we got an overall idea of what types of Operators, C and C++ support and its basic implementations. Following that, we studied Arithmetic Operators where we got a detailed understanding of the types and use of Arithmetic operators in C and C++. In this article, let’s try to understand the types and uses of Relational and Logical Operators. 
Relational Operators Relational operators are used for comparison of two values to understand the type of relationship a pair of number shares. For example, less than, greater than, equal to etc. Let’s see them one by one  
Equal to operator: Represented as ‘==’, the equal to operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.Not equal to operator: Represented as ‘!=’, the not equal to operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.Greater than operator: Represented as ‘>’, the greater than operator checks whether the first operand is greater than the second operand or not. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.Less than operator: Represented as ‘<‘, the less than operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.Greater than or equal to operator: Represented as ‘>=’, the greater than or equal to operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true else it returns false. For example, 5>=5 will return true.Less than or equal to operator: Represented as ‘<=’, the less than or equal tooperator checks whether the first operand is less than or equal to the second operand. If so, it returns true else false. For example, 5<=5 will also return true.
Equal to operator: Represented as ‘==’, the equal to operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
Not equal to operator: Represented as ‘!=’, the not equal to operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
Greater than operator: Represented as ‘>’, the greater than operator checks whether the first operand is greater than the second operand or not. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
Less than operator: Represented as ‘<‘, the less than operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
Greater than or equal to operator: Represented as ‘>=’, the greater than or equal to operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true else it returns false. For example, 5>=5 will return true.
Less than or equal to operator: Represented as ‘<=’, the less than or equal tooperator checks whether the first operand is less than or equal to the second operand. If so, it returns true else false. For example, 5<=5 will also return true.
Examples:  
C
C++
// C program to demonstrate working of relational operators#include <stdio.h> int main(){    int a = 10, b = 4;     // greater than example    if (a > b)        printf("a is greater than b\n");    else        printf("a is less than or equal to b\n");     // greater than equal to    if (a >= b)        printf("a is greater than or equal to b\n");    else        printf("a is lesser than b\n");     // less than example    if (a < b)        printf("a is less than b\n");    else        printf("a is greater than or equal to b\n");     // lesser than equal to    if (a <= b)        printf("a is lesser than or equal to b\n");    else        printf("a is greater than b\n");     // equal to    if (a == b)        printf("a is equal to b\n");    else        printf("a and b are not equal\n");     // not equal to    if (a != b)        printf("a is not equal to b\n");    else        printf("a is equal b\n");     return 0;}
// C++ program to demonstrate working of logical operators#include <iostream>using namespace std; int main(){    int a = 10, b = 4;     // greater than example    if (a > b)        cout << "a is greater than b\n";    else        cout << "a is less than or equal to b\n";     // greater than equal to    if (a >= b)        cout << "a is greater than or equal to b\n";    else        cout << "a is lesser than b\n";     // less than example    if (a < b)        cout << "a is less than b\n";    else        cout << "a is greater than or equal to b\n";     // lesser than equal to    if (a <= b)        cout << "a is lesser than or equal to b\n";    else        cout << "a is greater than b\n";     // equal to    if (a == b)        cout << "a is equal to b\n";    else        cout << "a and b are not equal\n";     // not equal to    if (a != b)        cout << "a is not equal to b\n";    else        cout << "a is equal b\n";     return 0;}
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
 
Logical Operators: They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under consideration. They are described below:  
Logical AND operator: The ‘&&’ operator returns true when both the conditions under consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.Logical NOT operator: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
Logical AND operator: The ‘&&’ operator returns true when both the conditions under consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.
Logical NOT operator: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
Examples:  
C
C++
// C program to demonstrate working of logical operators#include <stdio.h> int main(){    int a = 10, b = 4, c = 10, d = 20;     // logical operators     // logical AND example    if (a > b && c == d)        printf("a is greater than b AND c is equal to d\n");    else        printf("AND condition not satisfied\n");     // logical OR example    if (a > b || c == d)        printf("a is greater than b OR c is equal to d\n");    else        printf("Neither a is greater than b nor c is equal "               " to d\n");     // logical NOT example    if (!a)        printf("a is zero\n");    else        printf("a is not zero");     return 0;}
// C++ program to demonstrate working of// logical operators#include <iostream>using namespace std; int main(){    int a = 10, b = 4, c = 10, d = 20;     // logical operators     // logical AND example    if (a > b && c == d)        cout << "a is greater than b AND c is equal to d\n";    else        cout << "AND condition not satisfied\n";     // logical OR example    if (a > b || c == d)        cout << "a is greater than b OR c is equal to d\n";    else        cout << "Neither a is greater than b nor c is equal "                " to d\n";     // logical NOT example    if (!a)        cout << "a is zero\n";    else        cout << "a is not zero";     return 0;}
AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero
 
Short-Circuiting in Logical Operators:  
In case of logical AND, the second operand is not evaluated if first operand is false. For example, program 1 below doesn’t print “GeeksQuiz” as the first operand of logical AND itself is false.  
C
C++
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a == b) && printf("GeeksQuiz"));    return 0;}
#include <iostream>using namespace std; int main(){    int a = 10, b = 4;    bool res = ((a == b) && cout << "GeeksQuiz");    return 0;}
No Output
 
But below program prints “GeeksQuiz” as first operand of logical AND is true.  
C
C++
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a != b) && printf("GeeksQuiz"));    return 0;}
#include <iostream>using namespace std; int main(){    int a = 10, b = 4;    bool res = ((a != b) && cout << "GeeksQuiz");    return 0;}
GeeksQuiz
 
In case of logical OR, the second operand is not evaluated if first operand is true. For example, program 1 below doesn’t print “GeeksQuiz” as the first operand of logical OR itself is true.  
C
C++
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a != b) || printf("GeeksQuiz"));    return 0;}
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a != b) || cout << "GeeksQuiz");    return 0;}
No Output
 
But below program prints “GeeksQuiz” as first operand of logical OR is false.  
C
C++
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a == b) || printf("GeeksQuiz"));    return 0;}
#include <stdbool.h>#include <stdio.h>int main(){    int a = 10, b = 4;    bool res = ((a == b) || cout << "GeeksQuiz");    return 0;}
GeeksQuiz
 
Quiz on Operators in CThis article is contributed by Ayush Jaggi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
yashraj11
C-Operators
cpp-operator
Operators
Bit Magic
C Language
cpp-operator
Bit Magic
Operators
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Josephus problem | Set 1 (A O(n) Solution)
Divide two integers without using multiplication, division and mod operator
std::sort() in C++ STL
Arrays in C/C++
Substring in C++
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Function Pointer in C