scanf()
Reads formatted input from stdin.
scanf("format", &variable);This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Reads formatted input from stdin.
The scanf() function reads data from the standard input stream (stdin) and parses it according to the specified format string. It is a variadic function that requires the memory addresses of the destination variables, typically provided using the address-of operator (&). A critical aspect of scanf is its return value: it returns the number of successfully assigned input items. This return value should always be checked to handle invalid input or End-Of-File (EOF) conditions. Technically, scanf performs 'formatted' input, meaning it skips leading whitespace for most specifiers (like %d, %f, %s) but treats whitespace as literal data for others (like %c). Performance-wise, it is generally slower than fgets() followed by sscanf() because it interacts directly with the input buffer and lacks inherent protection against malformed input streams. One common edge case is the 'leftover newline' character in the buffer after reading a numeric value, which can cause a subsequent %c call to fail by reading the newline instead of the intended character.
Quick reference
Syntax
scanf("format", &variable);
Inputs
Parameters
See it in practice
Examples
Reading Multiple Primitive Types
int age;
float salary;
printf("Enter age and salary: ");
if (scanf("%d %f", &age, &salary) == 2) {
printf("Age: %d, Salary: %.2f\n", age, salary);
}Enter age and salary: 25 50000.50 Age: 25, Salary: 50000.50
Reads an integer and a float. The return value is checked against 2 to ensure both variables were successfully populated.
Safe String Input with Width Limit
char username[20];
printf("Enter username (max 19 chars): ");
scanf("%19s", username);Enter username (max 19 chars): admin_user User: admin_user
Uses a width specifier (%19s) to prevent buffer overflow. Note that the address-of operator & is omitted because array names decay to pointers.
Handling Mixed Input and Buffer Clearing
int quantity;
char category;
scanf("%d", &quantity);
scanf(" %c", &category); // Note the leading space10 A
The space before %c tells scanf to skip any leading whitespace, including the newline character left in the buffer by the previous integer input.
Debug faster
Common Errors
RuntimeError
Cause: Forgetting the address-of operator (&) for scalar variables like int, float, or char.
Fix: Prefix the variable name with & to pass the memory location where the data should be stored.
int x; scanf("%d", x); // Wrong: should be &xSecurityError
Cause: Using %s without a width limit, leading to potential stack-based buffer overflows if the input exceeds the array size.
Fix: Always specify a maximum width in the format string (e.g., %31s for a 32-byte buffer).
char buf[10]; scanf("%s", buf); // Dangerous if user enters 20 charsRuntime support
Compatibility
Part of stdio.h, prefer fgets for strings
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Reads formatted input from stdin.
format: Format string with specifiers. &variable: Address where input is stored.
RuntimeError: Prefix the variable name with & to pass the memory location where the data should be stored. SecurityError: Always specify a maximum width in the format string (e.g., %31s for a 32-byte buffer).