format expects argument of type C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see format '%d' expects argument of typemore '%' conversions than data argumentsformat specifies type 'int' but the argument has typeformat '%s' expects argument of type 'char*'
Seen on: C/C++ GCC Clang

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

  • %d used with long, size_t or long long
  • %s given a std::string instead of c_str()
  • Missing argument for a specifier, or an extra one
  • %f used with float in scanf (needs %f for float but %lf for double)
  • %p given a non-void pointer without a cast

⚡ Quick fix

  1. Use %zu for size_t, %ld for long, %lld for long long
  2. Pass str.c_str() for %s
  3. Compile with -Wall -Wformat=2

Detailed fix by platform

GCC / Clang

  1. Add -Wall -Wextra -Wformat=2 and treat as error: -Werror=format
  2. In C++20 use std::format("{}", x), which checks types at compile time

Code examples

Matching specifiers

c
#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

  1. Specifier — Which % matches which argument?
  2. Types — Exact type of each argument?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

C4996 — MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead (C/C++)C4996call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…comparison of integers of different signs — C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare] (C/C++)comparison of integer…control reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…implicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …stack smashing detected — C/C++: *** stack smashing detected ***: terminated — buffer overflow on the stack (C/C++)stack smashing detect…format ex…

Explore the map of this error Full error map →

🧠 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.