C scanf()
In this page:
scanf("%specifier", &variable_name);
What is scanf()?
scanf ("scan formatted") reads input the user types at the keyboard and stores it into your program's variables, making it the standard way a C program becomes interactive instead of just running with hardcoded data.
Example: What is scanf()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
sscanf("25", "%d", &age);
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading an Integer
Reading an int requires the %d specifier and an ampersand before the variable name (scanf("%d", &age);), because scanf needs the variable's memory address to write the value into -- passing the variable itself instead of its address is a classic beginner bug.
Example: Reading an Integer
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
sscanf("25", "%d", &age);
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading a Character
Reading a single character with %c is sensitive to leftover whitespace or newline characters sitting in the input buffer from a previous scanf call, which can cause the next read to silently grab a space instead of the character you expect.
Example: Reading a Character
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char grade;
sscanf("A", "%c", &grade);
// Print formatted text to the screen
printf("%c", grade);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading a Floating-Point Value
%f reads a decimal number into a float variable, letting your program accept things like measurements or prices directly from user input rather than only working with values baked into the source code.
Example: Reading a Floating-Point Value
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
float price;
sscanf("19.99", "%f", &price);
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading Multiple Values Together
A single scanf call can read several values at once, like scanf("%d %f", &count, &price);, as long as the format string lists each specifier in the same order as the variables and matching addresses that follow.
Example: Reading Multiple Values Together
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int count;
float price;
sscanf("3 9.99", "%d %f", &count, &price);
// Print formatted text to the screen
printf("%d %.2f", count, price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
&inscanf("%d", x), which passes the value instead of the address and crashes. - Using
%ffor adoubleinscanf, when%lfis required. - Not checking the return value of
scanf, so bad input goes unnoticed.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: