Paramclasses Academy - Blog

Tutorials Details

←
Post 7 of 15
→

What is Operators in C Language

What is Operators in C Language

October 14, 2026

1. Introduction

In Module 2, we learned about variables, constants, and data types in C programming.

Now, we will learn about operators, which allow us to perform calculations, compare values, assign data, and make logical decisions.

For example, a calculator uses operators such as +, -, *, and /. Similarly, C programs use operators to perform arithmetic and other operations on data.

In this module, you will learn the different types of operators in C, how they work, and how to use them in practical programs.

2. What Is an Operator?

An operator is a symbol that specifies an operation to be performed on one or more operands.

For example:

int a = 10;
int b = 5;

int sum = a + b;

Here:

  • a and b are operands.

  • + is the addition operator.

  • sum stores the result, which is 15.

C provides several categories of operators.

Operator category

Purpose

Arithmetic

Performs mathematical calculations

Relational

Compares two values

Logical

Combines or reverses conditions

Assignment

Assigns or updates values

Increment and decrement

Increases or decreases a value

Bitwise

Performs operations on individual bits

Conditional

Selects between two expressions

Special operators

Includes operators such as sizeof and address-of

3. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator

Operation

Example

Result

+

Addition

10 + 5

15

-

Subtraction

10 - 5

5

*

Multiplication

10 * 5

50

/

Division

10 / 5

2

%

Remainder

10 % 3

1

Example 1: Arithmetic Operations

#include <stdio.h>

int main(void)
{
    int a = 10;
    int b = 3;

    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("Remainder: %d\n", a % b);

    return 0;
}

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1

Important concept: Integer division

When both operands are integers, division discards the fractional part.

For example:

printf("%d\n", 10 / 3);

Output:

3

To perform floating-point division, use a floating-point operand:

printf("%.2f\n", 10.0 / 3);

Output:

3.33

Note: The % operator is used with integer operands in C. Division by zero and a remainder operation with a zero divisor are invalid.

4. Relational Operators

Relational operators compare two values. In C, a comparison expression evaluates to 1 when true and 0 when false.

Operator

Meaning

Example

==

Equal to

a == b

!=

Not equal to

a != b

>

Greater than

a > b

<

Less than

a < b

>=

Greater than or equal to

a >= b

<=

Less than or equal to

a <= b

Example 2: Comparing Values

#include <stdio.h>

int main(void)
{
    int a = 10;
    int b = 5;

    printf("a == b: %d\n", a == b);
    printf("a != b: %d\n", a != b);
    printf("a > b: %d\n", a > b);
    printf("a < b: %d\n", a < b);

    return 0;
}

Output:

a == b: 0
a != b: 1
a > b: 1
a < b: 0

Important distinction

  • = assigns a value.

  • == compares two values.

For example:

int x = 10;      // Assignment
printf("%d", x == 10);  // Comparison

Confusing these operators is a common beginner mistake.

5. Logical Operators

Logical operators combine conditions or reverse a condition.

| Operator | Name | Meaning | |---|---|---| | && | Logical AND | True if both conditions are true | | || | Logical OR | True if at least one condition is true | | ! | Logical NOT | Reverses the truth value |

Example 3: Logical Operations

#include <stdio.h>

int main(void)
{
    int age = 22;

    printf("%d\n", age >= 18 && age <= 60);
    printf("%d\n", age < 18 || age > 60);
    printf("%d\n", !(age >= 18));

    return 0;
}

Output:

1
0
0

Explanation

  • age >= 18 && age <= 60 is true because both conditions are true.

  • age < 18 || age > 60 is false because neither condition is true.

  • !(age >= 18) is false because the original condition is true.

Important: && and || use short-circuit evaluation. The second operand may not be evaluated if the first operand determines the result.

6. Assignment Operators

Assignment operators assign values to variables or update their existing values.

Operator

Example

Equivalent form

=

a = 10

Assigns 10 to a

+=

a += 5

a = a + 5

-=

a -= 5

a = a - 5

*=

a *= 2

a = a * 2

/=

a /= 2

a = a / 2

%=

a %= 3

a = a % 3

Example 4: Assignment Operators

#include <stdio.h>

int main(void)
{
    int a = 10;

    a += 5;
    printf("After +=: %d\n", a);

    a -= 3;
    printf("After -=: %d\n", a);

    a *= 2;
    printf("After *=: %d\n", a);

    return 0;
}

Output:

After +=: 15
After -=: 12
After *=: 24

Assignment operators are especially useful when updating totals, counters, and running calculations.

7. Increment and Decrement Operators

The increment operator ++ increases a variable by one.

The decrement operator -- decreases a variable by one.

They each have two forms:

  • Prefix: ++a or --a

  • Postfix: a++ or a--

Example 5: Increment and Decrement

#include <stdio.h>

int main(void)
{
    int a = 5;

    printf("Original: %d\n", a);
    printf("Prefix increment: %d\n", ++a);
    printf("Postfix increment: %d\n", a++);
    printf("After postfix: %d\n", a);

    return 0;
}

Output:

Original: 5
Prefix increment: 6
Postfix increment: 6
After postfix: 7

Prefix vs. postfix

  • ++a increments a first, then produces the updated value.

  • a++ produces the original value, then increments a.

Avoid modifying the same variable multiple times within a single complex expression. Such expressions can lead to undefined behavior.

8. Conditional Operator (?:)

The conditional operator selects one of two expressions based on a condition.

Syntax

condition ? expression1 : expression2;

If the condition is true, expression1 is selected. Otherwise, expression2 is selected.

Example 6: Finding the Larger Number

#include <stdio.h>

int main(void)
{
    int a = 20;
    int b = 15;

    int larger = (a > b) ? a : b;

    printf("Larger number: %d\n", larger);

    return 0;
}

