获取错误:格式指定类型“int",但参数的类型为“double" [英] Getting error: format specifies type 'int' but the argument has type 'double'

查看:58
本文介绍了获取错误:格式指定类型“int",但参数的类型为“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;
}

错误信息:

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.

推荐答案

在你的代码中,你需要改变

In your code, you need to change

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

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

注意两个变化

  1. 根据printf() 语法.
  2. double 应该用 %f 格式说明符打印.使用错误类型的参数会导致未定义行为.
  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.

这篇关于获取错误:格式指定类型“int",但参数的类型为“double"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