C Language Operators
Complete Notes, Examples, Programs and Practice Questions
1. What is an Operator?
An expression is a combination of operators and operands that is evaluated to produce a result.
Example
2 + 3
2 + 3→ Expression+→ Operator2and3→ Operands
Examples of Expressions
| Expression | Explanation |
|---|---|
2 + 3 |
Two constants are added. |
x + 10 |
A variable and a constant are added. |
x + y |
Two variables are added. |
(x + y) * z |
The result of x + y is multiplied by z. |
2. Types of Operators Based on Number of Operands
| Type | Number of Operands | Example | Explanation |
|---|---|---|---|
| Unary | 1 | ++x |
Works with one operand. |
| Binary | 2 | x + y |
Works with two operands. |
| Ternary | 3 | a ? b : c |
Conditional operator works with three expressions. |
++x has one operand, so it is a unary operator.
3. Types of C Operators Based on Operation
| # | Operator Category | Examples |
|---|---|---|
| 1 | Arithmetic Operators | + - * / % |
| 2 | Assignment Operators | = += -= *= /= %= |
| 3 | Increment / Decrement | ++ -- |
| 4 | Relational / Comparison | > >= < <= == != |
| 5 | Logical Operators | && || ! |
| 6 | Bitwise Operators | & | ~ ^ << >> |
4. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 2 |
7 |
| – | Subtraction | 5 - 2 |
3 |
| * | Multiplication | 5 * 2 |
10 |
| / | Division | 5 / 2 |
2 for integers |
| % | Modulus | 5 % 2 |
1 |
5 / 2 gives 2, not 2.5.
Example Program
#include <stdio.h>
int main()
{
int res;
res = 5 + 2;
printf("Result = %d\n", res);
res = 5 - 2;
printf("Result = %d\n", res);
res = 5 * 2;
printf("Result = %d\n", res);
res = 5 / 2;
printf("Result = %d\n", res);
res = 5 % 2;
printf("Result = %d\n", res);
return 0;
}
Output
5. Assignment Operator
= symbol is called the assignment operator
in C. It assigns the value on the right-hand side to the variable on the left-hand side.
Syntax
variable = value;
variable = expression;
variable = another_variable;
Example
#include <stdio.h>
int main()
{
int a, b;
a = 12;
b = 24;
printf("a = %d\n", a);
printf("b = %d\n", b);
a = b;
printf("After a = b:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
= means assignment, while == means comparison.
6. Swapping Two Variables
Swapping means exchanging the values of two variables.
Using a Temporary Variable
#include <stdio.h>
int main()
{
int x, y, temp;
x = 12;
y = 24;
printf("Before swapping:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
temp = x;
x = y;
y = temp;
printf("\nAfter swapping:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
temp = x;x = y;y = temp;
7. Short-Hand / Compound Assignment Operators
Compound assignment operators provide a shorter way of writing an expression where the same variable appears on both sides of an assignment.
| Short Form | Equivalent Form |
|---|---|
x += 5 |
x = x + 5 |
x -= 5 |
x = x - 5 |
x *= 5 |
x = x * 5 |
x /= 5 |
x = x / 5 |
x %= 5 |
x = x % 5 |
Example
#include <stdio.h>
int main()
{
int x = 10;
int y = 20;
printf("x = %d\n", x);
x += 2;
printf("After x += 2, x = %d\n", x);
x += y;
printf("After x += y, x = %d\n", x);
y += x;
printf("After y += x, y = %d\n", y);
return 0;
}
8. Type Conversion and Type Casting
Type conversion occurs when a value of one data type is converted into another data type.
Implicit Type Conversion
When the compiler automatically converts one data type into another, it is called implicit type conversion.
int a = 10;
float b;
b = a;
Here, the integer value 10 is automatically converted
to a floating-point value.
Explicit Type Conversion / Type Casting
When the programmer explicitly specifies the required data type, it is called type casting.
float x;
x = (float)'a';
printf("%f", x);
(float) explicitly converts the character value
into a floating-point value.
Example: Integer Division vs Type Casting
#include <stdio.h>
int main()
{
int a = 5;
int b = 2;
printf("Integer division = %d\n", a / b);
printf("Floating division = %.2f\n", (float)a / b);
return 0;
}
9. Type Conversion Practice
Hint: ASCII value of ‘A’ is 65.
10. Increment and Decrement Operators
| Operator | Name | Operation |
|---|---|---|
++ |
Increment | Adds 1 to a variable. |
-- |
Decrement | Subtracts 1 from a variable. |
Increment
x++ or ++x increases the value of
x by 1.
Conceptually:
x = x + 1;
Decrement
x-- or --x decreases the value of
x by 1.
x = x - 1;
Example
#include <stdio.h>
int main()
{
int x = 10;
printf("x = %d\n", x);
x++;
printf("After increment, x = %d\n", x);
x--;
printf("After decrement, x = %d\n", x);
return 0;
}
11. Pre-Increment and Post-Increment
| Operator | Name | Meaning |
|---|---|---|
x++ |
Post-increment | Use the current value first, then increment. |
++x |
Pre-increment | Increment first, then use the new value. |
x-- |
Post-decrement | Use the current value first, then decrement. |
--x |
Pre-decrement | Decrement first, then use the new value. |
Post-Increment Example
#include <stdio.h>
int main()
{
int a = 2;
printf("a = %d\n", a++);
printf("a = %d\n", a);
return 0;
}
In a++, the old value is used first and then
a is increased.
Another Example
#include <stdio.h>
int main()
{
int x = 10;
int y;
y = x++ + 5;
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
Post → Use first, change later.
Pre → Change first, use later.
Pre-Increment Example
int x = 10;
int y;
y = ++x + 5;
Here x becomes 11 first, so
y becomes 16.
12. Relational / Comparison Operators
Relational operators are used to compare two values. The result of a comparison is either true or false.
In C, a true condition generally evaluates to 1,
while a false condition evaluates to 0.
| Operator | Name | Example |
|---|---|---|
> |
Greater than | 5 > 3 |
>= |
Greater than or equal to | 5 >= 5 |
< |
Less than | 3 < 5 |
<= |
Less than or equal to | 5 <= 5 |
== |
Equal to | 5 == 5 |
!= |
Not equal to | 5 != 3 |
= → Assignment== → Comparison
Example Program
#include <stdio.h>
int main()
{
int res;
res = 5 > 7;
printf("5 > 7 = %d\n", res);
res = 5 >= 7;
printf("5 >= 7 = %d\n", res);
res = 5 < 7;
printf("5 < 7 = %d\n", res);
res = 5 <= 7;
printf("5 <= 7 = %d\n", res);
res = 5 == 7;
printf("5 == 7 = %d\n", res);
res = 5 != 7;
printf("5 != 7 = %d\n", res);
return 0;
}
13. Logical / Boolean Operators
Logical operators are used to combine or modify conditions.
Their result is either 1 (true) or 0 (false).
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | True only when both conditions are true. |
|| |
Logical OR | True when at least one condition is true. |
! |
Logical NOT | Reverses the logical result. |
Truth Table
| A | B | A && B | A || B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
Example Program
#include <stdio.h>
int main()
{
int res;
res = (5 > 7) && (12 < 20);
printf("AND result = %d\n", res);
res = (5 > 7) || (12 < 20);
printf("OR result = %d\n", res);
res = !(5 > 7);
printf("NOT result = %d\n", res);
return 0;
}
14. Bitwise Operators
Bitwise operators work directly with the individual bits of integer values.
| Operator | Name | Description |
|---|---|---|
& |
Bitwise AND | Sets a bit to 1 when both corresponding bits are 1. |
| |
Bitwise OR | Sets a bit to 1 when at least one corresponding bit is 1. |
^ |
Bitwise XOR | Sets a bit to 1 when the corresponding bits are different. |
~ |
Bitwise NOT / Complement | Flips each bit. |
<< |
Left Shift | Shifts bits toward the left. |
>> |
Right Shift | Shifts bits toward the right. |
Example: Bitwise AND
4 = 0100
5 = 0101
----
4 & 5 = 0100 = 4
Example Program
#include <stdio.h>
int main()
{
int res;
res = 4 & 5;
printf("4 & 5 = %d\n", res);
res = 4 << 2;
printf("4 << 2 = %d\n", res);
return 0;
}
Bitwise AND Example
4 = 0100
5 = 0101
----
4 & 5 = 0100
= 4
Bitwise OR Example
4 = 0100
5 = 0101
----
4 | 5 = 0101
= 5
Bitwise XOR Example
4 = 0100
5 = 0101
----
4 ^ 5 = 0001
= 1
Left Shift
4 = 0100
4 << 1 = 1000 = 8
4 << 2 = 10000 = 16
15. Quick Revision Table
| Category | Operators | Main Purpose |
|---|---|---|
| Arithmetic | + - * / % |
Mathematical operations |
| Assignment | = |
Assign a value |
| Compound Assignment | += -= *= /= %= |
Short form of assignment + operation |
| Increment / Decrement | ++ -- |
Increase/decrease by 1 |
| Relational | > >= < <= == != |
Compare values |
| Logical | && || ! |
Combine or reverse conditions |
| Bitwise | & | ^ ~ << >> |
Operate on individual bits |
16. Practice Questions
Level 1 – Basic
x + 10.
=
and ==?
Level 2 – Predict the Output
int x = 10;
printf("%d", x++);
int x = 10;
printf("%d", ++x);
x and y:
int x = 5;
int y;
y = x++ + 10;
int x = 5;
int y = 2;
printf("%d", x / y);
printf("%d", x % y);
printf("%d", 4 & 5);
Level 3 – Write Programs
17. Challenge Questions
int a = 5;
int b = 10;
a += b;
b += a;
printf("a = %d\n", a);
printf("b = %d\n", b);
a and b?
int a = 10;
int b = 20;
a++;
++b;
a += b;
b -= 5;
int a = 4;
int b = 5;
printf("%d\n", a & b);
printf("%d\n", a | b);
printf("%d\n", a ^ b);
printf("%d\n", 5 / 2);
printf("%.1f\n", (float)5 / 2);
18. Important Points to Remember
=is assignment;==is comparison.5 / 2gives2when both operands are integers.5 % 2gives the remainder1.- Post-increment uses the old value before incrementing.
- Pre-increment increments the value before it is used.
- Zero represents false in a logical context.
- Any non-zero value represents true in a logical context.
- Relational operators produce
0or1in C. - Bitwise operators work on individual bits of integer values.
- Type casting can be used to explicitly convert a value to another type.