Format String Mismatch
warning
warning: format '%d' expects argument of type 'int', but argument has type 'char *'
Description
The format specifier in a printf or scanf function doesn't match the type of the provided argument.
Common Causes
- Using %d for pointers or strings
- Using %s for integers
- Providing too few or too many arguments for format specifiers
Example of Error
Error Code
int main() {
char *str = "Hello";
printf("String: %d\n", str); // %d is for integers, not strings
return 0;
}
Solution
Use the correct format specifier for each argument type.
Corrected Code
int main() {
char *str = "Hello";
printf("String: %s\n", str); // %s for strings
return 0;
}
Additional Tips
- %d is for integers, %f for floats, %c for characters, %s for strings, %p for pointers
- Use %zu for size_t (e.g., array sizes)
- Use %lld for long long int
- Consider using -Wall -Wformat compiler flags to catch format string mismatches