Skip to main content
C Programming Basics
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
123456789101112
#include <stdio.h>

int main() {
    int age = 20;
    float gpa = 3.85;
    
    // Combining text and format specifiers
    printf("I am %d years old.\n", age);
    printf("My GPA is %.1f\n", gpa); // %.1f rounds to 1 decimal place
    
    return 0;
}

4. Input with scanf()

The scanf() function reads formatted input from the keyboard.
c
1234567891011
#include <stdio.h>

int main() {
    int age;
    
    printf("Enter your age: ");
    scanf("%d", &age); // The '&' is CRITICAL!
    
    printf("You are %d years old.\n", age);
    return 0;
}

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

SpecifierTypeExample
%d or %iInteger25
%fFloat3.14159
%lfDouble3.14159265
%cCharacter'A'
%sString (Text)"Hello"
%pPointer (Memory Address)0x7ffee123
%xHexadecimalff

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
12
printf("She said, \"C is awesome!\"\n");
printf("Column1\tColumn2\n");

7. Reading Multiple Inputs

You can read multiple values in a single scanf().
c
1234
int day, month, year;
printf("Enter your birthdate (DD MM YYYY): ");
scanf("%d %d %d", &day, &month, &year);
printf("You were born on %d/%d/%d\n", day, month, year);

8. Mini Project: Student Information System

c
12345678910111213141516171819202122232425
#include <stdio.h>

int main() {
    char initial;
    int age;
    float gpa;

    printf("--- STUDENT INFO REGISTRATION ---\n");
    
    printf("Enter first name initial: ");
    scanf("%c", &initial);
    
    printf("Enter age: ");
    scanf("%d", &age);
    
    printf("Enter current GPA: ");
    scanf("%f", &gpa);
    
    printf("\n--- REGISTRATION SUCCESSFUL ---\n");
    printf("Initial: %c\n", initial);
    printf("Age: %d\n", age);
    printf("GPA: %.2f\n", gpa);

    return 0;
}

9. Memory-Level Explanation

When you run scanf("%d", &age);:
  1. 1. The program pauses and waits for the OS to send keyboard data (stdin).
  1. 2. You type 25 and hit Enter. The buffer holds 25\n.
  1. 3. scanf reads the 25, converts it to binary, and writes it directly to the memory address provided by &age.
  1. 4. The \n remains in the buffer (which can cause bugs for future scanf calls!).

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. 1. Write a program that asks for a temperature in Celsius and prints it in Fahrenheit. (F = C * 9/5 + 32).
  1. 2. Write a program that uses \t to 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?

Q8. Can 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 to scanf instead of the variable itself?
  • Q: What is the input buffer, and why does reading a %c after a %d sometimes 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 use if, else, and switch.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·