C Operators and Expressions
Learn all the operators in C: math (+, -, *, /), comparisons (==, <, >), and logical operations (&&, ||). Build expressions that compute values.
Track Your Progress
Sign in to save your learning progress
What You Will Learn
- ✓Use arithmetic operators (+, -, *, /, %)
- ✓Compare values with relational operators
- ✓Combine conditions with && (AND) and || (OR)
- ✓Increment/decrement with ++ and --
01Why Do We Need Operators?
Think About Math in Real Life...
When you do math on paper, you use symbols like +, -, ×, ÷. In programming, we need the same ability - plus many more operations like comparisons (is 5 greater than 3?) and logical decisions (is this AND that true?).
Operators are the symbols that let us perform these operations in C.
Operators are symbols that tell the compiler to perform specific operations on values or variables. The values they work on are called operands.
Expression: 10 + 20
Operand
Operator
Operand
Result
Types Based on Number of Operands
Unary (1 operand)
++a, --b, !flag
Binary (2 operands)
a + b, x * y
Ternary (3 operands)
a ? b : c
02Arithmetic Operators
Used for mathematical calculations like addition, subtraction, multiplication, and division.
Key Points for Beginners
- • Division (/) with integers: Result is always an integer (truncated).
10 / 3 = 3, not 3.33 - • Modulus (%): Returns the remainder after division.
10 % 3 = 1because 10 = 3×3 + 1 - • ++a vs a++:
++aincrements before using the value;a++uses the value then increments
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3 (integer) |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ++ | Increment | a++ or ++a | a + 1 |
| -- | Decrement | a-- or --a | a - 1 |
▶ Try it: Demonstrates all arithmetic operators with two numbers and shows the difference between pre/post increment.
1#include <stdio.h>23int main() {4 int a = 17, b = 5;5 6 printf("a = %d, b = %d\n\n", a, b);7 printf("a + b = %d\n", a + b); // 228 printf("a - b = %d\n", a - b); // 129 printf("a * b = %d\n", a * b); // 8510 printf("a / b = %d\n", a / b); // 3 (integer division)11 printf("a %% b = %d\n", a % b); // 2 (remainder)12 13 // Pre vs Post increment14 int x = 5;15 printf("\nx = %d\n", x);16 printf("x++ = %d\n", x++); // Prints 5, then x becomes 617 printf("Now x = %d\n", x); // x is now 618 printf("++x = %d\n", ++x); // x becomes 7, then prints 719 20 return 0;21}a = 17, b = 5
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
x = 5
x++ = 5
Now x = 6
++x = 7
Pre vs Post Increment
++x (prefix): Increment first, then use the valuex++ (postfix): Use the value first, then increment
03Relational Operators
Used to compare two values. Returns 1 (true) or 0 (false).
| Operator | Meaning | Example (a=10, b=5) | Result |
|---|---|---|---|
| == | Equal to | a == b | 0 (false) |
| != | Not equal to | a != b | 1 (true) |
| > | Greater than | a > b | 1 (true) |
| < | Less than | a < b | 0 (false) |
| >= | Greater or equal | a >= b | 1 (true) |
| <= | Less or equal | a <= b | 0 (false) |
▶ Try it: Compares two numbers and prints 1 for true, 0 for false.
1#include <stdio.h>23int main() {4 int a = 10, b = 5;5 6 printf("a = %d, b = %d\n\n", a, b);7 printf("a == b: %d\n", a == b); // 0 (false)8 printf("a != b: %d\n", a != b); // 1 (true)9 printf("a > b: %d\n", a > b); // 1 (true)10 printf("a < b: %d\n", a < b); // 0 (false)11 printf("a >= b: %d\n", a >= b); // 1 (true)12 printf("a <= b: %d\n", a <= b); // 0 (false)13 14 return 0;15}04Logical Operators
Used to combine or negate conditions. Essential for complex if statements.
| Operator | Name | Description | Example |
|---|---|---|---|
| && | AND | True if both are true | 1 && 1 = 1, 1 && 0 = 0 |
| || | OR | True if any is true | 1 || 0 = 1, 0 || 0 = 0 |
| ! | NOT | Reverses the value | !1 = 0, !0 = 1 |
▶ Try it: Shows how logical operators combine conditions and how NOT reverses boolean values.
1#include <stdio.h>23int main() {4 int a = 1, b = 0; // 1 = true, 0 = false5 6 printf("a = %d (true), b = %d (false)\n\n", a, b);7 8 // AND: both must be true9 printf("a && b = %d\n", a && b); // 0 (false)10 printf("a && a = %d\n", a && a); // 1 (true)11 12 // OR: at least one must be true13 printf("a || b = %d\n", a || b); // 1 (true)14 printf("b || b = %d\n", b || b); // 0 (false)15 16 // NOT: reverse the value17 printf("!a = %d\n", !a); // 0 (false)18 printf("!b = %d\n", !b); // 1 (true)19 20 // Practical example21 int age = 25;22 int hasLicense = 1;23 if (age >= 18 && hasLicense) {24 printf("\nYou can drive!\n");25 }26 27 return 0;28}05Bitwise Operators
Operate on individual bits of integers. Used in low-level programming, flags, and optimization.
| Op | Name | Description |
|---|---|---|
| & | AND | 1 if both bits are 1 |
| | | OR | 1 if any bit is 1 |
| ^ | XOR | 1 if bits are different |
| ~ | NOT | Flip all bits |
| << | Left Shift | Multiply by 2ⁿ |
| >> | Right Shift | Divide by 2ⁿ |
Visual: 5 & 3 (Bitwise AND)
▶ Try it: Demonstrates bitwise operations and shows how left/right shift multiplies or divides by powers of 2.
1#include <stdio.h>23int main() {4 int a = 5, b = 3; // 5 = 0101, 3 = 00115 6 printf("a = %d (0101), b = %d (0011)\n\n", a, b);7 8 printf("a & b = %d\n", a & b); // 1 (0001)9 printf("a | b = %d\n", a | b); // 7 (0111)10 printf("a ^ b = %d\n", a ^ b); // 6 (0110)11 printf("~a = %d\n", ~a); // -6 (inverts all bits)12 13 // Shift operators14 printf("\na << 1 = %d\n", a << 1); // 10 (multiply by 2)15 printf("a << 2 = %d\n", a << 2); // 20 (multiply by 4)16 printf("a >> 1 = %d\n", a >> 1); // 2 (divide by 2)17 18 return 0;19}Shift Trick
x << n = x × 2ⁿ (fast multiplication)x >> n = x ÷ 2ⁿ (fast division)
06Assignment Operators
Used to assign values to variables. Compound operators combine assignment with another operation.
| Operator | Equivalent |
|---|---|
| a = b | a = b |
| a += b | a = a + b |
| a -= b | a = a - b |
| a *= b | a = a * b |
| a /= b | a = a / b |
| a %= b | a = a % b |
| Operator | Equivalent |
|---|---|
| a &= b | a = a & b |
| a |= b | a = a | b |
| a ^= b | a = a ^ b |
| a <<= b | a = a << b |
| a >>= b | a = a >> b |
▶ Try it: Shows compound assignment operators in action — they modify the variable in place.
1#include <stdio.h>23int main() {4 int x = 10;5 printf("Initial x = %d\n\n", x);6 7 x += 5; printf("x += 5 → x = %d\n", x); // 158 x -= 3; printf("x -= 3 → x = %d\n", x); // 129 x *= 2; printf("x *= 2 → x = %d\n", x); // 2410 x /= 4; printf("x /= 4 → x = %d\n", x); // 611 x %= 4; printf("x %%= 4 → x = %d\n", x); // 212 13 return 0;14}07Other Operators
sizeof Operator - Getting Size in Bytes
sizeof is a compile-time operator that returns the size (in bytes) of a variable or data type. It's not a function - no function call overhead!
| Operator | Description | Example |
|---|---|---|
| sizeof() | Returns size in bytes (compile-time) | sizeof(int) → 4 |
| ? : | Ternary (shorthand if-else) | a > b ? a : b |
| & | Address-of operator | &x → 0x7fff... |
| * | Dereference (get value at address) | *ptr → value |
| (type) | Type cast (convert to type) | (float)5 → 5.0 |
| , | Comma (evaluates left-to-right) | (a=1, b=2) → 2 |
▶ Try it: Demonstrates sizeof, ternary operator, address-of, and type casting.
1#include <stdio.h>23int main() {4 // sizeof - get size in bytes5 printf("sizeof(int): %zu bytes\n", sizeof(int));6 printf("sizeof(char): %zu byte\n", sizeof(char));7 8 // Ternary operator (condition ? if_true : if_false)9 int a = 10, b = 20;10 int max = (a > b) ? a : b;11 printf("\nMax of %d and %d is %d\n", a, b, max);12 13 // Address-of operator14 int x = 42;15 printf("\nValue of x: %d\n", x);16 printf("Address of x: %p\n", (void*)&x);17 18 // Type casting19 int num = 7;20 printf("\n7 / 2 = %d (int)\n", num / 2);21 printf("7 / 2 = %.2f (float)\n", (float)num / 2);22 23 return 0;24}08C Expressions
An expression is a combination of operands (values/variables) and operators that evaluates to a single value. Expressions are the building blocks of C statements.
| Expression Type | Description | Example |
|---|---|---|
| Constant | Contains only constant values | 5 + 3 * 2 |
| Integral | Produces integer result | a + b * c |
| Floating | Produces float/double result | 3.14 * r * r |
| Relational | Produces true (1) or false (0) | a > b |
| Logical | Combines conditions | a > 0 && b > 0 |
| Pointer | Produces memory address | &x, ptr + 1 |
▶ Try it: Demonstrates different types of expressions and how they evaluate to single values.
1#include <stdio.h>23int main() {4 int a = 10, b = 5;5 float pi = 3.14, r = 2.0;6 7 // Constant expression (evaluated at compile time)8 int size = 5 + 3 * 2; // 119 printf("Constant: 5 + 3 * 2 = %d\n", size);10 11 // Arithmetic expression12 int sum = a + b * 2; // 10 + 10 = 2013 printf("Arithmetic: a + b * 2 = %d\n", sum);14 15 // Floating expression16 float area = pi * r * r; // 3.14 * 4 = 12.5617 printf("Floating: pi * r * r = %.2f\n", area);18 19 // Relational expression (returns 0 or 1)20 int isGreater = a > b; // 1 (true)21 printf("Relational: a > b = %d\n", isGreater);22 23 // Logical expression24 int bothPositive = (a > 0) && (b > 0); // 1 (true)25 printf("Logical: (a > 0) && (b > 0) = %d\n", bothPositive);26 27 // Assignment expression (returns assigned value)28 int x, y;29 x = (y = 5) + 3; // y=5, x=830 printf("Assignment: x = (y = 5) + 3 → x=%d, y=%d\n", x, y);31 32 // Comma expression (evaluates left-to-right, returns last)33 int result = (a = 1, b = 2, a + b); // result = 334 printf("Comma: (a=1, b=2, a+b) = %d\n", result);35 36 return 0;37}Constant: 5 + 3 * 2 = 11
Arithmetic: a + b * 2 = 20
Floating: pi * r * r = 12.56
Relational: a > b = 1
Logical: (a > 0) && (b > 0) = 1
Assignment: x = (y = 5) + 3 → x=8, y=5
Comma: (a=1, b=2, a+b) = 3
Expression vs Statement
Expression: a + b — Produces a value
Statement: x = a + b; — Performs an action (ends with ;)
09Operator Precedence
When an expression has multiple operators, precedence determines which operator is evaluated first. This is why 10 + 20 * 30 equals 610, not 900!
Quick Example
* is evaluated before + (higher precedence)
Full Tutorial Available
Learn the complete precedence table, associativity rules, and the PUMAS REBL TAC memory trick.
!Code Pitfalls: Common Mistakes & What to Watch For
These are the most common mistakes that trip up beginners. Study them carefully to avoid hours of debugging!
Using = instead of ==
if (a = 5) { } // WRONG! This assigns 5 to aif (a == 5) { } // Correct! This compares a to 5Integer Division Confusion
int result = 5 / 2; // Result: 2, not 2.5!float result = 5.0 / 2; // Result: 2.5float result = (float)5 / 2; // Result: 2.5Confusing & with &&
if (a & b) { } // Bitwise AND (rarely intended)if (a && b) { } // Logical AND (usually intended)11Try It Yourself
Here's a simple calculator that demonstrates arithmetic operators:
1#include <stdio.h>23int main() {4 int a = 15, b = 4;5 6 // Arithmetic operators in action7 printf("a = %d, b = %d\n\n", a, b);8 printf("Addition: a + b = %d\n", a + b);9 printf("Subtraction: a - b = %d\n", a - b);10 printf("Multiplication: a * b = %d\n", a * b);11 printf("Division: a / b = %d\n", a / b);12 printf("Modulus: a %% b = %d\n", a % b);13 14 return 0;15}Expected Output:
Addition: a + b = 19
Subtraction: a - b = 11
Multiplication: a * b = 60
Division: a / b = 3
Modulus: a % b = 3
12Step-by-Step Precedence Solving
Understanding operator precedence is crucial for writing correct C code. Let's solve some complex expressions step-by-step.
Example 1: Arithmetic Precedence
20 * 30 = 600600 / 5 = 12010 + 120 = 130130 - 15 = 115Example 2: Mixed Operators
3 * 2 = 6, 8 / 4 = 25 + 6 = 1111 > 10 = 1 (true), 2 == 2 = 1 (true)1 && 1 = 1 (true)Example 3: Assignment with Operations
result = a++ + ++b * 2
4 * 2 = 85 + 8 = 13Precedence Quick Reference (High to Low)
| Priority | Operators | Description |
|---|---|---|
| 1 (Highest) | () [] -> . | Parentheses, array, member access |
| 2 | ++ -- ! ~ sizeof | Unary operators (right to left) |
| 3 | * / % | Multiplication, division, modulus |
| 4 | + - | Addition, subtraction |
| 5 | < <= > >= | Relational comparison |
| 6 | == != | Equality comparison |
| 7 | && | Logical AND |
| 8 | || | Logical OR |
| 9 (Lowest) | = += -= *= /= | Assignment (right to left) |
When in doubt, use parentheses () to make your intention clear!
14Frequently Asked Questions
Q:What's the difference between = and ==?
= is the assignment operator — it puts a value into a variable: x = 5;
== is the equality operator — it compares two values and returns 1 (true) or 0 (false): if (x == 5)
Using = instead of == in conditions is one of the most common bugs in C!
Q:Why does 5 / 2 give 2 instead of 2.5?
When both operands are integers, C performs integer division, which truncates (cuts off) the decimal part.
To get 2.5, make at least one operand a float/double:
- •
5.0 / 2→ 2.5 - •
5 / 2.0→ 2.5 - •
(double)5 / 2→ 2.5
Q:When should I use && vs & (and || vs |)?
&& and || are logical operators — use them for conditions (if statements, loops).
& and | are bitwise operators — they work on individual bits of integers.
99% of the time, you want the double versions (&&, ||) for logic!
Q:What's the modulus operator (%) used for?
The modulus operator returns the remainder after division. Common uses:
- • Check if even/odd:
if (n % 2 == 0)→ even - • Wrap around:
index % arraySize→ keeps index in bounds - • Extract digits:
n % 10→ last digit - • Divisibility:
if (n % 5 == 0)→ divisible by 5
Q:What does the ternary operator ?: do?
It's a shorthand for if-else that returns a value:
Example: int max = (a > b) ? a : b;
This means: "If a > b, max is a; otherwise, max is b."
14Summary
What You Learned:
- ✓WHY: Operators let us perform math, compare values, and make decisions
- ✓Arithmetic: +, -, *, /, %, ++, --
- ✓Relational: ==, !=, >, <, >=, <=
- ✓Logical: &&, ||, !
- ✓Assignment: =, +=, -=, *=, /=, etc.
- ✓Precedence: *, / before +, - before comparisons before &&, ||
Test Your Knowledge
Related Tutorials
Operator Precedence & Associativity
Understand which operators are evaluated first. Learn the PUMAS REBL TAC memory trick and when to use parentheses.
Type Casting & Type Conversion
Learn how to convert between data types in C. Master implicit (automatic) and explicit (manual) type casting, avoid integer division bugs, and understand when data loss occurs.
Data Types & Variables
Learn to store different kinds of data: numbers (int), decimals (float), and characters (char). Understand how much memory each type uses.
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!