A Comprehensive Guide to Operators in C Programming with Examples
Operators in C
Operators are symbols used to perform operations on operands (values or variables). C provides a wide range of operators for arithmetic, logical, relational, bitwise, and other operations.
Arithmetic Operators:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder)
Example:
int x = 10;
int y = 5;
int sum = x + y;
int difference = x - y;
int product = x * y;
int quotient = x / y;
int remainder = x % y;
Relational Operators:
==
: Equal to!=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal to
Example:
int age = 25;
if (age >= 18) {
printf("You are an adult.\n");
}
Logical Operators:
&&
: Logical AND||
: Logical OR!
: Logical NOT
Example:
int x = 10;
int y = 5;
if (x > 0 && y > 0) {
printf("Both x and y are positive.\n");
}
Bitwise Operators:
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise NOT<<
: Left shift>>
: Right shift
Example:
int a = 5; // 0000 0101
int b = 3; // 0000 0011
int result = a & b; // 0000 0001
Other Operators:
?:
: Ternary operator (conditional expression)sizeof
: Sizeof operator (returns the size of a data type or variable)*
: Dereference operator (used with pointers)&
: Address-of operator (used to get the address of a variable)->
: Member access operator (used with structures and unions)