Operators combine, compare or modify values. The main beginner groups are arithmetic operators, relational operators, logical operators, assignment operators, and increment/decrement operators.
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Integer remainder |
#include <stdio.h>
int main(void) {
int x = 30;
int y = 20;
printf("x + y = %d\n", x + y);
printf("x - y = %d\n", x - y);
printf("x * y = %d\n", x * y);
printf("x / y = %d\n", x / y);
printf("x %% y = %d\n", x % y);
return 0;
}
Output
x + y = 50
x - y = 10
x * y = 600
x / y = 1
x % y = 10
Because both operands in x / y are integers, this example uses integer division.
int i = 45;
i++; // 46
i += 5; // 51
i--; // 50
Compound assignment such as i += 5 means i = i + 5.
int i = 45;
int a = i++; // a gets 45, then i becomes 46
int j = 45;
int b = ++j; // j becomes 46, then b gets 46
Key change: the position of ++ changes whether increment happens before or after the value of the expression is produced.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
int x = 30;
int y = 20;
printf("%d\n", x == y); // 0
printf("%d\n", x > y); // 1
printf("%d\n", x != y); // 1
Comparison and logical expressions produce 0 when false and a nonzero value when true; the operators shown here produce either 0 or 1.
| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
int x = 40;
int y = 50;
printf("%d\n", (x == 40) && (y == 30)); // 0
printf("%d\n", (x == 40) || (y == 30)); // 1
printf("%d\n", !(x == 40)); // 0
50 / 4 when both values are integers.i++ and ++i when their values are assigned to another variable.age >= 18 and has_id == 1.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.