warning: format '%s' expects argument of type 'char *', but argument has type 'char (*)[10]' / scanf overflows buffer
scanf was called with a wrong format/argument combination or without a width limit.
scanf buffer overflowscanf missing & segmentation faultformat '%s' expects argument of type 'char *'ignoring return value of scanf Meaning
%s stops only at whitespace and writes past the buffer. %d needs int*, %f needs float* and %lf needs double*. Forgetting & for numbers gives a crash.
Common causes
- Missing
&before a numeric variable %fused for double (needs%lf)%swithout width- Return value of scanf ignored
⚡ Quick fix
- Use
scanf("%9s", buf)with a width one less than the buffer - Check the return value (
== 1) - Prefer
fgets+strtol
Detailed fix by platform
C
if (scanf("%d", &n) != 1) { /* handle bad input */ }
Code examples
Safe scanf
int n; char name[10];
scanf("%d", n); // crash: missing &
scanf("%s", name); // overflow if input is longer than 9 chars
scanf("%d", &n);
scanf("%9s", name);How to diagnose
- Types — Format matches argument?
- Width — Specified?
- Return — Checked?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
- format expects argument of type C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
Most viewed in C/C++
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 2 Dec 2026