In C programming, operators are symbols that perform operations on variables and values. Operators in C can be classified into several categories based on their functionality.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations. The most commonly used arithmetic operators in C are:
- Addition (
+
): Adds two operands. - Subtraction (
-
): Subtracts the right operand from the left operand. - Multiplication (
*
): Multiplies two operands. - Division (
/
): Divides the left operand by the right operand. - Modulus (
%
): Returns the remainder of the division of the left operand by the right operand.
Example:
#include <stdio.h> int main() { int a = 10, b = 5; printf("a + b = %d\n", a + b); // Output: 15 printf("a - b = %d\n", a - b); // Output: 5 printf("a * b = %d\n", a * b); // Output: 50 printf("a / b = %d\n", a / b); // Output: 2 printf("a %% b = %d\n", a % b); // Output: 0 return 0; }
2. Relational Operators
Relational operators are used to compare two values. They return a boolean value (true
or false
), where true
is typically represented as 1
and false
as 0
.
- Equal to (
==
): Returns true if the operands are equal. - Not equal to (
!=
): Returns true if the operands are not equal. - Greater than (
>
): Returns true if the left operand is greater than the right operand. - Less than (
<
): Returns true if the left operand is less than the right operand. - Greater than or equal to (
>=
): Returns true if the left operand is greater than or equal to the right operand. - Less than or equal to (
<=
): Returns true if the left operand is less than or equal to the right operand.
Example:
#include <stdio.h> int main() { int a = 10, b = 5; printf("a == b: %d\n", a == b); // Output: 0 (false) printf("a != b: %d\n", a != b); // Output: 1 (true) printf("a > b: %d\n", a > b); // Output: 1 (true) printf("a < b: %d\n", a < b); // Output: 0 (false) printf("a >= b: %d\n", a >= b); // Output: 1 (true) printf("a <= b: %d\n", a <= b); // Output: 0 (false) return 0; }
3. Logical Operators
Logical operators are used to perform logical operations, often with boolean values. These operators are commonly used in conditional statements.
- Logical AND (
&&
): Returns true if both operands are true. - Logical OR (
||
): Returns true if at least one of the operands is true. - Logical NOT (
!
): Reverses the logical state of its operand (i.e., returns true if the operand is false, and false if it is true).
Example:
#include <stdio.h> int main() { int a = 1, b = 0; printf("a && b: %d\n", a && b); // Output: 0 (false) printf("a || b: %d\n", a || b); // Output: 1 (true) printf("!a: %d\n", !a); // Output: 0 (false) printf("!b: %d\n", !b); // Output: 1 (true) return 0; }
4. Bitwise Operators
Bitwise operators are used to perform operations on individual bits of data. These are mainly used for low-level programming tasks.
- AND (
&
): Performs bitwise AND operation between two integers. - OR (
|
): Performs bitwise OR operation between two integers. - XOR (
^
): Performs bitwise XOR operation between two integers. - NOT (
~
): Inverts all the bits of an integer. - Left Shift (
<<
): Shifts the bits of an integer to the left. - Right Shift (
>>
): Shifts the bits of an integer to the right.
Example:
#include <stdio.h> int main() { int a = 5, b = 3; printf("a & b = %d\n", a & b); // Output: 1 (0101 & 0011 = 0001) printf("a | b = %d\n", a | b); // Output: 7 (0101 | 0011 = 0111) printf("a ^ b = %d\n", a ^ b); // Output: 6 (0101 ^ 0011 = 0110) printf("~a = %d\n", ~a); // Output: -6 (~0101 = 1010, two's complement) printf("a << 1 = %d\n", a << 1); // Output: 10 (0101 << 1 = 1010) printf("a >> 1 = %d\n", a >> 1); // Output: 2 (0101 >> 1 = 0010) return 0; }
5. Assignment Operators
Assignment operators are used to assign values to variables.
- Simple assignment (
=
): Assigns the right-hand operand to the left-hand operand. - Add and assign (
+=
): Adds the right-hand operand to the left-hand operand and assigns the result to the left operand. - Subtract and assign (
-=
): Subtracts the right-hand operand from the left-hand operand and assigns the result to the left operand. - Multiply and assign (
*=
): Multiplies the right-hand operand by the left-hand operand and assigns the result to the left operand. - Divide and assign (
/=
): Divides the left-hand operand by the right-hand operand and assigns the result to the left operand. - Modulus and assign (
%=
): Takes the modulus of the left-hand operand by the right-hand operand and assigns the result to the left operand.
Example:
#include <stdio.h> int main() { int a = 10; a += 5; // a = a + 5 printf("a += 5: %d\n", a); // Output: 15 a -= 3; // a = a - 3 printf("a -= 3: %d\n", a); // Output: 12 a *= 2; // a = a * 2 printf("a *= 2: %d\n", a); // Output: 24 a /= 4; // a = a / 4 printf("a /= 4: %d\n", a); // Output: 6 a %= 5; // a = a % 5 printf("a %%= 5: %d\n", a); // Output: 1 return 0; }
6. Increment and Decrement Operators
The increment and decrement operators are used to increase or decrease the value of a variable by 1.
- Increment (
++
): Increases the value of a variable by 1. - Decrement (
--
): Decreases the value of a variable by 1.
These operators can be used in two forms:
- Pre-increment/Decrement (
++a
,--a
): Increases or decreases the value before using it in the expression. - Post-increment/Decrement (
a++
,a--
): Increases or decreases the value after using it in the expression.
Example:
#include <stdio.h> int main() { int a = 5; printf("Pre-increment: %d\n", ++a); // Output: 6 (a is incremented before printing) a = 5; printf("Post-increment: %d\n", a++); // Output: 5 (a is used before incrementing) printf("After Post-increment: %d\n", a); // Output: 6 return 0; }
7. Conditional (Ternary) Operator
The conditional operator is a shorthand for if-else
statements. It is used to evaluate a condition and choose one of two values based on whether the condition is true or false.
Syntax:
condition ? expression1 : expression2;
- If
condition
is true,expression1
is evaluated. - If
condition
is false,expression2
is evaluated.
Example:
#include <stdio.h> int main() { int a = 5, b = 10; int result = (a > b) ? a : b; // If a > b, result = a, else result = b printf("Result: %d\n", result); // Output: 10 return 0; }
8. Comma Operator
The comma operator allows multiple expressions to be evaluated in a single statement, with the result of the rightmost expression being the result of the entire statement.
Example:
#include <stdio.h> int main() { int a = 1, b = 2, c = 3; int result = (a++, b++, c++); printf("Result: %d\n", result); // Output: 3 (value of c after increment) printf("a: %d, b: %d, c: %d\n", a, b, c); // Output: a: 2, b: 3, c: 4 return 0; }
9. Pointer Operators
Pointer operators are used to work with pointers in C.
- Address-of (
&
): Returns the memory address of a variable. - Dereference (
*
): Accesses the value stored at the memory address pointed to by a pointer.
Example:
#include <stdio.h> int main() { int a = 10; int *ptr = &a; printf("Address of a: %p\n", ptr); // Output: address of a printf("Value of a: %d\n", *ptr); // Output: 10 (dereferencing the pointer) return 0; }
Conclusion
Operators are fundamental in C programming, allowing you to perform a variety of operations on data. Understanding and utilizing the different types of operators—arithmetic, relational, logical, bitwise, and others—enables you to write efficient and effective C programs.