Input and Output with printf and scanf
Input is not a value until a program converts it, stores it in the right place, and checks that the conversion succeeded. Learn the complete, typed path from standard input through scanf to a clear printf result.
Follow data in both directions
scanf store it in typed variables.Output makes the program state observable
printf starts with a format string. Ordinary characters appear as written; a placeholder reserves a place for the next value after the string. The placeholder must describe that value accurately. In this course, use %d for an int, %f for a double, and %s for a text reference. Each placeholder consumes one following argument, from left to right.
Format a typed output line
Change the activity text or seat count, predict the exact terminal line, then run it. Keep the %s and %d placeholders aligned with their values.
Edit the program, predict its output, then run it.
Print a whole number
%d receives an integer expression such as a count, difference, or named int variable.
Print a decimal quantity
%f displays a double. In this beginner form, printf shows six digits after the decimal point by default.
Print text
%s prints a null-terminated text sequence. The text has already been prepared; printf does not ask the user for it.
Print a percent sign
Use two percent characters in the format string to write one literal percent sign without consuming a value.
Input begins as characters, not as an int or double
When a person types 18, the environment first provides characters. scanf attempts to convert those characters according to its format string. On success, it stores the converted numeric value in a variable. That is why a variable must be declared before the scan, and why the format and target type must agree.
The terminal or Standard input panel supplies characters such as 18 or 1.5.
scanf interprets the next token using its numeric placeholder.
The address marker points to a declared variable where the result belongs.
Only after successful conversion should a calculation or printf read that value.
Read one whole number with scanf
The source below declares attendees without an initial value because input will provide its first value. &attendees means “the address of attendees”: it gives scanf a place to store the converted number. Edit the Standard input panel below, predict the final output, and run the program. The browser runner reads its input panel in token order; a local terminal reads what you type there.
Read an int from Standard input
The runner accepts one whole-number token for %d. Try a different positive, zero, or negative value and trace where it is stored.
Edit the program, predict its output, then run it.
Match scanf formats to the target type
For scanf, use %d with an int target and %lf with a double target. The extra l matters here: it tells scanf to write a double. This differs from printf, where %f displays a double. Memorizing one format character is less reliable than reading the entire pair: function, placeholder, target type, and address.
Read an int and a double in order
The first Standard input token goes to sessions; the second goes to hours_per_session. Spaces and newlines both separate numeric input tokens in this runner.
Edit the program, predict its output, then run it.
Read an int
Use a declared int and its address. The input should be a whole-number token such as 0, 18, or -4.
Read a double
Use a declared double and its address. The input can include a fraction such as 1.5 or -0.25.
Confirm conversion later
Real C returns the number of fields it converted. When conditions arrive next, compare that result with the number you expected before using the input variables.
Separate numeric tokens
For these numeric formats, spaces, tabs, and newlines separate values. A prompt should explain the expected order so a person can provide the right sequence.
Treat failed conversion as a real error state
If the program expects %d but the next input token is many, no valid integer is stored. In a complete C program, check the scanf return value before using the target variable. This course introduces that check concept now; the next lesson supplies the if statement needed to express the full recovery branch safely. The browser runner stops instead of pretending that invalid input produced a usable value.
Repair an address mistake before reading the value
This source has a single defect: the scanf call passes open_seats instead of its address. Leave the Standard input value in place, run the program, read the message, and add only the missing &. Then run it again and explain why the repaired program can safely store the number.
Give scanf a storage address
The runner is intentionally strict about the address marker. Repair the call by changing the smallest possible part of the source.
Edit the program, predict its output, then run it.
Do not use scanf for beginner text input yet
scanf("%s", name) is not a safe first text-input pattern. It needs a writable character array with a known capacity, a field width that prevents overflow, and a plan for spaces and the remaining newline. Those are array and string concepts, so this lesson intentionally limits interactive input to numeric int and double values. Later, you will compare bounded scans with line-based input such as fgets.
Compile locally and read input from a terminal
Save an example as input.c and compile it with warnings enabled. In a terminal, the program waits at scanf until you type the requested values and press Enter. The local compiler is the authority for the full language and will help identify a format or argument mismatch that the browser subset deliberately refuses.
clang -Wall -Wextra -Wpedantic -Wconversion input.c -o input./inputgcc -Wall -Wextra -Wpedantic -Wconversion input.c -o input./inputRead every format-related warning before running the executable. It often identifies the exact format and argument pair that disagree.cl /W4 input.cinput.exeThe command differs by toolchain, but the habit does not: compile, inspect diagnostics, type an expected input shape, then verify the output.Independent lab: build an input-driven workshop report
Use the starter to generate a report from two user-supplied values. Change the workshop label, then provide a new whole-number registration count and decimal duration in Standard input. Add one meaningful calculation using the input—perhaps extra spaces, total minutes, or a different group size—and print it with a matching format. Keep every value's unit clear in its name and in the output label.
Build a typed workshop report
The starter reads registrations and duration, then calculates full groups and remaining learners. Change the data and make the report yours.
Edit the program, predict its output, then run it.
Lesson review
You now know how typed data crosses a program boundary. printf turns known values into readable terminal text. scanf attempts to turn incoming characters into typed values at the addresses you provide. Reliable input code matches every format to its target, explains the expected shape to the user, verifies conversion before using data, and postpones text scanning until memory capacity is understood.
- I can match
%d,%f, and%swith the values passed toprintf. - I can declare an
intordoublebefore scanning, then pass its address with&. - I can explain why
scanfuses%dfor an int and%lffor a double, while printf uses%fto display a double. - I can describe conversion failure, whitespace-separated input order, and why text input must wait for arrays, bounds, and string-safety techniques.