Variables, Types, and Operators
A program becomes useful when names stand for changing information. Learn the small, reliable path from a typed declaration to a calculation, an updated value, and terminal output—without losing track of what each value means.
Make every value carry a clear promise
int, double, or text value with the correct printf placeholder.A variable is a named place for a typed value
Read a declaration from left to right: the type says what kind of value the name may hold, the name gives that value a useful role, and the expression after = supplies the first value. In int remaining = capacity - enrolled;, the program evaluates the right side first, then stores the result in a new whole-number variable named remaining.
Keep three actions separate in your mental model. A declaration introduces a name and type. Initialization gives that newly declared name its first value. An assignment later replaces the value held by an already declared, mutable name. The equal sign is not a math claim; it means “compute the right side, then store it in the left-side variable.”
Store a whole-number calculation
Change capacity or enrolled, predict the remaining-seat count, then run the source. Keep each value's meaning visible in its name.
Edit the program, predict its output, then run it.
Choose the type and give the value a meaningful name.
Compute or provide the first value after the declaration's =.
Use an operator to calculate a new value from known values.
Print the result with a placeholder that matches its type.
Use a type as a useful boundary
C has more types than a first lesson needs. Start with the ones that make everyday data intentions clear. A type does not merely label a value for the compiler: it determines the operations and representation rules that apply to it. Use the smallest type concept that accurately describes the problem, then let later lessons add detail about sizes, arrays, pointers, and conversion rules.
Whole-number quantities
Use int for counts, positions, completed tasks, and other values that should not include a fractional part. Its exact range is implementation-dependent, so do not assume it can store every possible real-world count without checking the requirements.
Fractional numeric values
Use double for quantities such as hours, measurements, and rates when a fraction matters. Binary floating-point values are approximations, so later you will learn why money and exact decimal rules require deliberate design.
Read-only text reference
This beginner form lets a name refer to a text string such as "C study lab". The pointer and string rules deserve their own lesson; for now, use it to give a label to output without trying to edit the string itself.
A value you do not plan to replace
Put const before a declaration when the name should not be assigned a new value through that variable. It documents an invariant and gives the compiler a chance to catch accidental reassignment.
Update a variable by reading, calculating, then storing
An assignment can reuse the current value of the same variable. C evaluates the expression on the right before it changes the left. In checked_in = checked_in + 1;, the old count is read, one is added, and the new count replaces the old value. Nothing “changes itself”; each step has a clear order.
Trace a reassignment
Run the starter once. Then change the amount added to checked_in and explain the old value, the calculation, and the stored result.
Edit the program, predict its output, then run it.
Calculate with operators—and read their order
Arithmetic operators transform numeric values. Multiplication, division, and remainder happen before addition and subtraction. Parentheses make a grouping explicit when the calculation matters to a human reader. Rather than memorizing a long precedence table, write a small named intermediate value or use parentheses whenever a reader could reasonably misread your intent.
Add and subtract
Combine quantities or calculate a difference: open_seats = seats - booked.
Multiply and divide
Scale a quantity or split it into groups. The types of both operands affect division.
Find an integer remainder
For integers, 17 % 5 is 2: five fits three times with two left over.
Calculate groups and a remainder
Change the chapter count or group size. Before running it, predict total_pages, whole_groups, and spare_pages independently.
Edit the program, predict its output, then run it.
Match a value's type to its output format
printf needs a format string and values that match it. In this lesson, pair %d with an int, %f with a double, and %s with a text reference such as const char *. A mismatched format can make a real C program behave unpredictably, so treat each placeholder as part of the value's contract.
Calculate and format a double
This program uses decimal operands, so the division and multiplication remain fractional when needed. The lesson runner displays %f with six digits after the decimal point, as printf does by default.
Edit the program, predict its output, then run it.
Print an int
%d asks printf to interpret the corresponding value as an integer. Use it for whole-number counts and calculation results.
Print a double
%f displays a floating-point value. In this beginner form, standard printf displays six decimal places unless you later specify a precision.
Print text
%s expects a pointer to a null-terminated character sequence. You will study that representation carefully in the arrays-and-strings unit.
Print a percent sign
Two percent characters in the format string request one literal percent sign. This keeps output formatting separate from the value expressions that follow the string.
Protect a fixed rule with const
Use const when a name represents a rule or fixed configuration that this function must not overwrite. The broken source below tries to change max_attempts after promising it is constant. Read the runner message, then make one intentional repair: either preserve the fixed rule by removing the assignment, or make the value truly mutable by changing only const int to int.
Repair a const contract
Do not guess at multiple changes. Decide whether max_attempts is a fixed rule or a changing counter, then make the source match that decision.
Edit the program, predict its output, then run it.
Compile with conversion warnings visible
Save a version of these examples as values.c. A local compiler understands the full C language and your system's implementation choices. Use warning flags while you experiment so a suspicious conversion or formatting mismatch is visible before it becomes a hard-to-find output bug.
clang -Wall -Wextra -Wpedantic -Wconversion values.c -o values./valuesgcc -Wall -Wextra -Wpedantic -Wconversion values.c -o values./valuesUse the diagnostic line and its nearby source, then make the smallest correction that restores the intended type relationship.cl /W4 values.cvalues.exeToolchains use different flag names, but the habit stays the same: read warnings before trusting an apparently successful build.Independent lab: report a study session
Use the starter to produce a short, readable terminal report. Change the session name, seats, booked count, and duration. Then add a new int calculation that has a clear unit—perhaps reserved_seats or remaining_after_waitlist—and print it using the correct placeholder. Your program should have a label, a whole-number calculation, a fractional value, and a visible success status.
Build a typed study-session report
Use the starter's structure, but make all values describe a real session you could explain to another person. Predict the output before running it.
Edit the program, predict its output, then run it.
Lesson review
You now have the core data flow that every later C program will use: choose a type, give the value a meaningful name, initialize it, calculate with the correct operators, update it only when the program's rules permit it, and format the output according to the value's type. The next lesson will add input, which means users will start supplying some of those values themselves.
- I can distinguish a declaration, initialization, and later assignment in a C source file.
- I can choose
intfor a whole-number count,doublefor a fractional quantity, andconst char *for a beginner text label. - I can trace
+ - * / %, state the difference between integer division and remainder, and add parentheses or a named intermediate value when clarity needs it. - I can match
%d,%f, and%sto the values passed toprintf, and useconstto protect a fixed rule.