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.

All topics in C Beginner

  1. Introduction to Programming & C
  2. Your First C Program
  3. Variables & Data Types
  4. Input & Output
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Arrays
  10. Strings
  11. Pointers
  12. Structures
  13. Mini-Projects & Practice