Hardcoding numbers is fine for a first attempt, but real software needs to react to what people type. The original snippet used constants. It’s static. Predictable. Boring.

To make a program useful, you need to pull data from the user at runtime. You do this with scanf. It’s the counterpart to printf. Where printf pushes data out to the screen, scanf pulls it in from the keyboard.

Here is how you handle basic integer input in C.

Capturing User Input

You can’t just declare a variable and expect it to hold user data magically. You need to ask for it. You also need to tell the computer where to store that data. That’s why scanf requires a pointer (the & symbol). If you forget the ampersand, the program will crash or write garbage to memory. Always include it for primitive types like int.

Consider this updated logic:

  1. Declare variables to hold the input and the result.
  2. Prompt the user so they know what to type.
  3. Use scanf with the correct format specifier (%d for integers).
  4. Perform the calculation using the stored values.
  5. Output the result by mixing the input variables into the print string.

Why This Matters

The shift from constants to variables changes everything. The program is no longer a broken record. It adapts. You can run it once with 5 and 7. You can run it again with 100 and 200. The logic remains the same; the data changes.

This is the foundation of interactive CLI (Command Line Interface) applications. Before you can build complex tools, you have to understand how to get data into your variables safely.

Key Takeaways

  • scanf needs a pointer: Always use the address-of operator (& ) before your variable name in scanf.
  • Format specifiers must match: If you are reading an int, you must use %d. Using %f for an integer will cause undefined behavior.
  • Prompts are crucial: Without a clear prompt like “Enter the first value:”, the user is left staring at a blinking cursor, wondering if the program is frozen. It’s not. It’s just waiting.

The code works. It’s simple. But it’s functional. You’ve moved from a static script to a dynamic tool. The next step is usually error handling. What happens if the user types “hello” instead of a number? scanf will fail silently, leaving your variables uninitialized. That’s a problem for another day.

For now, you have a program that actually listens.

Input, Output, and the Rules of Engagement

You’ve made the edits. Now compile. Run it. If it crashes, don’t panic. It probably missed a small syntax detail.

Take scanf. It mimics printf in its use of format strings. Type man scanf if you need the fine print. But pay attention to the ampersand (& ) before variables like a and b.

This is the address operator.

It returns the memory address of the variable. It might look confusing now. Pointers will clarify this later. But for now, follow the rule: if you are using scanf on a char, int, or float, you need the &. You also need it for structures.

Skip the &? You’ll get a run-time error. Break it intentionally. See what happens. It’s the only way to remember why it matters.

Formatting Output Precisely

Let’s dissect printf. Start simple.

printf("Hello");

This sends the word “Hello” to standard output. Nothing else. No newline. The cursor stays right after the “o”.

Now add a newline.

printf("Hello\n");

The \n triggers a carriage return. The output ends on its own line. This is the standard expectation for clean console output.

Variables require placeholders. To print the value of b :

printf("%d", b);

The %d is a placeholder. When the code executes, printf swaps it for the actual integer value of b.

You can embed values in sentences. You could chain multiple calls:

printf("The temperature is "); printf("%d", b); printf(" degrees\n");

But that’s tedious. The cleaner approach combines them into one format string:

printf("The temperature is %d degrees\n", b);

Multiple values work too.

printf("%d + %d = %d\n", a, b, c);

Here is the trap. The number of placeholders in the format string must match the number of variables following it. Exactly.

If you have three %d tags, you need three parameters. The types must match. The order must match. Mismatch these? The output will be garbage. Or worse, it will crash.

Matching Types to Placeholders

printf handles most standard C types. You just need the right code.

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

For deeper nuance, check man 3 printf on a UNIX system. Your specific compiler will likely have a manual or help file that covers edge cases.

Common Pitfalls

New developers trip over these repeatedly.

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

scanf demands the address operator (& ). Forget it, and the program won’t store your input correctly.

Parameter count matters. Too many or too few arguments in printf or scanf leads to undefined behavior.

Declaration comes before usage. You cannot use a variable name in C without declaring it first. The compiler needs to know the memory layout and type beforehand.

The compiler doesn’t guess. It enforces.