Children actively learning with a colorful abacus in a preschool setting, enhancing educational fun.

Mastering Operators in C: Arithmetic, Relational, and Logical

Welcome to the third post in our “C Programming Insights” series! Now that you’re familiar with variables and data types, let’s take a deeper dive into operators—the tools that allow you to perform calculations, compare values, and control program flow in C.


What Are Operators in C?

Operators are symbols that perform operations on variables and values. For example:

int result = 10 + 5;

Here, + is the arithmetic operator used to add two values.


Types of Operators in C

1. Arithmetic Operators

Used to perform basic mathematical operations:

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example Code:

int a = 10, b = 3;  
printf("Addition: %d\n", a + b);  
printf("Remainder: %d\n", a % b);  

Relational Operators

Used to compare two values, resulting in true (1) or false (0).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example Code:

int x = 5, y = 10;  
printf("x > y: %d\n", x > y);  // Output: 0 (false) 

3. Logical Operators

Used to combine or invert conditions.

OperatorDescriptionExample
&&Logical AND (true if both true)(a > 0) && (b > 0)
``
!Logical NOT (invert truth value)!(a > b)

Example Code:

int a = 10, b = -5;  
printf("Logical AND: %d\n", (a > 0) && (b > 0));  // Output: 0 (false)

Combining Operators in a Program

Here’s how you can use different types of operators together:

Example Program:

#include <stdio.h>  

int main() {  
    int a = 15, b = 10, c = 5;  

    // Arithmetic  
    int sum = a + b + c;  

    // Relational  
    int isGreater = sum > 20;  

    // Logical  
    int check = (a > b) && (b > c);  

    printf("Sum: %d\n", sum);  
    printf("Is sum greater than 20? %d\n", isGreater);  
    printf("Logical check: %d\n", check);  

    return 0;  
}  

Output:

Sum: 30  
Is sum greater than 20? 1  
Logical check: 1  

Programming Tips for Operators

  1. Use parentheses () to make expressions clearer and avoid operator precedence issues.
  2. Be careful with integer division; it truncates the decimal part.
    • Example: 5 / 2 = 2 (not 2.5).
  3. Always test your conditions with edge cases to avoid logical errors.

Practice Problem

Write a program that:

  1. Takes two integers as input.
  2. Uses relational and logical operators to check if both numbers are positive.
  3. Displays the larger of the two numbers.

What’s Next?

In the next post, we’ll explore control structures in C, such as if, else, and loops, to create more dynamic and interactive programs.

Follow Us
Stay updated with isoftguide.in for more tips, tutorials, and career resources in programming and IT!

Leave a Comment

Your email address will not be published. Required fields are marked *