C Operators

What are Operators in C?

Operators are symbols used to perform operations on variables and values.

Operators help in calculations, comparisons, and logical decisions.

Examples of operators are +, -, *, /, %, ==, >, <, &&, ||.

Why Operators are Important?

Operators are important because they:

• Perform mathematical calculations.

• Compare values and conditions.

• Help make decisions in programs.

• Reduce code complexity and improve efficiency.

Types of Operators in C

C provides different types of operators for different purposes.

The main types of operators are:

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Assignment Operators

• Increment and Decrement Operators

• Bitwise Operators

• Conditional (Ternary) Operator

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Common arithmetic operators are:

• + (Addition)

• – (Subtraction)

• * (Multiplication)

• / (Division)

• % (Modulus – Remainder)

Example Program

Program

#include <stdio.h>

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

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(“Modulus = %d\n”, a % b);

return 0;
}

Output

Addition = 15

Subtraction = 5

Multiplication = 50

Division = 2

Modulus = 0

Explanation

• int a = 10, b = 5; → Declares and initializes two integer variables.

• a + b → Performs addition and gives 15.

• a – b → Performs subtraction and gives 5.

• a * b → Performs multiplication and gives 50.

• a / b → Performs division and gives 2.

• a % b → Returns the remainder after division, which is 0.

• printf() → Displays the result on the screen.

• return 0; → Ends the program successfully.

Relational Operators

Relational operators are used to compare two values.

These operators return either True (1) or False (0).

Common relational operators are:

• == (Equal to)

• != (Not Equal to)

• > (Greater Than)

• < (Less Than)

• >= (Greater Than or Equal To)

• <= (Less Than or Equal To)

Example Program

Program

#include <stdio.h>

int main()
{
int a = 10, b = 20;

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 : 0

a < b : 1

Explanation

• a == b → Checks whether both values are equal.

• a != b → Checks whether values are different.

• a > b → Checks whether a is greater than b.

• a < b → Checks whether a is less than b.

• Output 1 means True.

• Output 0 means False.

• Relational operators are mainly used in decision-making statements such as if, if-else, and loops.

Logical Operators

Logical operators are used to combine multiple conditions.

They return either True (1) or False (0).

Common logical operators are:

• && (Logical AND)

• || (Logical OR)

• ! (Logical NOT)

Example Program

Program

#include <stdio.h>

int main()
{
int a = 10, b = 20;

printf(“(a < b && a > 5) = %d\n”, (a < b && a > 5));
printf(“(a > b || a < 15) = %d\n”, (a > b || a < 15));
printf(“!(a > b) = %d\n”, !(a > b));

return 0;
}

Output

(a < b && a > 5) = 1

(a > b || a < 15) = 1

!(a > b) = 1

Explanation

• && returns True only if both conditions are True.

• || returns True if at least one condition is True.

• ! reverses the result of a condition.

• Logical operators are widely used in decision-making and loops.

• Output 1 means True.

• Output 0 means False.

Assignment Operators

Assignment operators are used to assign values to variables.

The most commonly used assignment operators are:

• = (Assign)

• += (Add and Assign)

• -= (Subtract and Assign)

• *= (Multiply and Assign)

• /= (Divide and Assign)

Example Program

Program

#include <stdio.h>

int main()
{
int a = 10;

a += 5;
printf(“a += 5 : %d\n”, a);

a -= 3;
printf(“a -= 3 : %d\n”, a);

a *= 2;
printf(“a *= 2 : %d\n”, a);

a /= 4;
printf(“a /= 4 : %d\n”, a);

return 0;
}

Output

a += 5 : 15

a -= 3 : 12

a *= 2 : 24

a /= 4 : 6

Explanation

• = assigns a value to a variable.

• += adds a value and stores the result.

• -= subtracts a value and stores the result.

• *= multiplies a value and stores the result.

• /= divides a value and stores the result.

• Assignment operators make code shorter and easier to read.

Increment and Decrement Operators

Increment and Decrement operators are used to increase or decrease the value of a variable by 1.

The two operators are:

• ++ (Increment Operator)

• — (Decrement Operator)

Increment increases the value by 1.

Decrement decreases the value by 1.

Example Program

Program

#include <stdio.h>

int main()
{
int a = 10;

a++;
printf(“After Increment : %d\n”, a);

a–;
printf(“After Decrement : %d\n”, a);

return 0;
}

Output

After Increment : 11

After Decrement : 10

Explanation

• int a = 10; → Initializes variable a with value 10.

• a++; → Increases the value of a by 1.

• a–; → Decreases the value of a by 1.

• printf() → Displays the result on the screen.

• Increment operator (++) adds 1 to the variable.

• Decrement operator (–) subtracts 1 from the variable.

Bitwise Operators

Bitwise operators perform operations on the binary representation of numbers.

Common bitwise operators are:

• & (Bitwise AND)

• | (Bitwise OR)

• ^ (Bitwise XOR)

• ~ (Bitwise NOT)

• << (Left Shift)

• >> (Right Shift)

Example Program

Program

#include <stdio.h>

int main()
{
int a = 5, b = 3;

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 = 1

a | b = 7

a ^ b = 6

Explanation

• & performs Bitwise AND operation.

• | performs Bitwise OR operation.

• ^ performs Bitwise XOR operation.

• Bitwise operators work directly on binary values.

• They are commonly used in low-level programming and embedded systems.

Conditional (Ternary) Operator

The Conditional (Ternary) Operator is a shorthand form of the if-else statement.

It is represented by ? :

Syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is executed.

If the condition is false, expression2 is executed.

Example Program

Program

#include <stdio.h>

int main()
{
int age = 18;

(age >= 18) ? printf(“Eligible to Vote”) : printf(“Not Eligible”);

return 0;
}

Output

Eligible to Vote

Explanation

• age >= 18 checks the condition.

• ? is used to separate the true expression.

• : is used to separate the false expression.

• Since age is 18, the condition is true.

• Therefore, “Eligible to Vote” is displayed.

• The ternary operator makes code shorter and easier to read.

Key Points

• Operators are used to perform operations on variables and values.

• Arithmetic operators perform mathematical calculations.

• Relational operators compare values and return True (1) or False (0).

• Logical operators combine multiple conditions.

• Assignment operators assign values to variables.

• Increment and Decrement operators increase or decrease values by 1.

• Bitwise operators work on binary values.

• The Conditional (Ternary) Operator is a shorthand form of if-else.