Introduction
In the previous chapter, we learned about C constants and literals. In this chapter, we will learn about C operators. Operators are special symbols that perform operations on variables and values. They are essential for performing calculations, making comparisons, and manipulating data in C programs.
Types of Operators
C provides a rich set of operators, which can be classified into the following categories:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
- Addition (
+
): Adds two operands. - Subtraction (
-
): Subtracts the second operand from the first. - Multiplication (
*
): Multiplies two operands. - Division (
/
): Divides the first operand by the second. - Modulus (
%
): Returns the remainder of the division of the first operand by the second.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b); // Addition
printf("a - b = %d\n", a - b); // Subtraction
printf("a * b = %d\n", a * b); // Multiplication
printf("a / b = %d\n", a / b); // Division
printf("a %% b = %d\n", a % b); // Modulus (%% is used to print % symbol)
return 0; // Returning 0 to indicate successful execution
}
Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
2. Relational Operators
Relational operators are used to compare two values. They return either true (1) or false (0).
- Equal to (
==
): Checks if two operands are equal. - Not equal to (
!=
): Checks if two operands are not equal. - Greater than (
>
): Checks if the first operand is greater than the second. - Less than (
<
): Checks if the first operand is less than the second. - Greater than or equal to (
>=
): Checks if the first operand is greater than or equal to the second. - Less than or equal to (
<=
): Checks if the first operand is less than or equal to the second.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b: %d\n", a == b); // Equal to
printf("a != b: %d\n", a != b); // Not equal to
printf("a > b: %d\n", a > b); // Greater than
printf("a < b: %d\n", a < b); // Less than
printf("a >= b: %d\n", a >= b); // Greater than or equal to
printf("a <= b: %d\n", a <= b); // Less than or equal to
return 0; // Returning 0 to indicate successful execution
}
Output:
a == b: 0
a != b: 1
a > b: 0
a < b: 1
a >= b: 0
a <= b: 1
3. Logical Operators
Logical operators are used to combine multiple conditions or expressions.
- Logical AND (
&&
): Returns true if both operands are true. - Logical OR (
||
): Returns true if at least one of the operands is true. - Logical NOT (
!
): Returns true if the operand is false, and vice versa.
Example:
#include <stdio.h>
int main() {
int a = 1, b = 0;
printf("a && b: %d\n", a && b); // Logical AND
printf("a || b: %d\n", a || b); // Logical OR
printf("!a: %d\n", !a); // Logical NOT
return 0; // Returning 0 to indicate successful execution
}
Output:
a && b: 0
a || b: 1
!a: 0
4. Bitwise Operators
Bitwise operators are used to perform operations on individual bits of integer values.
- Bitwise AND (
&
): Performs a logical AND operation on each bit. - Bitwise OR (
|
): Performs a logical OR operation on each bit. - Bitwise XOR (
^
): Performs a logical XOR operation on each bit. - Bitwise NOT (
~
): Inverts all the bits. - Left shift (
<<
): Shifts bits to the left. - Right shift (
>>
): Shifts bits to the right.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3; // a = 0101, b = 0011 in binary
printf("a & b: %d\n", a & b); // Bitwise AND
printf("a | b: %d\n", a | b); // Bitwise OR
printf("a ^ b: %d\n", a ^ b); // Bitwise XOR
printf("~a: %d\n", ~a); // Bitwise NOT
printf("a << 1: %d\n", a << 1); // Left shift
printf("a >> 1: %d\n", a >> 1); // Right shift
return 0; // Returning 0 to indicate successful execution
}
Output:
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
5. Assignment Operators
Assignment operators are used to assign values to variables.
- Assign (
=
): Assigns the value of the right operand to the left operand. - Add and assign (
+=
): Adds the right operand to the left operand and assigns the result to the left operand. - Subtract and assign (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiply and assign (
*=
): Multiplies the right operand with the left operand and assigns the result to the left operand. - Divide and assign (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus and assign (
%=
): Takes the modulus of the left operand by the right operand and assigns the result to the left operand.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
a += b; // Equivalent to a = a + b
printf("a += b: %d\n", a);
a -= b; // Equivalent to a = a - b
printf("a -= b: %d\n", a);
a *= b; // Equivalent to a = a * b
printf("a *= b: %d\n", a);
a /= b; // Equivalent to a = a / b
printf("a /= b: %d\n", a);
a %= b; // Equivalent to a = a % b
printf("a %%= b: %d\n", a); // %% is used to print % symbol
return 0; // Returning 0 to indicate successful execution
}
Output:
a += b: 15
a -= b: 10
a *= b: 50
a /= b: 10
a %= b: 0
6. Miscellaneous Operators
Miscellaneous operators include several useful operators in C.
- Sizeof (
sizeof
): Returns the size of a data type or variable. - Comma (
,
): Used to separate expressions. The left expression is evaluated first, and the result of the right expression is returned. - Pointer (
*
): Used to declare and dereference pointers. - Address-of (
&
): Returns the memory address of a variable.
Example:
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a; // Pointer to integer a
printf("Size of int: %zu bytes\n", sizeof(int)); // Sizeof operator
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a); // Address-of operator
printf("Value at ptr: %d\n", *ptr); // Pointer dereference operator
int x = 1, y = 2;
int result = (x, y); // Comma operator
printf("Result of comma operator: %d\n", result);
return 0; // Returning 0 to indicate successful execution
}
Output:
Size of int: 4 bytes
Value of a: 10
Address
of a: 0x7ffee7b5c5ac
Value at ptr: 10
Result of comma operator: 2
Conclusion
Understanding and using operators effectively is crucial for writing efficient and powerful C programs. Operators allow you to perform calculations, make comparisons, manipulate data, and control the flow of your program. By mastering these operators, you can enhance your programming skills and write more sophisticated code.