π§π» Operators in C Programming with example for beginners
π§π» Operators in C Programming
πExplained for Beginners
✨ Introduction
After learning variables, data types, and input/output in C, the next important concept is operators. "Operators are essential in C; without them, a program cannot perform calculations, comparisons, or logical decisions."
In this beginner-friendly guide, we will understand operators step by step with simple examples.
In this blog, you will learn:
π What are operators in C
➕ Arithmetic operators
π Relational operators
π§ Logical operators
π Assignment operators
π Increment and decrement operators
π§ͺ Example programs
π Practice questions
"This guide is tailored for beginners who are just starting with C programming."
1️⃣ What Are Operators in C?
Operators are special symbols used to perform operations on variables and values.
Example:
sum = a + b;
Here, + is an operator used to add two values, and a & b are operands.
2️⃣ Types of Operators in C
C programming supports many types of operators. The most important ones for beginners are:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
3️⃣ ➕ Arithmetic Operators in C
π Arithmetic Operators Table
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
π§ͺ Example Program
#include <stdio.h> // Includes the standard input-output library
int main() { // Main function: program execution starts here
int a = 10, b = 3; // Declare two integer variables and assign values
printf("Addition = %d", a + b); // Adds a and b, then prints the result
return 0; // Ends the program successfully
}
π€ Output:
Addition = 13
4️⃣ π Relational Operators in C
Relational operators help you compare two values. They are essential when you want your program to make decisions based on conditions, like checking if a number is greater or smaller than another.They return either true (1) or false (0).
π Relational Operators Table
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
== |
Equal to | a == b |
!= |
Not equal to | a != b |
π§ͺ Example
int a = 5, b = 3; // Declares two integer variables a and b with values 5 and 3
printf("%d", a > b); // Compares a and b and prints 1 if a is greater than b, otherwise 0
π€ Output:
1
(1 means condition is true)
5️⃣ π§ Logical Operators in C
π Logical Operators Table
| Operator | Meaning | Example |
|---|---|---|
&& |
Logical AND | (a > 0 && b > 0) |
|| |
Logical OR | (a > 0 || b > 0) |
! |
Logical NOT | !(a > b) |
π§ͺ Example
int a = 5, b = 3; // Declares two integer variables a and b and assigns values
printf("%d", (a > 0 && b > 0)); // Logical AND example
π€ Output:
1
6️⃣ π Assignment Operators in C
Assignment operators are used to store values in variables. They also include shortcuts to modify values efficiently, like adding or multiplying while assigning.
π Assignment Operators Table
| Operator | Meaning | Example |
|---|---|---|
= |
Assign | a = 10 |
+= |
Add and assign | a += 5 |
-= |
Subtract and assign | a -= 5 |
*= |
Multiply and assign | a *= 2 |
/= |
Divide and assign | a /= 2 |
π§ͺ Example
int a = 10; // Declares an integer variable 'a' and assigns value 10
a += 5; // Adds 5 to 'a' and stores the result back in 'a'
printf("%d", a); // Prints the updated value of 'a'
π€ Output:
15
7️⃣ π Increment and Decrement Operators
Increment and decrement operators are special operators used to increase or decrease a variable’s value by 1, which is often needed in loops and counting operations.
| Operator | Meaning | Example |
|---|---|---|
++ |
Increment | a++ |
-- |
Decrement | a-- |
π§ͺ Example
int a = 5; // Declares an integer variable 'a' and assigns value 5
a++; // Increments the value of 'a' by 1
printf("%d", a); // Prints the updated value of 'a'
π€ Output:
6
8️⃣ π§ͺ Complete Example Program Using Operators
#include <stdio.h> // Includes standard input-output functions like printf() and scanf()
int main() { // Main function: program execution starts here
int a, b; // Declares two integer variables a and b
printf("Enter two numbers: "); // Prompts the user to enter two numbers
scanf("%d %d", &a, &b); // Takes two integer inputs and stores them in variables a and b
printf("Sum = %d", a + b); // Adds a and b and prints their sum
return 0; // Ends the program successfully
}
9️⃣ π Practice Questions (With Answers)
✅ Question 1: Add Two Numbers
int a = 4, b = 6; // Declares two integer variables a and b and assigns values 4 and 6
printf("%d", a + b); // Adds a and b and prints the result
π€ Output:
10
✅ Question 2: Compare Two Numbers
int a = 5, b = 8; // Declares two integer variables a and b with values 5 and 8
printf("%d", a < b); // Compares a and b and prints 1 if a is less than b, otherwise 0
π€ Output:
1
Explanation: The condition a < b is true, so relational operators return 1 for true.
✅ Question 3: Logical Operator Example
π‘ Important beginner tip :
Relational operators in C do not print true/false words.
They print 1 for true and 0 for false.
π Summary of all Operators in C (Quick Reference)
✅ Conclusion:
In this blog, you learned:
✔ What operators are in C
✔ Different types of operators
✔ How to use operators with examples
Operators are the building blocks of logic in C programming. Mastering them will help you write clearer and more efficient programs.
Reflection: "Understanding operators is the first step toward thinking like a programmer."
✨ Written by Krishna Popat
π± Founder, Learning Growth Hub
Comments
Post a Comment