A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
scanf is printf's inverse, but it is far less forgiving. Where a printf mismatch gives you garbage output, a scanf mismatch corrupts memory silently. The program keeps running. The variable holds nonsense. You do not find out until three functions later when that nonsense becomes an array index. This is how security vulnerabilities start.
scanf needs the address of the variable it will write into. You pass &age, not age. The one exception is char arrays (strings), where the array name already decays to a pointer. Forget the ampersand on an int and scanf writes the input value to whatever address happens to be stored in that int variable. On a good day, you segfault immediately. On a bad day, you corrupt a neighboring variable and spend an evening hunting it.
#include <stdio.h>int main(void) {int age;float height;char name[50];printf("Enter your age: ");scanf("%d", &age);printf("Enter your height in meters: ");scanf("%f", &height);printf("Enter your name: ");scanf("%49s", name);printf("\n%s is %d years old, %.2f m tall.\n", name, age, height);return 0;}
The buffer is 50 bytes. One of those bytes must hold the null terminator. So the maximum safe input length is 49 characters. Writing %s with no width limit tells scanf to keep reading until whitespace, regardless of buffer size. That is a textbook buffer overflow. Every static analysis tool flags it. Every security review catches it. And yet it appears in production code constantly because beginners copy examples from the 1990s.
Rule: every %s in a scanf format string must have a width specifier that is one less than the destination buffer size. No exceptions. Not even in throwaway test code, because throwaway test code has a habit of becoming production code.
scanf returns the number of items successfully read. If you ask for one int and the user types "abc", scanf returns 0 and leaves the variable uninitialized. If you do not check that return value, you proceed with garbage. Always check.
#include <stdio.h>int main(void) {int value;printf("Enter an integer: ");int result = scanf("%d", &value);if (result != 1) {printf("That was not a valid integer.\n");return 1;}printf("You entered: %d\n", value);return 0;}
There is a subtlety here. When scanf fails to match, it leaves the offending characters in the input buffer. The next read attempt hits the same characters and fails again. You get an infinite loop if this is inside a while loop. We will cover clearing the input buffer properly in a later lesson. For now, know that the problem exists.
Input handling in C is a minefield dressed up as a simple function call. The tools exist to do it safely, but the language does not force you to use them. That philosophical choice, freedom versus safety, defines C. Next lesson we start making decisions in code with if statements.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.