Thuta Learning
AdvancedProgrammingbeginner

User Input

Relax. We'll talk through this in plain words — no textbook voice.

Want to read a value the user types on the keyboard? You can use scanf(). Since it needs the variable's address to store the input, you'll see the & operator show up.

c
#include <stdio.h>

int main() {
  int age;

  printf("Enter your age: ");
  scanf("%d", &age);

  printf("You are %d years old.", age);
  return 0;
}

scanf("%d", &age) reads an integer input and stores it at the memory address of the age variable.

You should see
(If the user enters 20) Enter your age: 20 You are 20 years old.

Info

printf() takes the variable's value, but scanf() needs the variable's address instead — keep that distinction in mind.

Easy traps

  • Writing age instead of &age means the input has nowhere valid to be stored — expect an error or a crash.
User Input | Thuta Learning