Operators are symbols that perform operations on variables and values. C supports a wide range of operators, which can be broadly categorized into several types.
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus | a % b |
Increment and decrement operators are used to increase or decrease the value of a variable by one.
Operator | Description | Example |
---|---|---|
++ |
Increment | a++ or ++a |
-- |
Decrement | a-- or --a |
Relational operators are used to compare two values. They return either true (1) or false (0).
Operator | Description | Example |
---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
&& |
Logical AND | a && b |
|| |
Logical OR | a || b |
! |
Logical NOT | !a |
Bitwise operators are used to perform bit-level operations on integer types.
Operator | Description | Example |
---|---|---|
& |
Bitwise AND | a & b |
| |
Bitwise OR | a | b |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise NOT | ~a |
<< |
Left shift | a << 1 |
>> |
Right shift | a >> 1 |
The sizeof
operator is used to determine the size, in bytes, of a variable or data type.
The conditional (ternary) operator is used to evaluate a condition and return one of two values based on the result.
The address operator &
is used to obtain the address of a variable, and the dereference operator *
is used to access the value at a given address.
Here are some examples demonstrating the usage of various operators in C: