Operators In C | Set 2 (Relational And Logical Operators)

Operators In C | Set 2 (Relational And Logical Operators)

In C programming, operators are like tools that help us make decisions and control how our program works. This time, we’re going to focus on two important types of operators: relational and logical operators. Relational operators help us compare things, like checking if one number is greater than another. Logical operators help us combine conditions and decide what our program should do based on those conditions. Learning how to use these operators is crucial for writing good C code. So, let’s dive into the world of relational and logical operators and see how they can make our programs smarter and more effective.

Relational Operators

Here are some common comparison operators in C:

  1. Equal to operator (==): This operator checks if two values are the same. If they are equal, it returns true; otherwise, it returns false. For example, 5 == 5 will return true.
  2. Not equal to operator (!=): This operator checks if two values are not the same. If they are not equal, it returns true; otherwise, it returns false. It’s the opposite of the equal to operator. For example, 5 != 5 will return false.
  3. Greater than operator (>): The greater than operator checks if the first value is larger than the second value. If it is, it returns true; otherwise, it returns false. For example, 6 > 5 will return true.
  4. Less than operator (<): The less than operator checks if the first value is smaller than the second value. If it is, it returns true; otherwise, it returns false. For example, 6 < 5 will return false.
  5. Greater than or equal to operator (>=): This operator checks if the first value is greater than or equal to the second value. If it is, it returns true; otherwise, it returns false. For example, 5 >= 5 will return true.
  6. Less than or equal to operator (<=): This operator checks if the first value is less than or equal to the second value. If it is, it returns true; otherwise, it returns false. For example, 5 <= 5 will also return true.

These operators are fundamental for making comparisons and decisions in C programming, allowing you to control the flow of your programs based on different conditions.


// 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;
}

Output

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

Time Complexity: 0(1)

Auxiliary Space: O(1)

Logical Operators

  1. 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).
  2. 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.
  3. 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

Exampe

// 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;
}

Output

AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero

Time Complexity: O(1)

Auxiliary Space: O(1)

Short-Circuiting in Logical Operators

In many programming languages, including C and C++, the logical AND operator (&&) performs short-circuit evaluation. This means that if the first operand of a logical AND is false, the second operand is not evaluated because the overall result will always be false. This can be particularly useful for improving efficiency and preventing errors in situations where evaluating the second operand might lead to issues.

For example, In program 1 below doesn’t print “skill Vertex Quiz” as the first operand of logical AND itself is false. 

#include <stdbool.h>
#include <stdio.h>
int main()
{
    int a = 10, b = 4;
    bool res = ((a == b) && printf("Skill Vertex Quiz"));
    return 0;
}

Output

No Output

Whereas, the program below prints Skill Vertex Quiz as the First Operand of Logical AND is true

#include <stdbool.h>
#include <stdio.h>
int main()
{
    int a = 10, b = 4;
    bool res = ((a != b) && printf("Skill Vertex Quiz"));
    return 0;
}

Output

Skill Vertex Quiz

Time Complexity: O(1)

Auxiliary Space: O(1)

In the logical OR operator (||) also performs short-circuit evaluation. This means that if the first operand of a logical OR is true, the second operand is not evaluated because the overall result will always be true. For example, program 1 below won’t print “SkillVertexQuiz” as the first operand of logical OR itself is true.

#include <stdbool.h>
#include <stdio.h>
int main()
{
    int a = 10, b = 4;
    bool res = ((a != b) || printf("Skill Vertex Quiz"));
    return 0;
}

Output

No Output 

Time Complexity: O(1)

Auxiliary Space:O(1)

Whereas, below program below will print “Skill Vertex Quiz” as the first operand of logical OR is false. 

#include <stdbool.h>
#include <stdio.h>
int main()
{
    int a = 10, b = 4;
    bool res = ((a == b) || printf("Skill Vertex Quiz"));
    return 0;
}

Output

Skill Vertex Quiz

Time Complexity: O(1)

Auxiliary Space: O(1)

FAQ- Operators In C | Set 2 (Relational and Logical Operators)

Q1. What is relational operator and logical operator?

Ans. Relational, equality, and logical operators are essential tools in programming.
Relational operators compare values to determine relationships (e.g., greater than or less than).
Equality operators check if values are equal or not equal.
Logical operators combine true and false values to create more complex conditions (e.g., AND and OR).
These operators help in decision-making, controlling program flow, and forming logical expressions.

Q2. What are the 2 operators in C?

Ans. In C programming, the increment (++) and decrement (--) operators are used to modify the value of an operand, whether it’s a constant or a variable. The increment operator (++) increases the value by 1, while the decrement operator (--) decreases the value by 1. These operators are considered unary operators because they operate on a single operand.

Q3. What is %d in C?

Ans. Format specifier: It is denoted by a % symbol followed by a letter, and it instructs the program on how to format and interpret data for input and output.
%d: This format specifier is used to represent a signed decimal integer. It tells the program that the associated variable should be treated as an integer and formatted accordingly.
%i: Similarly, %i is also a format specifier used for integers. In C, %d and %i are often interchangeable when reading or printing integers, as both are used for the same purpose

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment