Curriculum / C / Lesson 05

Input & Output in C

C uses two core functions for console I/O: printf() to print output, and scanf() to read input from the keyboard.

5 of 11
beginner 📖 4 min read 🎯 1 question

Input & Output

C uses two core functions for console I/O: printf() to print output, and scanf() to read input from the keyboard. Both live in stdio.h.

scanf needs the memory address of a variable (using &) so it knows exactly where to store what you type.

💡 Key insight Forgetting the <code>&amp;</code> before a variable in <code>scanf</code> is the single most common beginner bug in C.

Syntax

syntax
printf("format string", values...);
scanf("format string", &variable);

Example

&age passes the address of age so scanf can write the typed value directly into that memory location.

c
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You are %d years old.\n", age);
    return 0;
}
outputEnter your age: 20 You are 20 years old.

Try it yourself

Copy this example into the C compiler and run it — try changing a value and see what happens.

Quick Quiz

1. Why does scanf need '&' before a variable name?

Back to curriculum