Operators in C Programming (Arithmetic, Relational, Logical, Assignment)
This tutorial explains operators in C programming, which are used to perform operations on variables and values. It covers arithmetic, relational, logical, and assignment operators with syntax and examples, helping beginners understand how expressions and conditions work in C programs.
1. What Are Operators in C
Operators are symbols that perform operations on operands (variables or values).
Example:
int sum = a + b;
Here, + is an operator.
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operators List
| OperatorDescription | |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
Example Program
#include <stdio.h>
int main() {
int a = 10, 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("Modulus: %d\n", a % b);
return 0;
}
3. Relational Operators
Relational operators are used to compare two values and return true (1) or false (0).
Operators List
| OperatorMeaning | |
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example
#include <stdio.h>
int main() {
int x = 10, y = 20;
printf("%d\n", x == y);
printf("%d\n", x < y);
printf("%d\n", x >= y);
return 0;
}
4. Logical Operators
Logical operators are used to combine multiple conditions.
Operators List
| OperatorMeaning | |
&& | Logical AND |
| ` | |
! | Logical NOT |
Example
#include <stdio.h>
int main() {
int age = 25;
int salary = 30000;
if (age > 18 && salary > 20000) {
printf("Eligible\n");
}
return 0;
}
5. Assignment Operators
Assignment operators are used to assign values to variables.
Operators List
| OperatorDescription | |
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Example
#include <stdio.h>
int main() {
int a = 10;
a += 5;
printf("a = %d\n", a);
a *= 2;
printf("a = %d\n", a);
return 0;
}
6. Operator Precedence (Basic)
Operators with higher precedence are evaluated first.
Example:
int result = 10 + 5 * 2; // Result is 20
Key Points to Remember
- Arithmetic operators perform calculations
- Relational operators compare values
- Logical operators combine conditions
- Assignment operators update variable values
- Understand precedence to avoid logical errors