C Operators
Calculate and compare values with operators whose types, order, and side effects you can explain.
Arithmetic operators transform numeric values
Multiplication, division, and remainder happen before addition and subtraction. When both operands of / or % are integers, the result is an integer: 17 % 5 is 2, and 7 / 2 is 3. If a fraction matters, make at least one operand floating-point, as C Data Types explains.
Add and subtract
Combine quantities or calculate a difference.
Multiply and divide
The types of both operands decide whether division keeps a fraction.
Integer remainder
Defined for integers in this course. Do not treat it as a floating-point operator.
Calculate groups and a remainder
Change the chapter count or group size. Predict total_pages, whole_groups, and spare_pages before running.
Edit the program, predict its output, then run it.
Assignment stores; increment updates by one
= is not a math claim. It evaluates the right side, then stores the result in the left-side object. Compound forms such as += and -= are shorthand for “read, calculate, store.” Prefix ++n updates then yields the new value; postfix n++ yields the old value then updates. In beginner code, n = n + 1; is often the clearest form.
Trace a reassignment
Run once, then change the amount added to checked_in.
Edit the program, predict its output, then run it.
Comparison and logical operators produce conditions
Equality needs two characters: == compares, = assigns. Relational operators are < <= > >=. Logical && requires both sides, || accepts either, and ! inverts. C treats zero as false and nonzero as true. Using these operators inside if is the job of C If Else; this lesson is the catalog of the operators themselves.
Combine a comparison and a logical AND
Change score to 70, 69, and 0. Predict the message before each run.
Edit the program, predict its output, then run it.
Bitwise operators & | ^ ~ << >> exist in C. This course uses them only when a later systems topic needs them; do not treat them as a substitute for && and ||.
Make grouping visible
C evaluates ! before comparisons, comparisons before &&, and && before ||. Arithmetic * / % bind tighter than + -. You do not need to rely on that table for a complicated rule: parentheses or a named intermediate value make the intended grouping obvious.
Compile mixed expressions with conversion warnings
clang -std=c11 -Wall -Wextra -Wconversion operators.c -o operatorsgcc -std=c11 -Wall -Wextra -Wconversion operators.c -o operators/W4 and inspect every conversion diagnostic.cl /W4 operators.cIndependent lab: one named calculation
Write a program that computes a total with *, a group count with /, and a remainder with %, then prints whether the remainder is zero using ==. Predict each line before you run it.
Lesson review
- Arithmetic, assignment, increment, comparison, and logical operators are separate jobs.
- Integer
/and%stay in the integer domain. ==compares;=stores.- Parentheses or named intermediates beat a memorized precedence table.