Output:

Larger number: 20

This is a compact alternative to a simple if-else statement.

9. Bitwise Operators

Bitwise operators work on the individual bits of integer operands.

Operator

Name

&

Bitwise AND

\|

Bitwise OR

^

Bitwise XOR

~

Bitwise complement

<<

Left shift

>>

Right shift

Example 7: Bitwise AND, OR, and XOR

#include <stdio.h>

int main(void)
{
    unsigned int a = 5;
    unsigned int b = 3;

    printf("AND: %u\n", a & b);
    printf("OR: %u\n", a | b);
    printf("XOR: %u\n", a ^ b);

    return 0;
}

The values in binary are:

a = 5  -> 0101
b = 3  -> 0011

Output:

AND: 1
OR: 7
XOR: 6

Explanation:

  • AND produces a 1 where both bits are 1.

  • OR produces a 1 where at least one bit is 1.

  • XOR produces a 1 where the bits differ.

Bitwise operators are used in embedded programming, flags, masks, and low-level data manipulation.

10. The sizeof Operator

The sizeof operator determines the size in bytes of a type or object.

Example 8

#include <stdio.h>

int main(void)
{
    int number = 100;
    double price = 99.5;

    printf("Size of number: %zu bytes\n", sizeof(number));
    printf("Size of price: %zu bytes\n", sizeof(price));

    return 0;
}

On a common modern system, the output may be:

Size of number: 4 bytes
Size of price: 8 bytes

The sizes depend on the implementation. %zu is used to print the size_t value returned by sizeof.

11. Operator Precedence

Operator precedence determines which operations are grouped first in an expression.

Consider:

int result = 10 + 5 * 2;

The multiplication is grouped before addition, so the result is 20, not 30.

Parentheses can make the intended order explicit:

int result = (10 + 5) * 2;

The result is 30.

Practical rule

When an expression contains several different operators, use parentheses to make your intended evaluation order clear.

12. Real-World Example: Simple Calculator

Let's combine arithmetic operators in a practical program.

#include <stdio.h>

int main(void)
{
    double a, b;

    printf("Enter two numbers: ");

    if (scanf("%lf %lf", &a, &b) != 2)
    {
        printf("Invalid input.\n");
        return 1;
    }

    printf("Addition: %.2f\n", a + b);
    printf("Subtraction: %.2f\n", a - b);
    printf("Multiplication: %.2f\n", a * b);

    if (b != 0.0)
    {
        printf("Division: %.2f\n", a / b);
    }
    else
    {
        printf("Division is not possible with a zero divisor.\n");
    }

    return 0;
}

Sample input:

Enter two numbers: 20 5

Output:

Addition: 25.00
Subtraction: 15.00
Multiplication: 100.00
Division: 4.00

Notice that %lf is used with scanf() to read double values.

13. Common Beginner Mistakes

  1. Using = instead of == in a comparison.

  2. Expecting 10 / 3 to produce a decimal result when both operands are integers.

  3. Dividing by zero.

  4. Confusing logical && with bitwise &.

  5. Confusing logical || with bitwise |.

  6. Forgetting the difference between prefix and postfix increment.

  7. Writing complicated expressions without parentheses.

  8. Using bitwise operators without understanding the integer representation and shift rules.

14. Practical Exercises

Write C programs to complete the following exercises:

  1. Perform addition, subtraction, multiplication, division, and remainder operations on two integers.

  2. Compare two numbers using relational operators.

  3. Check whether a person's age is between 18 and 60 using logical operators.

  4. Update a variable using +=, -=, and *=.

  5. Demonstrate the difference between prefix and postfix increment.

  6. Find the larger of two numbers using the conditional operator.

  7. Perform bitwise AND, OR, and XOR on two unsigned integers.

  8. Create a calculator that handles division by zero.

15. Interview Questions

Q1. What is an operator in C?

An operator specifies an operation performed on one or more operands.

Q2. What is the difference between = and ==?

= performs assignment, while == checks equality.

Q3. What is the difference between / and %?

/ performs division, while % produces the remainder for integer operands.

Q4. What is the difference between && and &?

&& performs logical AND with short-circuit evaluation; & performs bitwise AND on integer operands.

Q5. What is the difference between prefix and postfix increment?

Prefix increments before producing the value; postfix produces the original value before incrementing.

Q6. What is the conditional operator?

The ?: operator selects one of two expressions based on a condition.

Q7. What does operator precedence mean?

It determines how operators are grouped in an expression.

16. Quick Revision

  • Arithmetic operators perform calculations.

  • Relational operators compare values.

  • Logical operators combine or reverse conditions.

  • Assignment operators assign or update values.

  • ++ increments a value; -- decrements it.

  • The conditional operator uses condition ? value1 : value2.

  • Bitwise operators manipulate individual bits.

  • sizeof determines sizes in bytes.

  • Parentheses help clarify expressions.

  • Always validate input and guard against division by zero.

17. Homework

Create a Student Marks Calculator with these requirements:

  1. Ask the user to enter marks for three subjects.

  2. Calculate the total marks using arithmetic operators.

  3. Calculate the average.

  4. Use relational operators to check whether the student passed, assuming the passing mark is 40 in each subject.

  5. Use logical operators to determine whether all three subjects were passed.

  6. Display the results with clear labels.

  7. Validate all input values.

Expected output format

Enter marks for three subjects: 75 82 68

Total Marks: 225
Average: 75.00
Passed All Subjects: 1

For this exercise, assume each subject is marked out of 100 and a score of at least 40 is required to pass each subject.

Conclusion

In Module 3, you learned how to use arithmetic, relational, logical, assignment, increment, decrement, conditional, and bitwise operators in C programming.

←
Post 7 of 15
→