Operators in C - Complete Guide with Examples (2026 Beginner Guide)



In the previous article, we learned about variables and data types in C and Introduction to C Programming. We understood how to store different types of values like integers, floats, and characters inside variables and what is c.
But storing values alone is not enough. In programming, we need to:
  • Perform calculations
  • Compare values
  • Make decisions
  • Modify existing data
  • Combine conditions
To do all these tasks, we use operators.

 What Are Operators in C?

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

In simple words, Operators tell the computer what action to perform.

For example: a + b

Here:

a and b are operands (values or variables)

+ is the operator

The operator performs addition. So, an operator works on operands to produce a result

Without operators, we cannot:

  • Add two numbers
  • Check if two values are equal
  • Increase or decrease values
  • Apply logical conditions
  • Write decision-making statements
Every real program depends heavily on operators.

For example:

  • Calculator program → uses arithmetic operators
  • Login system → uses relational and logical operators
  • ATM program → uses comparison operators
  • Loop control → uses increment operators

So, operators are the foundation of logic building in C programming.

 Types of Operators in C

C programming provides several types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment and Decrement Operator
  • Bitwise Operators
  • Conditional (Ternary) Operator
  • Special Operators

Each type is used for a specific purpose. 

Let’s understand them step by step.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.


Example: 

int a = 10;
int b = 3;

int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int div = a / b; // 3
int mod = a % b; // 1

2. Relational Operators 

Relational operators are used to compare two values.

They return:

1 → True

0 → False






Example:
int a = 5;
int b = 10;

printf("%d", a < b); // Output: 1 (true)
printf("%d", a == b); // Output: 0 (false)

These operators are mainly used in:

  • if statements

  • loops

  • decision-making

3. Logical Operators

Logical operators combine multiple conditions.

AND (&&)

True only if both conditions are true.

if(a > 0 && b > 0)

OR (||)

True if at least one condition is true.

if(a > 0 || b > 0)

NOT (!)

Reverses the condition.

if(!(a > 0))

Logical operators are very important in:

  • Login systems

  • Eligibility checks

  • Validation programs

4. Assignment Operators

Used to assign values to variables.
int a = 5;




Example:
int a = 10;
a += 5; // a = a + 5 → 15
a -= 3; // a = a - 3 → 12
These help make code shorter and cleaner.

 5. Increment and Decrement Operators

Used to increase or decrease value by 1.


Example:

int a = 5;
a++; // 6
a--; // 5

There are two types:

  • Pre-increment → ++a

  • Post-increment → a++

This is very important in loops.

 6. Bitwise Operators 

Used to perform operations at binary level.

Mostly used in:

  • Embedded systems

  • Low-level programming

 7. Conditional (Ternary) Operator

It is a short form of if-else.

Syntax:

condition? expression1: expression2;

Example:

int a = 20, b = 30;
int max = (a > b) ? a : b;

If condition is true → first value
If false → second value

Special Operators

In C, apart from arithmetic, relational, and logical operators, there are some special operators used for specific purposes.

These operators help in:

  • Finding memory size

  • Accessing structure members

  • Accessing pointer members

  • Writing compact expressions

Let’s understand each one clearly.

Types of Special Operators in C

  1. sizeof operator

  2. Comma , operator

  3. Pointer operator *

  4. Address-of operator &

  5. Structure member operator .

  6. Structure pointer operator ->

  7. Conditional (Ternary) operator ?:

 1. sizeof Operator

The sizeof operator is used to find the size (in bytes) of a variable or data type.

Syntax:

sizeof(variable);
sizeof(data_type);

#include <stdio.h> int main() { int a; float b; char c; printf("Size of int: %lu\n", sizeof(a)); printf("Size of float: %lu\n", sizeof(b)); printf("Size of char: %lu\n", sizeof(c)); return 0; }

Output (may vary by system):

Size of int: 4
Size of float: 4
Size of char: 1

Very useful in:

  • Memory management

  • Dynamic memory allocation

  • Understanding system architecture

2. Comma Operator (,)

The comma operator allows multiple expressions in a single statement.

It evaluates expressions from left to right, and returns the value of the last expression.

Example:

#include <stdio.h> int main() { int a; a = (5, 10, 15); printf("%d", a); return 0; }

Output:

15

Because the last value is returned.

It is rarely used but important to understand.

3. Address-of Operator (&)

