How to Read User Input in C with scanf

7

Constants are convenient. They are static. But they are also boring.

A program that always adds 5 and 7 does exactly one thing. It doesn’t adapt. It doesn’t interact. To make C code actually useful for end users, you need to stop hardcoding values and start listening.

The tool for that job is scanf.

From Hardcoded to Dynamic

In the previous example, the variables a and b were likely initialized with fixed numbers. The logic was rigid. The output was predictable. If you wanted to change the addition, you had to edit the source code, recompile, and run it again.

That is not user-friendly.

To fix this, we need to capture input from the console. The standard library handles both output (printf ) and input (scanf ). The shift from constants to user-defined values changes the program from a calculator demo into a simple utility.

The scanf Syntax

Here is the code that makes it happen:

The logic is straightforward. It is linear.

  1. The program prompts for the first integer.
  2. scanf waits for input.
  3. The user types a number and hits enter.
  4. The value is stored in a.
  5. Repeat for b.
  6. Add them.
  7. Print the result.

The Ampersand (&) Confusion

The most common point of failure for beginners here is the & symbol.

Look at this line:
scanf("%d", &a);

Why is there an ampersand? Why not just scanf("%d", a);?

Variables in C are not just names. They are memory addresses. When you declare int a, you are reserving a chunk of RAM. scanf does not want the value of a because a is currently empty (or contains garbage). scanf needs the address where it can write the new value.

The & is the “address-of” operator. It gives scanf the location in memory so it can modify a directly. Without it, you are pointing scanf to a random piece of memory. The program will likely crash, or worse, corrupt your data.

Key insight: printf uses the value. scanf needs the address.

Format Specifiers

You used %d in printf to display an integer. You must use %d in scanf to read one.

Mismatching these causes undefined behavior. If you try to read an integer using %f (float), the data will be interpreted incorrectly. The memory layout of a float is different from an integer. scanf will write the raw bits of a float

How scanf and printf handle variables in C

Compiling and running the code is where theory meets reality. If you skip this step, you are just guessing. The scanf function operates similarly to printf regarding format strings, but it has a stricter requirement for input. You must place the address operator (& ) before variables of type char, int, float, and structures. Without it, the program crashes.

Why scanf requires the address operator

The ampersand is not optional. It tells the compiler to store the input directly into the memory location of the variable. If you omit it, you trigger a runtime error. You should try removing it intentionally. Seeing the crash is more educational than reading about it. It shows exactly how fragile C can be when memory addresses are mismanaged.

How printf formats output for users

printf sends text to standard output. The simplest version looks like this:

This prints the word “Hello” with no newline. Compare that to:

The \n adds a carriage return. It ends the line. Without it, the next output appears on the same line. It is a small detail that ruins readability if ignored.

How to embed variables in printf strings

To output a variable’s value, you use a placeholder. The %d operator replaces itself with the integer value of the variable. Consider this example:

You can chain separate printf calls to mix text and variables, but it is inefficient:

The cleaner approach embeds the placeholder in the string:

Multiple placeholders work too:

The number of placeholders must match the number of variables exactly. Types must also correspond. Three %d specifiers require three int parameters in the same order. Mismatched types or counts lead to undefined behavior.

Which placeholders handle different C types

printf supports standard C types through specific format specifiers:

  • int uses %d
  • float uses %f
  • char uses %c
  • character strings use %s

You can find detailed documentation by typing man 3 printf on a UNIX machine. Other compilers provide similar manual pages or help files.

Common C errors to avoid

Case sensitivity is strict. Printf or PRINTF will fail. It must be lowercase printf.

  • Forgetting the & in scanf causes runtime crashes.
  • Mismatching parameter counts in printf or scanf corrupts output.
  • Using undeclared variables results in compilation errors.

Debugging these issues often comes down to checking syntax first. The compiler will usually tell you exactly where the mismatch lies. You just have to read the error message.