Operators are special symbols in C programming that perform operations on variables and values. They are the foundation of logic, mathematics, and data manipulation in your applications.
For instance, in the expression int sum = 10 + 5;, the + is an operator that performs addition, and the = is an operator that assigns the result to the variable sum.
C provides a rich set of built-in operators, which can be categorized into several main types:
Let's dive deep into each category.
Arithmetic operators are used to perform common mathematical operations, such as addition, subtraction, multiplication, and division.
| Operator | Name | Description | Example (x = 10, y = 3) |
|---|---|---|---|
+ |
Addition | Adds two values together | x + y results in 13 |
- |
Subtraction | Subtracts one value from another | x - y results in 7 |
* |
Multiplication | Multiplies two values | x * y results in 30 |
/ |
Division | Divides one value by another | x / y results in 3 (Integer division) |
% |
Modulus | Returns the division remainder | x % y results in 1 |
++ |
Increment | Increases the value of a variable by 1 | x++ results in 11 |
-- |
Decrement | Decreases the value of a variable by 1 | x-- results in 9 |
#include <stdio.h>int main() { int a = 12; int b = 5;
printf("Addition: %d\n", a + b); printf("Subtraction: %d\n", a - b); printf("Multiplication: %d\n", a * b);
// Note: Dividing two integers results in an integer (decimals are truncated) printf("Division: %d\n", a / b);
// Modulus finds the remainder (12 / 5 is 2 with a remainder of 2) printf("Modulus: %d\n", a % b);
return 0; }
Assignment operators are used to assign values to variables. The most common is the simple assignment operator =.
C also provides compound assignment operators, which combine an arithmetic or bitwise operation with assignment, making your code cleaner and more concise.
| Operator | Example | Same As |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
#include <stdio.h>int main() { int score = 10;
score += 5; // Exactly the same as: score = score + 5 printf("Score is now: %d\n", score); // Outputs 15
score *= 2; // Exactly the same as: score = score * 2 printf("Score is now: %d\n", score); // Outputs 30
return 0; }
Relational operators are used to compare two values (or variables). This is incredibly important in programming because it helps us make decisions and control loops.
The return value of a comparison is either 1 (which means true) or 0 (which means false).
| Operator | Name | Example (x = 5, y = 8) | Result |
|---|---|---|---|
== |
Equal to | x == y |
0 (False) |
!= |
Not equal | x != y |
1 (True) |
> |
Greater than | x > y |
0 (False) |
< |
Less than | x < y |
1 (True) |
>= |
Greater than or equal to | x >= y |
0 (False) |
<= |
Less than or equal to | x <= y |
1 (True) |
Warning: A very common beginner mistake is using a single equals sign
=(assignment) when you actually meant to use double equals==(comparison).
Logical operators are used to determine the logic between variables or values. They evaluate multiple conditions together and return 1 (true) or 0 (false).
| Operator | Name | Description | Example |
|---|---|---|---|
&& |
Logical AND | Returns true if both statements are true | (x < 5 && x < 10) |
|| |
Logical OR | Returns true if one of the statements is true | (x < 5 || x < 4) |
! |
Logical NOT | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
#include <stdio.h>int main() { int age = 22; int hasLicense = 1; // 1 represents True
// Check if BOTH conditions are true if (age >= 18 && hasLicense == 1) { printf("You are legally allowed to drive.\n"); } else { printf("You cannot drive.\n"); }
return 0; }
Bitwise operators perform mathematical operations on data at the bit level (binary 0s and 1s). These are highly efficient and are often used in low-level programming, like developing drivers, operating systems, or cryptography.
& (Bitwise AND): Sets each bit to 1 if both bits are 1.| (Bitwise OR): Sets each bit to 1 if one of two bits is 1.^ (Bitwise XOR): Sets each bit to 1 if only one of two bits is 1.~ (Bitwise NOT): Inverts all the bits (0 becomes 1, 1 becomes 0).<< (Left Shift): Shifts bits to the left, filling with zeros. (Equivalent to multiplying by 2).>> (Right Shift): Shifts bits to the right. (Equivalent to dividing by 2).When multiple operators appear in a single expression, C determines which operation to execute first based on Operator Precedence (similar to PEMDAS in math).
For example, multiplication is executed before addition:
int result = 100 + 50 * 3;
Here, 50 * 3 happens first, then 100 is added, resulting in 250.
If you want to force a specific order of evaluation, use parentheses ():
int result = (100 + 50) * 3;
Here, the addition happens first, resulting in 450.
Q: What is the difference between ++x (pre-increment) and x++ (post-increment)?
A: While both increase the value of x by 1, they differ in when the increment happens during an expression. ++x increments the value before it is used in the expression, whereas x++ increments the value after the expression has been evaluated.
Q: Can I chain assignment operators like a = b = c = 10;?
A: Yes! C evaluates assignment operators from right to left. So, c becomes 10, then b becomes the value of c (10), and finally a becomes the value of b (10).
Q: Why is it dangerous to use = inside an if statement?
A: Because an expression like if (x = 5) actually assigns 5 to x, and since 5 is a non-zero value, the if condition will always evaluate to true. You almost always mean to use the comparison operator if (x == 5).
What will be the output of 15 % 4 in C?