Operators in C - Complete Guide with Examples (2026 Beginner Guide)
- Perform calculations
- Compare values
- Make decisions
- Modify existing data
- Combine conditions
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
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; // 13int diff = a - b; // 7int product = a * b; // 30int div = a / b; // 3int mod = a % b; // 1
2. Relational Operators
Relational operators are used to compare two values.
They return:
1 → True
0 → False
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
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
a += 5; // a = a + 5 → 15
a -= 3; // a = a - 3 → 12
5. Increment and Decrement Operators
a++; // 6
a--; // 5
There are two types:
-
Pre-increment → ++a
-
Post-increment → a++
This is very important in loops.
6. Bitwise Operators
Mostly used in:
-
Embedded systems
-
Low-level programming
7. Conditional (Ternary) Operator
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
-
sizeofoperator -
Comma
,operator -
Pointer operator
* -
Address-of operator
& -
Structure member operator
. -
Structure pointer operator
-> -
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: 1Very 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:
15Because 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:
To declare a pointer
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:
ptrstores address ofa
*ptrgives value stored at that address5. 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).rollWe write:
ptr->rollIt makes code cleaner.
Special operators in C help in:
✔ Memory handling
✔ Pointer operations
✔ Structure handling
✔ Writing efficient expressionsThey 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 flowWithout 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: 4Write 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 NumberFind 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: 20Increment 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: 5Using 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
Write a C program to check whether a number is positive, negative, or zero.
Write a C program to swap two numbers using a temporary variable.
Write a C program to find the largest of three numbers using if-else.
Write a C program to calculate the area of a rectangle using arithmetic operators
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).Operators are essential in C programming because they help us perform calculations, make decisions, andWhat does sizeof operator do in C?
The
sizeofoperator 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 acondition in a single line.
control program flow. Mastering operators is important before moving to control statements and loops.







Helping to brush knowledge.
ReplyDeleteGood
ReplyDelete👍👍
ReplyDelete