Input with scanf()
scanf(), declared in <stdio.h>, reads formatted input from standard input (typically the keyboard). Like printf, it takes a format string describing what to read, followed by pointers to the variables that should receive the parsed values.C
#include <stdio.h>
int main(void) {
int age;
double price;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter a price: ");
scanf("%lf", &price);
printf("Age: %d, Price: %.2f\n", age, price);
return 0;
}Never Forget the & for Scalar Variables
Missing & is one of THE most common C beginner bugs
scanf needs the address of each variable so it can write the parsed value directly into memory — that's what the & (address-of) operator provides. Forgetting it for a scalar variable like int or double passes the variable's current value instead of its address. scanf then interprets that value as a memory address and tries to write there, which typically causes a segfault or other undefined behavior. (The one common exception is a char array used as a string buffer, which already decays to an address — no & needed there.)C
int age;
/* scanf("%d", age); */ // BUG: missing & — passes a garbage value as an address
scanf("%d", &age); // CORRECT — passes the address of age
char name[50];
scanf("%s", name); // CORRECT — arrays decay to a pointer automatically, no & neededAlways Check scanf's Return Value
Ignoring the return value hides malformed input
scanf returns the number of input items successfully matched and assigned — which can be less than the number of conversion specifiers if the input doesn't match the expected format (or reaches end-of-file). Code that ignores this return value will silently continue with uninitialized or stale variable values when the user types something unexpected.C
int age;
int matched = scanf("%d", &age);
if (matched != 1) {
printf("Invalid input — expected a number\n");
} else {
printf("You entered: %d\n", age);
}The Trailing Newline Gotcha
When a user types a number and presses Enter,
scanf("%d", ...) consumes the digits but leaves the newline character sitting in the input buffer. If the next input call is getchar() or fgets(), that leftover newline is read immediately, often making it look like the following input was skipped entirely.C
#include <stdio.h>
int main(void) {
int age;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your name: ");
/* BUG: fgets immediately reads the leftover '\n' from the previous
scanf call and returns an empty string, before the user can type. */
fgets(name, sizeof(name), stdin);
printf("Age: %d, Name: %s\n", age, name);
return 0;
}The fix: consume the leftover newline explicitly
After a numeric
scanf call that will be followed by line-based input, drain the rest of the current line first — either with a small loop calling getchar() until a newline or EOF, or by adding a leading space in the next scanf format string (a space in a scanf format matches and discards any amount of leading whitespace, including newlines).C
int age;
char name[50];
scanf("%d", &age);
while (getchar() != '\n') {
/* discard the rest of the line, including the leftover newline */
}
printf("Enter your name: ");
fgets(name, sizeof(name), stdin); // now reads the real input correctlyscanfwrites into the address you give it — always pass&variablefor scalarsAlways check the return value of
scanf(number of items matched) before trusting the resultscanf("%d", ...)leaves the trailing newline in the input buffer — drain it before callinggetchar/fgets