In C programming, operators are special symbols that perform operations on variables and values. They are categorized based on the type of operations they perform. Understanding operators is crucial for writing efficient and effective code in C. Below is an overview of the different types of operators in C:
1. Arithmetic Operators
These operators are used to perform basic mathematical operations.
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // 13
printf("Subtraction: %d\n", a - b); // 7
printf("Multiplication: %d\n", a * b); // 30
printf("Division: %d\n", a / b); // 3
printf("Modulus: %d\n", a % b); // 1
2. Relational Operators
These operators are used to compare two values and return a boolean result (true or false).
int a = 10, b = 20;
printf("Equal: %d\n", a == b); // 0 (false)
printf("Not Equal: %d\n", a != b); // 1 (true)
printf("Greater: %d\n", a > b); // 0 (false)
3. Logical Operators
These operators are used to perform logical operations, often used in conditional statements.
int a = 1, b = 0;
printf("Logical AND: %d\n", a && b); // 0 (false)
printf("Logical OR: %d\n", a || b); // 1 (true)
printf("Logical NOT: %d\n", !a); // 0 (false)
4. Bitwise Operators
These operators perform bit-level operations on integer types.
int a = 5, b = 3; // 5 = 0101, 3 = 0011 in binary
printf("Bitwise AND: %d\n", a & b); // 1 (0001)
printf("Bitwise OR: %d\n", a | b); // 7 (0111)
printf("Bitwise XOR: %d\n", a ^ b); // 6 (0110)
printf("Bitwise NOT: %d\n", ~a); // -6 (inverts bits)
5. Assignment Operators
These operators are used to assign values to variables.
int a = 10;
a += 5; // a = a + 5
printf("After += : %d\n", a); // 15
a *= 2; // a = a * 2
printf("After *= : %d\n", a); // 30
6. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by one.
int a = 10;
printf("Pre-Increment: %d\n", ++a); // 11
printf("Post-Increment: %d\n", a++); // 11 (then a becomes 12)
printf("Current Value: %d\n", a); // 12
7. Conditional (Ternary) Operator
The conditional operator is a shorthand way of expressing a simple if-else
statement.
int a = 10, b = 20;
int max = (a > b) ? a : b; // If a > b, max = a, else max = b
printf("Max: %d\n", max); // 20
8. Sizeof Operator
The sizeof
operator is used to determine the size (in bytes) of a data type or variable.
int a;
printf("Size of int: %zu bytes\n", sizeof(a)); // Typically 4 bytes
printf("Size of float: %zu bytes\n", sizeof(float)); // Typically 4 bytes
Summary
Operators in C are essential for performing a wide variety of tasks, from mathematical calculations to logical comparisons. Understanding how to use these operators effectively will help you write efficient and clear C programs.
Final Example
Here's a simple program that demonstrates various types of operators
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Arithmetic Operators
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
// Relational Operators
printf("Equal: %d\n", a == b);
// Logical Operators
printf("Logical AND: %d\n", a && b);
// Bitwise Operators
printf("Bitwise AND: %d\n", a & b);
// Assignment Operators
a += 5; // a = a + 5
printf("After += : %d\n", a);
// Increment and Decrement Operators
printf("Pre-Increment: %d\n", ++a);
// Conditional Operator
int max = (a > b) ? a : b;
printf("Max: %d\n", max);
// Sizeof Operator
printf("Size of int: %zu bytes\n", sizeof(int));
return 0;
}
Output
Addition: 30
Subtraction: -10
Equal: 0
Logical AND: 1
Bitwise AND: 0
After += : 15
Pre-Increment: 16
Max: 20
Size of int: 4 bytes
This program showcases various operators in action, demonstrating their functionality and how they can be utilized in a C program. Understanding these operators will significantly enhance your programming skills in C.