Ch 4 · Input & Output · C
Topic 4 of 13 in C — Complete Tutorial (Beginner) — 2 lessons.
Reading Input with scanf
While printf prints output, scanf reads input that the user types. You give scanf a format specifier saying what type to expect (%d, %f, %c), and the address of the variable to store it in — which you write with an & in front of the variable name. The & means "the address of," telling scanf where in memory to put the value it reads.
Formatting Output
printf gives you fine control over how values look. Between the % and the type letter you can add formatting: a number sets a minimum width, and .N sets decimal places for floats. For example %5d prints an integer in a field at least 5 characters wide, and %.2f prints a float with exactly 2 decimal places. This is how you line up neat columns of numbers.