Operators are the predefined symbols which are used to perform mathematical and logical operations.
For example: A+B , Here A and B are the operands and "+" is the operator .
The main types of operators in C are:
Arithmetic Operators:
Used for mathematical calculations.
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo - remainder after division)
++ (increment - adds 1)
-- (decrement - subtracts 1)
Relational Operators:
Used to compare two operands and return a boolean result (true or false, represented as 1 or 0).
== (equal to)
!= (not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Logical Operators:
Used to combine or modify conditional expressions.
&& (logical AND)
|| (logical OR)
! (logical NOT)
Bitwise Operators:
Used to perform operations on individual bits of integer operands.
& (bitwise AND)
| (bitwise OR)
^ (bitwise XOR)
~ (bitwise NOT/one's complement)
<< (left shift)
>> (right shift)
Assignment Operators:
Used to assign values to variables.
= (simple assignment)
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= (compound assignment operators)
Conditional (Ternary) Operator:
A shorthand for an if-else statement.
condition ? expression1 : expression2;
Special Operators:
sizeof() (returns the size of a variable or type)
& (address-of operator)
* (pointer dereference operator)
, (comma operator)
Increment/Decrement Operators:
Increment (++) and decrement (--) operators in C are unary operators that increase or decrease the value of a variable by one .
There are two forms for both operators:
Prefix Form: The operator is placed before the variable (e.g., ++a, --a).
Behavior: The value of the variable is modified before it is used in the expresssion.
Example: int a = 5; int b = ++a; // a becomes 6, then b is assigned 6
Postfix Form: The operator is placed after the variable (e.g., a++, a--).
Behavior: The original value of the variable is used in the expression first, and then the variable's value is modified.
Example: int x = 5; int y = x++; // y is assigned 5, then x becomes 6
Similarly decrement operator works .