-
Hajipur, Bihar, 844101
In C programming, operators are symbols that tell the compiler to perform specific operations on data. Operators are essential because they allow you to manipulate variables and constants, perform calculations, compare values, and control the flow of a program. Understanding operators is critical for writing functional and efficient C programs.
This chapter explains the different types of operators in C, how they work, and provides practical examples for each.
An operator is a symbol that performs operations on operands. Operands can be constants, variables, or expressions. For example, in the expression:
int sum = a + b;
+ is the operator.
a and b are operands.
The operation adds the values of a and b and stores the result in sum.
C provides several categories of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
Conditional (Ternary) Operator
Bitwise Operators
Comma Operator
Sizeof Operator
Arithmetic operators are used for mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus | a % b |
Example:
#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;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Relational operators compare two values and return 1 (true) or 0 (false).
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater or equal | a >= b |
<= |
Less or equal | a <= b |
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a > b: %d\n", a > b);
printf("a <= b: %d\n", a <= b);
return 0;
}
Logical operators are used to combine multiple conditions.
| Operator | Meaning | Example |
|---|---|---|
&& |
Logical AND | (a > 0 && b > 0) |
| ` | ` | |
! |
Logical NOT | !(a == b) |
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a > 0 && b > 0) {
printf("Both numbers are positive.\n");
}
if (a > 0 || b < 0) {
printf("At least one condition is true.\n");
}
if (!(a == b)) {
printf("a and b are not equal.\n");
}
return 0;
}
Assignment operators assign values to variables. The basic operator is =, but there are compound assignment operators as well:
| Operator | Meaning | Example |
|---|---|---|
= |
Assign | a = b |
+= |
Add and assign | a += b |
-= |
Subtract and assign | a -= b |
*= |
Multiply and assign | a *= b |
/= |
Divide and assign | a /= b |
%= |
Modulus and assign | a %= b |
Example:
int a = 10;
a += 5; // a = 15
++ increases a variable by 1, and -- decreases it by 1. They can be prefix or postfix:
Prefix (++a): Increment before use
Postfix (a++): Increment after use
Example:
int a = 5;
printf("%d\n", ++a); // 6
printf("%d\n", a++); // 6
printf("%d\n", a); // 7
The ternary operator ? : allows for a compact if-else statement:
result = (condition) ? value_if_true : value_if_false;
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
Bitwise operators perform operations at the binary level:
| Operator | Meaning | Example |
|---|---|---|
& |
AND | a & b |
| ` | ` | OR |
^ |
XOR | a ^ b |
~ |
NOT | ~a |
<< |
Left shift | a << 1 |
>> |
Right shift | a >> 1 |
The comma operator , allows multiple expressions in a single statement:
int a, b, c;
a = (b = 5, b + 2); // b=5, a=b+2=7
sizeof returns the size of a data type or variable in bytes:
int a;
printf("Size of int: %lu\n", sizeof(a));
Understand operator precedence:
Some operators are evaluated before others. Use parentheses for clarity.
Use meaningful expressions:
Avoid overly complex expressions in a single line.
Be careful with division and modulus:
Division between integers truncates decimals; modulus only works with integers.
Use logical operators correctly:
Ensure conditions are properly grouped with parentheses to avoid unexpected results.
Operators in C are symbols that perform operations on variables and constants. C provides arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, comma, and sizeof operators. Understanding how to use these operators effectively is essential for writing functional and readable programs. Proper use of operators allows you to perform calculations, compare values, make decisions, and manipulate data efficiently.
What is an operator in C, and how does it differ from an operand?
List the arithmetic operators in C and give an example of each.
Write a C program to find the remainder when 25 is divided by 4 using a modulus operator.
Explain the difference between == and = operators with examples.
How does the logical AND (&&) operator work? Give a practical example.
What is the difference between prefix and postfix increment operators? Illustrate with code.
Write a C program using the ternary operator to find the larger of two numbers.
Explain the purpose of bitwise operators and give an example using & or |.
What does the sizeof operator do, and why is it useful?
Identify the error in the following statement:
int a = 5, b = 10, c;
c = a + b, 20;