Operators in C are symbols that perform operations on variables and values. C
supports a variety of operators that allow you to manipulate data, perform
arithmetic, logical operations, and more. Understanding how to use operators
effectively is essential to writing functional and efficient code.
1. Types of Operators in C
Operators in C can be categorized into several types:
1.1 Arithmetic Operators
These operators perform basic arithmetic operations like addition, subtraction,
multiplication, division, and modulus (remainder).
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulus (remainder of division)
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // 10 + 3 = 13
printf("Subtraction: %d\n", a - b); // 10 - 3 = 7
printf("Multiplication: %d\n", a * b); // 10 * 3 = 30
printf("Division: %d\n", a / b); // = 3 (integer division)
printf("Modulus: %d\n", a % b); // 10 % 3 = 1
return 0;
}
, 1.2 Relational Operators
These operators are used to compare two values. They return either true (1) or
false (0) based on the result of the comparison.
==: Equal to
!=: Not equal to
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
Example:
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("x == y: %d\n", x == y); // 5 == 10 (False)
printf("x != y: %d\n", x != y); // 5 != 10 (True)
printf("x > y: %d\n", x > y); // 5 > 10 (False)
printf("x < y: %d\n", x < y); // 5 < 10 (True)
return 0;
}
1.3 Logical Operators
Logical operators are used to perform logical operations on expressions. They are
often used in decision-making structures like if statements.
&&: Logical AND
||: Logical OR
!: Logical NOT
Example:
#include <stdio.h>