On this page
scanf
Prev: gets | Up: stdio.h | Next: stdlib.h
Rel: fgets(), stdio.h, stdio.h & File I/O Guide
Ref: cppreference — scanf
Tags: #public #prog #c #stdlib #stdio #tier/legacy
Include
#include <stdio.h>
Signature & Overview
int scanf(const char *restrict fmt, ...);
Reads formatted input from stdin; prone to unbounded %s reads, integer overflow UB, and leftover newline issues.
Legacy / Avoid: Prefer fgets() in all new code.
Notes & Pitfalls
- Always specify max width on
%s("%31s") and check the return value: Better yet, read lines withfgets()and parse withsscanf()orstrtol().
Example
#include <assert.h>
#include <stdio.h>
int main(void) {
int port = 0;
assert(sscanf("port=8080", "port=%d", &port) == 1 && port == 8080);
}
Linked from
Related notes
- tuts/c/stdlib/stdio/gets Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib
- tuts/c/stdlib/stdio/fgets Linked here; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio_and_file_io Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio/fclose shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/feof shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/ferror shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fflush shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fopen shared tags: #c, #prog, #stdio, #stdlib, #tier