
Operators in C
? Operators in C
Operators are symbols that perform operations on variables and values. C has a rich set of operators grouped by functionality � arithmetic, logical, relational, and more.
? 1. Arithmetic Operators
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | a + b | 7 |
- | Subtraction | a - b | 3 |
* | Multiplication | a * b | 10 |
/ | Division | a / b | 2 |
% | Modulus (remainder) | a % b | 1 |
? 2. Relational (Comparison) Operators
Operator | Meaning | 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 |
?? 3. Logical Operators
Operator | Meaning | Example |
---|---|---|
&& | Logical AND | a > 0 && b > 0 |
` | ` | |
! | Logical NOT | !(a > b) |
? 4. Assignment Operators
Operator | Meaning | Example |
---|---|---|
= | Assign value | a = 10 |
+= | Add & assign | a += 5 |
-= | Subtract & assign | a -= 3 |
*= | Multiply & assign | a *= 2 |
/= | Divide & assign | a /= 4 |
%= | Modulus & assign | a %= 3 |
? 5. Increment & Decrement
Operator | Description | Example |
---|---|---|
++ | Increment by 1 | a++ or ++a |
-- | Decrement by 1 | a-- or --a |
? 6. Bitwise Operators
Operator | Description | Example |
---|---|---|
& | AND | a & b |
` | ` | OR |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left shift | a << 1 |
>> | Right shift | a >> 1 |
? 7. Ternary Operator
condition ? value_if_true : value_if_false;
Example:
int max = (a > b) ? a : b;
? 8. Sizeof Operator
Returns the size (in bytes) of a data type or variable:
printf("%d", sizeof(int)); // Usually 4