Chapter 522 min read
C Operators, Expression Evaluation & Memory Operations
A clear guide to mathematical, logical, bitwise, memory, and comparison operators in C.
Released1972
Core MottoLow-Level Bit Manipulation & Fast Operator Precedence
CreatorsDennis Ritchie / Bell Labs
1. What is an Operator & Arity?
Operators are symbols that instruct the computer to perform calculations or logic on variables/values (operands).
Arity refers to how many inputs (operands) an operator needs:Unary (1), Binary (2), or Ternary (3).
Understanding Operators & Operands
Operator:The action symbol (e.g., `+`, `-`, `*`).
Operand:The values or variables being acted upon (e.g., in `10 + 5`, `10` and `5` are operands).
Unary Operators (1 Input):Operates on one value. Examples: `-5` (flips sign), `++x` (adds 1 to x), `!flag` (flips true/false).
Binary Operators (2 Inputs):Needs two values. Examples: `a + b`, `x > y`.
Ternary Operator (3 Inputs):Uses three parts to make a fast inline decision: `(condition) ? value_if_true : value_if_false`.
Arithmetic Operators & The Integer Division Trap
Addition (+), Subtraction (-), Multiplication (*).
Division (/):Dividing two whole numbers cuts off the decimal entirely! For example, `7 / 2` equals `3` (not `3.5`). To get decimals, at least one number must be a floating point value (`7.0 / 2`).
Modulus (%):Returns the remainder after integer division. Example: `10 % 3` equals `1` (because 10 divided by 3 is 3 with a remainder of 1).
c
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Quotient (10 / 3): %d\n", a / b); // Outputs: 3
printf("Remainder (10 %% 3): %d\n", a % b); // Outputs: 1
// Decimal division fix
printf("Decimal (10.0 / 3): %.2f\n", 10.0 / b); // Outputs: 3.33
return 0;
}2. Relational, Logical & Compound Operators
Relational operators compare values and return 1 for True or 0 for False.
Logical operators combine conditions with AND, OR, and NOT rules.
Relational Operators (Comparisons)
Checks relationships:`==` (equal to), `!=` (not equal), `>` (greater than), `<` (less than), `>=` (greater than or equal), `<=` (less than or equal).
Return Values:C represents True as `1` and False as `0`.
Common Pitfall:Do not confuse `=` (assigning a value) with `==` (checking equality). Writing `if (x = 5)` sets x to 5 instead of comparing it!
Logical Operators & Short-Circuit Evaluation
Logical AND (&&):True only if BOTH conditions are true (e.g., `age >= 18 && hasID == 1`).
Logical OR (||):True if AT LEAST ONE condition is true.
Logical NOT (!):Flips True to False and False to True.
Short-Circuit Protection:C stops evaluating early if the outcome is guaranteed. In `(0 && do_something())`, C skips `do_something()` because an AND can never be true if the first part is false.
c
#include <stdio.h>
int main() {
int age = 20;
int hasLicense = 1;
if (age >= 18 && hasLicense) {
printf("You are allowed to drive!\n");
}
// Compound assignments update variables in place
int score = 50;
score += 25; // Equivalent to: score = score + 25 (75)
score *= 2; // Equivalent to: score = score * 2 (150)
printf("Final Score: %d\n", score);
return 0;
}3. Memory, Bitwise & Pointer Operators
C provides special low-level operators to inspect byte sizes, look up RAM memory addresses, and manipulate individual binary bits.
Memory & Pointer Operators
sizeof:Tells you how many bytes of RAM a variable or data type consumes (e.g., `sizeof(int)` is usually 4 bytes).
Address-of (&):Finds the exact location in RAM where a variable lives.
Dereference (*):Gets the value stored inside a memory address pointer.
Bitwise Operators & Ternary Shortcut
Bitwise Math:Operates directly on binary `0`s and `1`s: `&` (Bitwise AND), `|` (Bitwise OR), `^` (Bitwise XOR), `~` (Bitwise NOT), `<<` (Shift Left), `>>` (Shift Right).
Ternary Shortcut:Replaces simple `if-else` blocks with a single readable line.
c
#include <stdio.h>
int main() {
int age = 20;
int x = 10;
int *ptr = &x; // '&' gets the memory address of x
printf("Bytes used by int: %lu bytes\n", sizeof(int));
printf("Value at address (*ptr): %d\n", *ptr);
// Ternary operator: (condition) ? if_true : if_false
const char *status = (age >= 18) ? "Adult" : "Minor";
printf("Status: %s\n", status);
return 0;
}4. Operator Precedence (Order of Operations)
Precedence determines which operations happen first in complex equations, similar to standard math rules.
Order of Operations Priority
1.
Parentheses `()` (Highest Priority - Always runs first)
2.
Unary / Increment / Size `++`, `--`, `!`, `sizeof`
3.
Multiplicative `*`, `/`, `%`
4.
Additive `+`, `-`
5.
Relational `<`, `>`, `<=`, `>=`
6.
Equality `==`, `!=`
7.
Logical `&&` then `||`
8.
Ternary `?:`
9.
Assignment `=`, `+=`, `-=` (Lowest Priority)
Precedence Example
Without Parentheses:`7 + 3 * 2` equals `13` because multiplication runs before addition.
With Parentheses:`(7 + 3) * 2` equals `20` because parentheses override standard precedence.
c
#include <stdio.h>
int main() {
int result1 = 7 + 3 * 2; // 3 * 2 = 6, then + 7 = 13
int result2 = (7 + 3) * 2; // (7 + 3) = 10, then * 2 = 20
printf("Standard Precedence: %d\n", result1);
printf("Overridden with (): %d\n", result2);
return 0;
}Classroom Discussion Starter
Why should you avoid writing tricky statements like 'a = i++ + ++i' in real projects?
Takeaway Goal: Modifying the same variable multiple times inside one line leads to Undefined Behavior in C. Different compilers can process the additions in different orders, leading to unpredictable bugs.