Lectu 3: Operators in C
Page 2: Quick recap
- Last class…
- C code organization
- Compilation process
- Today's class…
- Operators in C
Page 3: Arithmetic Operators and Precedence
Arithmetic Operators and Precedence
- Operator names, with their descriptions and examples:
- + Addition: Adds together two values
- Example: x + y
- In code: sum = x + y;
- Math representation:
- - Subtraction: Subtracts one value from another
- Example: x - y
- In code: sub = x - y;
- Math representation:
- * Multiplication: Multiplies two values
- Example: x * y
- In code: mul = x * y;
- Math representation: or
- / Division: Divides one value by another
- Example: x / y
- In code: div = x / y;
- Math representation:
- % Modulus: Returns the division remainder
- Example: x % y
- In code: mod = x % y;
- Math representation: not a simple single fraction, represents remainder
Precedence note
- Precedence of operators follows general math rules: multiplicative operators have higher precedence than additive operators; relational and logical operators typically have lower precedence than arithmetic operators.
- For emphasis, multiplicative: come before additive: ; then relational, then logical operators.
General guidance
- These rules affect evaluation order in expressions unless parentheses override order.
Code example
int main() {
int x = 4, y = 3;
int sum, sub, mul, div, mod;
sum = x + y;
sub = x - y;
mul = x * y;
div = x / y;
mod = x % y;
}
Page 5: Relational Operators
- Relational operators and their meanings
- == Equal to
- != Not equal
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
- Example code
int main() {
int x = 4, y = 3, res;
res = x == y;
res = x != y;
res = x > y;
res = x < y;
res = x >= y;
res = x <= y;
}
- Notes
- The expressions yield boolean-like results represented as integers in C (0 for false, nonzero for true).
Page 6: Logical Operators
- Logical operators and their semantics
- && Logical and: Returns true if both statements are true
- || Logical or: Returns true if one of the statements is true
- ! Logical not: Reverses the result
- Example usage and conventions
- In C, true is represented by a nonzero value; false is represented by 0. In many contexts, true is 1.
- When using bool type, you typically include
and use true/false literals.
- Example code
#include <stdbool.h>
int main() {
bool x = true, y = false;
bool res;
res = x && y; // false (0)
res = x || y; // true (1)
res = !x || y; // false (0)
res = 2 && 4; // true (1) because both are nonzero
res = 0 || !(-3); // 0 || true => true (1)
}
- Important notes
- False is 0; True is nonzero (commonly 1 when using bools).
- The code sample shows that nonzero integers are treated as true in logical contexts.
Page 8: Bitwise Operators
- Bitwise operators and their purposes
- & Bitwise AND: Returns 1 only if both bits are 1
- | Bitwise OR: Returns 1 if at least one of the bits is 1
- ^ Bitwise XOR: Returns 1 if the operands are different
- Descriptive caveat
- Bitwise operators operate on individual bits and are distinct from logical operators; the latter operate on truthiness of entire expressions.
- Example code
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Result: 0001 (1 in decimal)
}
Page 9: Assignment Operators
- Assignment operators and their equivalences
- = C = A + B will assign the value of (A + B) to C
- += C += A is equivalent to C = C + A
- -= C -= A is equivalent to C = C - A
- *= C *= A is equivalent to C = C * A
- /= C /= A is equivalent to C = C / A
- %= C %= A is equivalent to C = C % A
- Notes
- These operators combine arithmetic with assignment in a single operation for brevity and clarity.
Page 10: Increment/Decrement Operators
- Increment and Decrement descriptions
- ++ Increases the value of a variable by 1
- -- Decreases the value of a variable by 1
- Prefix vs postfix
- i++ Postfix increment: value used then incremented
- ++i Prefix increment: value incremented then used
- i-- Postfix decrement: value used then decremented
- --i Prefix decrement: value decremented then used
- Example code
int main() {
int x = 4, res;
res = x++;
res = --x;
x--;
++x;
}
Page 11: Do Now — L3 Worksheet and Operators Quiz
- Activity setup
- Do now: L3 Worksheet (join a partner, max. 3)
- Q1.3 | Operators Quiz (individual)
- Context
- NAU NORTHERN ARIZONA UNIVERSITY
- Purpose
- Reinforce understanding of operators covered: arithmetic, relational, logical, bitwise, assignment, and increment/decrement.
Cross-page notes and context
- Quick recap of course framing
- CS 136 - Computer Science II
- Module 1: Introduction to C Programming
- Topics include C code organization, compilation process, and operators in C
- Practical emphasis
- Concrete examples demonstrate how to translate operator concepts into actual C code
- Distinctions between similar concepts (logical vs bitwise, prefix vs postfix) highlighted across examples
- Foundational ties
- These operators underpin control flow decisions, numerical computations, and data manipulation in C programs
- Real-world relevance
- Understanding operator behavior is essential for correct program logic, optimization, and avoiding common pitfalls (e.g., operator precedence, boolean evaluation, and side effects in increments)
- Ethical/practical notes
- Clear, correct code reduces bugs and maintenance burden, which aligns with professional software development ethics and quality standards