Éanan Halferty Éanan Halferty - 3 months ago 13
C Question

Getting error: format specifies type 'int' but the argument has type 'double'

I don't know why this won't compile. Any idea?

It is pretty straight forward and I've spent forever looking at it and can't figure out what is wrong with it.

#include <stdio.h>
#include <stdlib.h>

void euroToDollar(double euro);

int main() {
double europrice1 = 1.00;

euroToDollar(europrice1);
return 0;
}

void euroToDollar(double euro){

double dollar = euro * 1.11;

printf("EUR%.2d = USD%.2d" ,euro, dollar);
return;
}


The error message:

Basic.c:24:39: error: format specifies type 'int' but the argument has type
'double' [-Werror,-Wformat]
printf("EUR%.2d = USD%.2d" ,euro, dollar);
~~~~ ^~~~~~
%.2f
2 errors generated.

Answer

In your code, you need to change

printf("EUR%.2d = USD%.2d" euro, dollar);

to

printf("EUR%.2f = USD%.2f", euro, dollar);

Notice two changes

  1. Added the ,, as required by the printf() syntax.
  2. double should be printed with %f format specifier. Using wrong type of argument invokes undefined behaviour.
Comments