Éanan Halferty Éanan Halferty - 1 year ago 88
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;

return 0;

void euroToDollar(double euro){

double dollar = euro * 1.11;

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

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);
~~~~ ^~~~~~
2 errors generated.

Answer Source

In your code, you need to change

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


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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download