C-Program
Operators in c
Last Updated on: 5th Jul 2025 20:38:25 PM
Operators in C are special symbols used to perform operations on variables and values. They are essential to perform tasks like mathematical calculations, comparisons, logic, assignments, and more.
1. Arithmetic Operators
Used to perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus (Remainder) | 10 % 3 |
1 |
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
2. Relational and Logical Operators
a. Relational Operators
Used to compare two values and return true (1) or false (0).
Operator | Description | Example (a = 5 , b = 3 ) |
Result |
---|---|---|---|
== |
Equal to | a == b |
0 |
!= |
Not equal to | a != b |
1 |
> |
Greater than | a > b |
1 |
< |
Less than | a < b |
0 |
>= |
Greater or equal | a >= b |
1 |
<= |
Less or equal | a <= b |
0 |
b. Logical Operators
Used to perform logical (boolean) operations.
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | a > 2 && b < 5 |
1 |
|| | Logical OR | (a > 0 || b < 0) | 1 |
! |
Logical NOT | !(a == b) |
1 |
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a > b: %d\n", a > b);
printf("a == b: %d\n", a == b);
printf("Logical AND: %d\n", a > 2 && b < 5);
return 0;
}
3. Assignment and Unary Operators
Used to assign values to variables.
a. Assignment Operators
Used to assign values to variables.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= |
Simple assignment | a = 5; |
Assign 5 to a |
+= |
Add and assign | a += 3; → |
a = a + 3 |
-= |
Subtract and assign | a -= 2; |
a = a - 2 |
*= |
Multiply and assign | a *= 4; |
a = a * 3 |
/= |
Divide and assign | a /= 2; |
a = a / 2 |
%= |
Modulus and assign | a %= 3; |
a = a % 2 |
b. Unary Operators
Work with a single operand.
Operator | Description | Example | Result |
---|---|---|---|
++ |
Increment | a++ |
a = a + 1 |
-- |
Decrement | a-- |
a = a - 1 |
- |
Unary minus | -a |
Negates a |
+ |
Unary plus | +a |
Returns a |
! |
Logical NOT | !a |
1 if a is 0 |
Example:
#include <stdio.h>
int main() {
int x = 5;
printf("x = %d\n", x);
printf("x++ = %d\n", x++); // Post-increment
printf("++x = %d\n", ++x); // Pre-increment
return 0;
}
4. Bitwise, Ternary, and Special Operators
a. Bitwise Operators
Operate on binary bits.
Operator | Name | Example (a=5, b=3) | Result (Binary) |
---|---|---|---|
& |
AND | a & b |
1 (0101 & 0011 = 0001) |
` | ` | OR | `a |
^ |
XOR | a ^ b |
6 (0101 ^ 0011 = 0110) |
~ |
NOT | ~a |
-6 (Inverts all bits) |
<< |
Left Shift | a << 1 |
10 |
>> |
Right Shift | a >> 1 |
2 |
b. Ternary Operator (?:
)
A shorthand for if-else.
Syntax:
(condition) ? value_if_true : value_if_false;
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
max will store the greater value between a
and b
.
c. Special Operators
Operator | Purpose |
---|---|
sizeof |
Returns the size of a data type or variable |
& |
Address-of (used with pointers or scanf ) |
* |
Pointer dereference |
-> |
Access structure member via pointer |
. |
Access structure member |
Example:
int a = 5;
printf("Size of a: %lu", sizeof(a)); // Output: 4 (depends on system)
Quick Summary Table :
Operator Type | Examples | Purpose |
---|---|---|
Arithmetic | +, -, *, /, % | Math operations |
Relational | ==, !=, >, < | Compare values |
Logical | &&, ` | |
Assignment | =, +=, -= | Assign values |
Unary | ++, --, -, ! | One-operand operations |
Bitwise | &, ` | , ^, ~, <<` |
Ternary | ? : | Shorthand if-else |
Special | sizeof, &, *, . | Memory, pointer, structure access |