C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
The printf/scanf format specifier does not match the argument's type, which is undefined behaviour and prints garbage or crashes.
format '%d' expects argument of typemore '%' conversions than data argumentsformat specifies type 'int' but the argument has typeformat '%s' expects argument of type 'char*' Meaning
%d is int, %ld is long, %lld is long long, %zu is size_t, %f/%lf are double, %s is char*, and %p expects void*. A wrong specifier reads the wrong number of bytes from the argument list. In C++ prefer std::cout or std::format.
Common causes
%dused withlong,size_torlong long%sgiven astd::stringinstead ofc_str()- Missing argument for a specifier, or an extra one
%fused withfloatinscanf(needs%ffor float but%lffor double)%pgiven a non-void pointer without a cast
⚡ Quick fix
- Use
%zuforsize_t,%ldfor long,%lldfor long long - Pass
str.c_str()for%s - Compile with
-Wall -Wformat=2
Detailed fix by platform
GCC / Clang
- Add
-Wall -Wextra -Wformat=2and treat as error:-Werror=format - In C++20 use
std::format("{}", x), which checks types at compile time
Code examples
Matching specifiers
#include <stdio.h>
#include <stddef.h>
int main(void) {
long big = 1234567890123L;
size_t n = 42;
printf("%ld %zu\n", big, n); /* not %d for either of them */
return 0;
}How to diagnose
- Specifier — Which % matches which argument?
- Types — Exact type of each argument?
- Count — Same number of specifiers and arguments?
🔧 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
- C4996 MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead
- 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)
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
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 15 Oct 2026