C Operators and Expressions
Learn all the operators in C: math (+, -, *, /), comparisons (==, <, >), and logical operations (&&, ||). Build expressions that compute values.
What You Will Learn
- โUse arithmetic operators (+, -, *, /, %)
- โCompare values with relational operators
- โCombine conditions with && (AND) and || (OR)
- โIncrement/decrement with ++ and --
01What are Operators?
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 |
๐ This program 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) |
๐ This program 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 |
๐ This program 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)
๐ This program 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 |
๐ This program 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 |
๐ This program 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 |
๐ This program 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.
10Common Mistakes
โ Using = instead of ==
if (a = 5) { } // WRONG! This assigns 5 to aif (a == 5) { } // Correct! This compares a to 5โ Integer 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.5โ Confusing & with &&
if (a & b) { } // Bitwise AND (rarely intended)if (a && b) { } // Logical AND (usually intended)11Summary
What You Learned:
- โArithmetic: +, -, *, /, %, ++, --
- โRelational: ==, !=, >, <, >=, <=
- โLogical: &&, ||, !
- โBitwise: &, |, ^, ~, <<, >>
- โAssignment: =, +=, -=, *=, /=, etc.
- โExpressions: Combinations of operands and operators
12Next Steps
Continue your learning journey: