{ }codelper

Error Messages › C

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;}
Input
7
When you run it
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 SIGSEGV

Edit and run it →

Fixed 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;}
Input
7
Output
8

Edit and run it →

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;}
Input
2.5
When you run it
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.00

Edit and run it →

Fixed 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;}
Input
2.5
Output
2.50

Edit and run it →

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;}
When you run it
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
-1294967296

Edit and run it →

Fixed 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;}
Output
3000000000

Edit and run it →

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;}
When you run it
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 SIGSEGV

Edit and run it →

Fixed 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;}
Output
A
mina

Edit and run it →

What to check when you see this error

  1. 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 ...).
  2. argument 2 is the first value, right after the format string. argument 3 is the second value.
  3. Match the pairs: %d for int, %lld for long long, %c for char, %s for a string, and for double, %f to print and %lf to read.
  4. For scanf, check that there is a & in front of the variable. If the expected type has a *, it is asking for an address.
  5. If you pass a double to %d or an int to %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)x or (double)n, and pass that.
  6. The message differs between compilers. clang says format specifies type 'int' but the argument has type 'double'.

Practice problems

See also