Constants are rigid. They don’t change. Life isn’t like that, and neither should your test programs be. If you’re learning C, hard-coding numbers like 5 and 7 gets old fast. You want interaction. You want the computer to wait for you to type something in.
Enter scanf.
It’s the other half of the printf story. While printf pushes data to the screen, scanf pulls data from the keyboard. It’s not the most robust tool in the C standard library, but for simple console input, it gets the job done without needing a third-party dependency.
Here is how you take a static script and make it dynamic.
The Code
Copy this. Run it. Type numbers when it asks.
Breaking Down scanf
The magic happens in these two lines:
scanf("%d", &a);
scanf: The function name. Short for “scan formatted input.”"%d": The format specifier. It tellsscanfto expect an integer. If you were reading a float, you’d use"%f".&a: This is the critical part. You aren’t passing the value ofa. You’re passing the address ofa.
Think of it this way: printf needs to know what’s inside the variable. scanf needs to know where to put the data. The & operator provides that memory address. Skip the ampersand, and you’ll crash. Or worse, you’ll get a warning that looks like gibberish and a variable that doesn’t change.
How It Works in Practice
When you run the program:
- The terminal prints:
Enter the first value: - The cursor blinks. It waits. You’re stuck here until you type something and hit Enter.
- You type
5. You hit Enter. scanfgrabs that5, converts the string of characters “5” into the integer value 5, and stores it at memory address&a.- The same loop happens for the second value.
Then comes the math. c = a + b;. Simple addition.
Finally, printf displays the result. But notice the format string: printf("%d + %d = %d\n", a, b, c);. You’re injecting the variables back into the string. This is how you create readable output.
Why This Matters
Most modern applications don’t use console input. You don’t type commands into a black box for your web browser or your photo editor. But understanding this flow is foundational.
Input -> Process -> Output.
That’s the basic loop of all software. `
How scanf handles memory addresses
When you run this code, the difference between success and a runtime crash comes down to one character. Specifically, the ampersand (& ) before variables a and b.
This is the address operator. It tells scanf exactly where in memory to store the input it receives. Without it, scanf doesn’t know the location of the variable. It just gets a value, which is useless for storing data back into the program. If you omit it, the program won’t compile or will crash immediately upon execution. You need this operator for char, int, float, and structure types.
Try removing it. Watch what happens. It is a painful but necessary lesson in how C manages memory.
Mastering printf output
printf is your window to the user. It sends data to standard output. The simplest version is straightforward.
This prints the word Hello. Nothing else. No line break. The cursor stays right after the ‘o’.
Add a newline character.
Now the output moves to the next line. The \n is a carriage return. It is essential for readable console output.
Variables change the game. You cannot just print a variable name like you do in Python or JavaScript. You must use a placeholder.
The %d is a placeholder. It gets replaced by the integer value of b at runtime. This is how C handles dynamic output.
You can mix text and variables. One clumsy way is chaining multiple prints.
It works. It is also verbose. The cleaner approach embeds everything in one string.
Multiple values work too. Just keep the placeholders aligned with the arguments.
The order matters. The first %d maps to a. The second to b. The third to c. If you swap them, the math is wrong. If you miss one, the behavior is undefined. The number and type of operators in the format string must match the parameters exactly.
Type-specific placeholders
C is strict about types. You cannot use the same placeholder for everything. You must match the data type to the format specifier.
- Use %d for int (integer values).
- Use %f for float (floating point numbers).
- Use %c for char (single characters).
- Use %s for character strings (arrays of characters).
If you use %d for a float, you get garbage output. If you use %s for an int, the program likely crashes. Precision is not optional here.
For deeper details on these specifiers, check the UNIX manual. Type man 3 printf. Other compilers provide similar documentation. It is worth reading.
Common pitfalls to avoid
C compilers are helpful, but they are not mind readers. You will make mistakes. Here is how to avoid the most common ones.
- Case sensitivity : C is case-sensitive.
PrintforPRINTFwill fail. It must beprintf. The same applies to variable names and function calls. - Missing the address operator : Forgetting the
&inscanfis a frequent error. It causes immediate runtime failure. Always include it for primitive types. - Mismatched parameters : Too many or too few arguments in
printforscanfleads to errors. The format string dictates the structure. Stick to it. - Undeclared variables : You must declare a variable before using it. C does not infer types. You define
int a;before you can printa.
These errors are repetitive. They are also easy to fix once you recognize the pattern.





























