C Operators
Operators are symbols that perform operations on values and variables. C supports arithmetic operators (+, -, *, /, %), assignment operators (=, +=, -=), and comparison operators (==, !=, <, >).
Understanding operator precedence โ which operators run first โ is important, since it changes the result of an expression, like * running before + unless parentheses say otherwise.
result = a + b;
if (a == b) { ... }Arithmetic and assignment
Arithmetic operators perform math: +, -, *, /, and % (modulus, the remainder of division). Assignment operators like += and *= combine an operation with assignment in one step.
Comparison and logical
Comparison operators (==, !=, <, >, <=, >=) return 1 (true) or 0 (false). Logical operators && (AND), || (OR), and ! (NOT) combine boolean conditions.
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("%d %d\n", a / b, a % b);
return 0;
}3 1Integer division truncates toward zero, and % gives the remainder.
#include <stdio.h>
int main() {
int a = 5;
a += 3;
printf("%d\n", a);
printf("%d\n", (a > 5) && (a < 10));
return 0;
}8
1a += 3 adds 3 to a, and the logical AND expression evaluates to 1 (true).
Key points
- % gives the remainder of integer division.
- == tests equality; = performs assignment โ don't confuse them.
- && and || combine boolean conditions; ! negates one.
- Compound assignment operators like += shorten common patterns.
