C Operators
Operators in C are symbols that perform actions on values, such as adding numbers, assigning a value, or comparing two expressions. They let you build expressions that calculate results and make decisions.
Arithmetic Operators
Arithmetic operators work with numeric values. You have already used = to store values; arithmetic operators are what let you calculate new ones.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Remainder | a % b |
#include <stdio.h>
int main(void)
{
int a = 17;
int b = 5;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Integer division: %d\n", a / b);
printf("Remainder: %d\n", a % b);
return 0;
}
Output:
Sum: 22
Difference: 12
Product: 85
Integer division: 3
Remainder: 2
Because a and b are both integers, a / b performs integer division. The decimal part is discarded, so 17 / 5 gives 3. The remainder operator % gives what is left over.
Assignment Operators
The assignment operator = stores the value on its right into the variable on its left. C also has shortcut assignment operators that combine a calculation with assignment.
#include <stdio.h>
int main(void)
{
int score = 10;
score += 5;
printf("After bonus: %d\n", score);
score *= 2;
printf("After doubling: %d\n", score);
score -= 4;
printf("Final score: %d\n", score);
return 0;
}
Output:
After bonus: 15
After doubling: 30
Final score: 26
The statement score += 5; means the same thing as score = score + 5;. Similar shortcuts include -=, *=, /=, and %=.
Increment and Decrement
The operators ++ and -- add or subtract 1 from a variable. They are common in counters and loops.
#include <stdio.h>
int main(void)
{
int count = 3;
count++;
printf("After increment: %d\n", count);
count--;
printf("After decrement: %d\n", count);
return 0;
}
Output:
After increment: 4
After decrement: 3
Comparison Operators
Comparison operators test relationships between values. In C, a true comparison produces 1, and a false comparison produces 0.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
#include <stdio.h>
int main(void)
{
int age = 20;
int minimum_age = 18;
printf("Old enough: %d\n", age >= minimum_age);
printf("Exactly 18: %d\n", age == minimum_age);
return 0;
}
Output:
Old enough: 1
Exactly 18: 0
Be careful not to confuse = and ==. Use = to assign a value, and use == to compare two values.
Logical Operators
Logical operators combine or reverse comparisons. They are especially useful in conditions.
| Operator | Meaning |
|---|---|
&& |
AND: true when both sides are true |
|| |
OR: true when at least one side is true |
! |
NOT: reverses true and false |
#include <stdio.h>
int main(void)
{
int age = 20;
int has_ticket = 1;
printf("Can enter: %d\n", age >= 18 && has_ticket == 1);
printf("Needs help: %d\n", !(age >= 18));
return 0;
}
Output:
Can enter: 1
Needs help: 0
Operator Precedence
C follows precedence rules to decide which operators run first. Multiplication and division happen before addition and subtraction, so 2 + 3 * 4 is 14, not 20.
Use parentheses when they make an expression clearer or when you need a different order.
#include <stdio.h>
int main(void)
{
int first = 2 + 3 * 4;
int second = (2 + 3) * 4;
printf("Without parentheses: %d\n", first);
printf("With parentheses: %d\n", second);
return 0;
}
Output:
Without parentheses: 14
With parentheses: 20
The key idea is that operators turn values into expressions: they calculate, assign, compare, and combine results. Next, you will use these expressions more often when writing conditions and control flow.
