C format '%d' expects argument of type
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’
This warning means a format in printf or scanf (%d, %f, %s ...) does not match the type of the value passed for it. Read it like this: "%d expects an int, but argument 2 has type double". The format string is argument 1, so the first value is argument 2. It is a warning, so the code compiles, but you must fix it because the values come out wrong or the program crashes.
Cause 1. You left out & in scanf
%d in scanf takes the address of an int (int *), not an int. If you pass the variable without &, the warning says expects argument of type 'int *', but argument 2 has type 'int'. When it says it expects a type with a star, you left out the &.
If you run it as it is, the program treats the value 0 stored in n as an address, tries to write there, and crashes.
Wrong code
#include <stdio.h> int main(void) { int n = 0; scanf("%d", n); printf("%d\n", n + 1); return 0;}7
main.c: In function ‘main’:
main.c:5:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
5 | scanf("%d", n);
| ~^ ~
| | |
| | int
| int *
[SIGSEGV] process killed by signal SIGSEGVFixed code
Put & in front of the variable.
#include <stdio.h> int main(void) { int n = 0; scanf("%d", &n); printf("%d\n", n + 1); return 0;}7
8
Cause 2. You read a double with %f
In printf you print a double with %f, but in scanf you must read it with %lf. That is because %f in scanf expects the address of a float. This difference trips up a lot of people.
If you read with %f, the value is written to only part of the space the double takes up, so you end up with a value completely different from the number you typed. Below, 2.5 goes in and 0.00 comes out.
Wrong code
#include <stdio.h> int main(void) { double x = 0; scanf("%f", &x); printf("%.2f\n", x); return 0;}2.5
main.c: In function ‘main’:
main.c:5:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
5 | scanf("%f", &x);
| ~^ ~~
| | |
| | double *
| float *
| %lf
0.00Fixed code
Use %lf to read a double. For printing, %f stays the same.
#include <stdio.h> int main(void) { double x = 0; scanf("%lf", &x); printf("%.2f\n", x); return 0;}2.5
2.50
Cause 3. You printed a long long with %d
%d is for int. Even if you correctly store a number too big for int in a long long, printing it with %d reads only part of the value and a wrong number comes out. Below, 3 billion is printed as a negative number.
Wrong code
#include <stdio.h> int main(void) { long long big = 3000000000; printf("%d\n", big); return 0;}main.c: In function ‘main’:
main.c:5:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long long int’ [-Wformat=]
5 | printf("%d\n", big);
| ~^ ~~~
| | |
| int long long int
| %lld
-1294967296Fixed code
Print a long long with %lld, and use %lld to read one too.
#include <stdio.h> int main(void) { long long big = 3000000000; printf("%lld\n", big); return 0;}3000000000
You get the same warning when you print the result of strlen or sizeof with %d (argument 2 has type 'size_t'). The format for this type is %zu. Or convert it to int, as in (int)strlen(s), and print it with %d.
Cause 4. You passed a single character to %s
%s expects a string, that is, the address of its first character (char *). If you pass a single character (char), the program treats that character's code ('A' is 65) as an address, tries to read from it, and crashes. The warning shows the type you passed as int, not char, because a char is converted to int when it is passed to printf.
Wrong code
#include <stdio.h> int main(void) { char grade = 'A'; printf("%s\n", grade); return 0;}main.c: In function ‘main’:
main.c:5:14: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
5 | printf("%s\n", grade);
| ~^ ~~~~~
| | |
| | int
| char *
| %d
[SIGSEGV] process killed by signal SIGSEGVFixed code
Print a single character with %c. Use %s only for a char array or a "..." string.
#include <stdio.h> int main(void) { char grade = 'A'; char name[] = "mina"; printf("%c\n", grade); printf("%s\n", name); return 0;}A mina
What to check when you see this error
- Read three things from the warning: which format it is (
%d), the type that format expects, and the type you passed (argument 2 has type ...). argument 2is the first value, right after the format string.argument 3is the second value.- Match the pairs:
%dforint,%lldforlong long,%cforchar,%sfor a string, and fordouble,%fto print and%lfto read. - For
scanf, check that there is a&in front of the variable. If the expected type has a*, it is asking for an address. - If you pass a
doubleto%dor anintto%f, a wrong value that changes from run to run may be printed. If you want to convert the value, convert it yourself, as in(int)xor(double)n, and pass that. - The message differs between compilers. clang says
format specifies type 'int' but the argument has type 'double'.
Practice problems
See also
- C Segmentation fault (core dumped)
- Syntax: C printf(): Format Specifiers and Output
- Syntax: C scanf(): Read Input
- Syntax: C scanf String Input: %s and fgets
- Learn it step by step: C Reading input
- All C error messages
- Online C compiler