The & operator is used to get the memory address of a variable.

Example:

#include <stdio.h>

int main() {
int a = 10;

printf("Value of a: %d\n", a);
printf("Address of a: %p\n", &a);

return 0;
}

This is mainly used with:

  • Pointers

  • scanf() function

Example in input:

scanf("%d", &a);

4. Pointer Operator (*)

The * operator is used:

  1. To declare a pointer

  2. To access value stored at a memory address

Example:

#include <stdio.h>

int main() {
int a = 10;
int *ptr;

ptr = &a;

printf("Value of a: %d\n", *ptr);

return 0;
}

Here:

  • ptr stores address of a

  • *ptr gives value stored at that address

5. Structure Member Operator (.)

Used to access members of a structure variable.

Example:

#include <stdio.h>

struct Student {
int roll;
float marks;
};

int main() {
struct Student s1;

s1.roll = 1;
s1.marks = 85.5;

printf("Roll: %d\n", s1.roll);
printf("Marks: %.2f\n", s1.marks);

return 0;
}

The dot (.) is used to access structure elements.

6.Structure Pointer Operator (->)

Used to access structure members using a pointer.

Example:

#include <stdio.h>

struct Student {
int roll;
};

int main() {
struct Student s1;
struct Student *ptr = &s1;

ptr->roll = 5;

printf("Roll: %d", ptr->roll);

return 0;
}

Instead of writing:

(*ptr).roll

We write:

ptr->roll

It makes code cleaner.

Special operators in C help in:

✔ Memory handling
✔ Pointer operations
✔ Structure handling
✔ Writing efficient expressions

They are especially important when learning:

  • Pointers

  • Structures

  • Dynamic memory allocation

Operators in C allow us to:

✔ Perform calculations
✔ Compare values
✔ Make logical decisions
✔ Modify variable values
✔ Control program flow

Without operators, programming is impossible.

💻 C Programs Based on Operators

Write a C program to perform addition, subtraction, multiplication and division of two numbers.

#include <stdio.h> int main() { int a = 20, 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); return 0; }

Output:

Addition: 25
Subtraction: 15
Multiplication: 100
Division: 4

Write a program to check whether a number is even or odd using modulus operator.

#include <stdio.h>

int main() {
int num = 7;

if(num % 2 == 0)
printf("Even Number");
else
printf("Odd Number");

return 0;
}

Output:
Odd Number

Find Largest of Two Numbers (Ternary Operator)

#include <stdio.h>

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

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

printf("Largest number: %d", max);

return 0;
}
Output:
Largest number: 20

Increment and Decrement Example

#include <stdio.h>

int main() {
int a = 5;

printf("Original value: %d\n", a);
printf("After increment: %d\n", ++a);
printf("After decrement: %d\n", --a);

return 0;
}

Output:

Original value: 5
After increment: 6
After decrement: 5
Using sizeof Operator
#include <stdio.h> int main() { printf("Size of int: %lu bytes\n", sizeof(int)); printf("Size of float: %lu bytes\n", sizeof(float)); printf("Size of char: %lu bytes\n", sizeof(char)); return 0; }
Output (may vary):
Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 byte
📝 5 Practice Programming Questions (For Students)

🔹 Practice Questions

  1. Write a C program to check whether a number is positive, negative, or zero.

  2. Write a C program to swap two numbers using a temporary variable.

  3. Write a C program to find the largest of three numbers using if-else.

  4. Write a C program to calculate the area of a rectangle using arithmetic operators

  5. Write a C program to check whether a number is divisible by both 3 and 5 using logical operators.

❓ FAQ Section

What are operators in C?

Operators in C are special symbols used to perform operations on variables and values such as addition,

comparison, and logical evaluation.

How many types of operators are there in C?

There are several types of operators in C including arithmetic, relational, logical, assignment,

increment/decrement, bitwise, and special operators

What is the difference between == and = in C?

= is an assignment operator (used to assign value).

== is a relational operator (used to compare values).

What does sizeof operator do in C?

The sizeof operator returns the size (in bytes) of a data type or variable.

What is the ternary operator in C?

The ternary operator ?: is a shorthand version of if-else used to evaluate a

condition in a single line.

Operators are essential in C programming because they help us perform calculations, make decisions, and

control program flow. Mastering operators is important before moving to control statements and loops.

Related Posts:

Comments

Post a Comment

Popular posts from this blog