Relational and Logical Operators in C (Explained Simply)

Master decision-making in C programming! Learn how to use relational (==, <, >) and logical (&&, ||, !) operators to build smart, dynamic software log

Introduction

In our previous chapter, we mastered arithmetic operators, allowing our software to act as a powerful calculator. However, real-world software does much more than calculate; it makes decisions. Think about an ATM: If your PIN is correct and your balance is greater than the requested amount, the machine dispenses cash. Otherwise, it declines the transaction.

How do we teach a computer to make these logical decisions? We use Relational and Logical Operators. These tools allow the compiler to compare values, evaluate complex conditions, and decide whether a statement is true or false.

In this chapter, we will learn the foundation of software decision-making. We will uncover how C handles truth, explore the "equals" trap that catches every beginner, and combine conditions to build smart logic.



Illustration of a glowing logic gate evaluating a condition using AND operators to output a true binary signal, with a deep blue tech theme.




How C Handles Truth (0 and 1)

Before we look at the operators, you must understand a fundamental quirk of the C language. Many modern languages have a built-in "Boolean" data type that holds the words true or false. Traditional C programming does not.

In C, truth is represented by mathematics:

  • False is always represented by the number 0.
  • True is represented by 1 (or technically, any non-zero number).

When you ask the computer a question like "Is 5 greater than 3?", it will not print the word "true". It will print the integer 1.



Relational Operators (Comparing Data)

Relational operators compare two values. They form the basis of all questions you ask your computer.

  • Greater Than (>): Checks if the left side is larger. (e.g., 5 > 3 is 1/True)
  • Less Than (<): Checks if the left side is smaller. (e.g., 5 < 3 is 0/False)
  • Greater Than or Equal To (>=): (e.g., 5 >= 5 is 1/True)
  • Less Than or Equal To (<=): (e.g., 4 <= 5 is 1/True)
  • Equal To (==): Notice the double equals! Checks if two values are exactly the same. (e.g., 5 == 5 is 1/True)
  • Not Equal To (!=): The exclamation mark means "Not". Checks if values are different. (e.g., 5 != 3 is 1/True)

Logical Operators (Combining Conditions)

Often, you need to check multiple things at once. For example, to get a senior citizen discount, your age must be greater than 60 AND you must be a registered member. Logical operators connect multiple relational conditions.

  • Logical AND (&&): Returns True (1) ONLY if all conditions are true. If even one is false, the whole thing is false.
  • Logical OR (||): Returns True (1) if at least one condition is true. It only returns False if both are false.
  • Logical NOT (!): Reverses the truth. If a condition is True, NOT makes it False. (e.g., !(5 == 5) becomes 0/False).


Code Examples: Voting Eligibility Checker

#include <stdio.h>

int main() {
    int age;
    int is_citizen; // 1 for Yes, 0 for No
    
    printf("--- Voting Eligibility System ---\n");
    printf("Enter your age: ");
    scanf("%d", &age);
    
    printf("Are you a citizen? (Enter 1 for Yes, 0 for No): ");
    scanf("%d", &is_citizen);

    // Using Relational Operators
    int is_adult = (age >= 18);
    
    // Using Logical AND (&&) to combine conditions
    int can_vote = is_adult && (is_citizen == 1);

    printf("\n--- Results ---\n");
    printf("Adult Status (1=True, 0=False): %d\n", is_adult);
    printf("Voting Eligibility (1=True, 0=False): %d\n", can_vote);

    return 0;
}

Code Output

--- Voting Eligibility System ---
Enter your age: 20
Are you a citizen? (Enter 1 for Yes, 0 for No): 0

--- Results ---
Adult Status (1=True, 0=False): 1
Voting Eligibility (1=True, 0=False): 0

Code Explanation

In this code, we evaluate two conditions. First, we use the relational operator >= to check if the user is 18 or older. Because they entered 20, is_adult stores 1 (True). Next, we use the logical operator &&. For can_vote to be True, the user must be an adult AND they must be a citizen. Since they entered 0 for citizenship, the AND logic fails, storing 0 (False) in can_vote.



Common Mistakes for Beginners

  • The Assignment vs. Equality Trap: The biggest mistake beginners make is using a single = to compare values (e.g., age = 18). A single equals sign assigns a value. You MUST use double equals == to compare values (e.g., age == 18).
  • Chaining Relational Operators: Writing 10 < age < 20 works in math class, but it completely breaks in C. You must split it up using Logical AND: (age > 10) && (age < 20).


Best Practices

  • Use Parentheses for Readability: While not always strictly required, wrapping your conditions in parentheses like (age > 18) && (score == 100) makes your code infinitely easier for other engineers to read.
  • Short-Circuit Evaluation: Know that C is efficient. If you use && and the first condition is False, C instantly stops checking the rest of the line because it knows the final result will be False anyway.

Interview Questions

Q: What is the output of the expression: (5 > 3) || (10 < 2)?
Answer: The output is 1 (True). Because we used the Logical OR (||) operator, only one of the conditions needs to be true. Since 5 is greater than 3, the entire expression evaluates to True.

Q: In C, is the number -5 considered true or false?
Answer: It is considered True. In traditional C, absolutely any non-zero integer (whether positive or negative) evaluates to True. Only exactly 0 evaluates to False.



MCQs

Q1. Which operator is used to check if two values are NOT equal?
A) <>
B) !==
C) != (Correct)
D) =!

Q2. What will the expression !(1 == 1) evaluate to?
A) 1
B) 0 (Correct)
C) True
D) Error



Practice Questions

  1. Write a program that takes a student's score. Calculate a variable passed that stores 1 if the score is greater than or equal to 40, and 0 if it is less.
  2. Write an expression that evaluates to 1 ONLY if a variable temperature is between 20 and 30 (inclusive).

Mini Assignment

Build a "Discount Approver" logic sequence. Ask the user for their total purchase amount and whether they are a VIP member (1 for yes, 0 for no). Create a variable that stores 1 (True) if they spend more than $100 OR they are a VIP member. Print the result.



Conclusion

You have just learned the core syntax of software intelligence. By combining relational operators (to compare data) with logical operators (to combine rules), you can now build systems that evaluate complex real-world conditions.

Right now, we are just printing 1s and 0s to represent decisions. In Chapter 13: Increment, Decrement, and Assignment Operators, we will learn how to rapidly change variable values, paving the way for the ultimate decision-making tool: the if-else statement! Keep up the great work.



📚 Recommended Reading for Aspiring Developers

Logical operators are the backbone of decision-making in tech. See how this logic is applied in advanced computer science concepts across NeoGyan:


About the author

Jayanta Mondal
Jayanta Mondal is a BCA student, web developer, and the founder of NeoGyan. He is passionate about simplifying complex tech concepts for beginners.

Post a Comment