CHAPTER 06
Beginner
User Input and Output
Updated: May 17, 2026
5 min read
# CHAPTER 6
User Input and Output
1. Introduction
So far, our programs have been static — the data was hardcoded. Real applications interact with users. They ask for names, ages, and passwords. In C, we handle this interaction using standard input (keyboard) and standard output (screen) functions provided by the<stdio.h> library.
2. Learning Objectives
-
Use
printf()to output formatted data.
-
Use
scanf()to read user input.
- Understand format specifiers deeply.
- Master escape sequences.
- Address common input buffer issues.
3. Output with printf()
The printf() function sends formatted output to the screen.
c
4. Input with scanf()
The scanf() function reads formatted input from the keyboard.
c
Why the & (Ampersand)?
scanf needs to know *where in memory* to store the data you typed. The & symbol means "address of". So &age means "store this input at the memory address of the variable age."
5. Format Specifiers Reference
| Specifier | Type | Example |
|---|---|---|
%d or %i | Integer | 25 |
%f | Float | 3.14159 |
%lf | Double | 3.14159265 |
%c | Character | 'A' |
%s | String (Text) | "Hello" |
%p | Pointer (Memory Address) | 0x7ffee123 |
%x | Hexadecimal | ff |
6. Escape Sequences
Special character combinations that format text output.-
\n: New line
-
\t: Tab (horizontal spacing)
-
\\: Prints a backslash
-
\": Prints a double quote
c
7. Reading Multiple Inputs
You can read multiple values in a singlescanf().
c
8. Mini Project: Student Information System
c
9. Memory-Level Explanation
When you runscanf("%d", &age);:
- 1. The program pauses and waits for the OS to send keyboard data (stdin).
-
2.
You type
25and hit Enter. The buffer holds25\n.
-
3.
scanfreads the25, converts it to binary, and writes it directly to the memory address provided by&age.
-
4.
The
\nremains in the buffer (which can cause bugs for futurescanfcalls!).
10. Common Mistakes
-
Forgetting the
&in scanf:scanf("%d", age);will cause the program to crash (Segmentation Fault) because it tries to write to memory address 20 (or whatever value age held).
-
Using
&with strings: When reading strings (%s), you don't need the&(we'll explain why in Chapter 11).
-
The Input Buffer Issue: If you read an integer, then try to read a character,
scanf("%c")might accidentally read the "Enter" key (\n) left behind by the integer input. Add a space before%c:scanf(" %c", &var);to fix it.
11. Exercises
-
1.
Write a program that asks for a temperature in Celsius and prints it in Fahrenheit.
(F = C * 9/5 + 32).
-
2.
Write a program that uses
\tto print a neatly aligned grocery receipt.
12. MCQ Quiz with Answers
Question 1
Which function is used for reading user input?
Question 2
What does the & operator do in scanf()?
Question 3
Which library must be included to use printf and scanf?
Question 4
What is the format specifier for a single character?
Question 5
How do you print a double quote inside a printf string?
Question 6
What happens if you forget & in scanf("%d", var)?
Question 7
What does %.2f do?
scanf read multiple variables at once?
a) Yes b) No
Answer: a) Yes
Question 9
Which escape sequence creates a tab space?
Question 10
Standard input in C usually comes from:
13. Interview Questions
-
Q: Why do we pass the address (
&) of a variable toscanfinstead of the variable itself?
-
Q: What is the input buffer, and why does reading a
%cafter a%dsometimes fail?
-
Q: How can you prevent buffer overflows when reading strings using
scanf?
14. Summary
Interactivity in C is handled via<stdio.h>. printf() outputs data using format specifiers and escape sequences. scanf() reads data, requiring the memory address (&) of the variables where data should be stored.
15. Next Chapter Recommendation
Now that our programs can accept input, we need them to make decisions. In Chapter 7: Conditional Statements, we will learn how to useif, else, and switch.