使用gcc编译时出现错误:警告:格式指定类型为'int *',但参数的类型为'double *'

托尼·GW

这是我用C语言编写的第一个程序,请当心。

我试图获取用户输入的温度之间的转换,并使用开关盒来计算转换后的温度。当在Mac上尝试使用gcc对其进行编译时,我的以下程序会抛出此类错误:

convertTemp.c:17:20: warning: format specifies type 'int *' but the argument has type 'double *' [-Wformat]
      scanf ("%d", &Celcius);
              ~~   ^~~~~~~~
              %lf
convertTemp.c:21:72: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
      printf ("The converted temperature of %d in Fahreight is: %d\n", Celcius, Fahr);
                                            ~~                         ^~~~~~~
                                            %f
convertTemp.c:21:81: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
      printf ("The converted temperature of %d in Fahreight is: %d\n", Celcius, Fahr);
                                                                ~~              ^~~~
                                                                %f
convertTemp.c:25:20: warning: format specifies type 'int *' but the argument has type 'double *' [-Wformat]
      scanf ("%d", &Fahr);
              ~~   ^~~~~
              %lf
convertTemp.c:29:70: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
      printf ("The converted temperature of %d in Celcius is: %d\n", Fahr, Celcius);     
                                            ~~                       ^~~~
                                            %f
convertTemp.c:29:76: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
      printf ("The converted temperature of %d in Celcius is: %d\n", Fahr, Celcius);     
                                                              ~~           ^~~~~~~
                                                              %f
6 warnings generated.

代码:

#include <stdio.h>

int main (void)
{
  int choice;
  double Celcius, Fahr;

  printf ("Do you want to convert from C to F (1) or from F to C(2))?");

  scanf ("%i", &choice);


  switch(choice)
  {
    case 1:
      printf ("Please type the temp in Celcius");
      scanf ("%d", &Celcius);

      Fahr = (Celcius * 9) / 5;
      Fahr += 32;
      printf ("The converted temperature of %d in Fahreight is: %d\n", Celcius, Fahr);

    case 2:
      printf ("Please type the temp in Fahrenheit");
      scanf ("%d", &Fahr);

      Celcius = (Fahr - 32) * 5;
      Celcius /= 9;
      printf ("The converted temperature of %d in Celcius is: %d\n", Fahr, Celcius);

  }

  return 0;

} 
吊床

您的程序调用未定义的行为使用错误的转换说明符将调用UB。要扫描double使用说明%lf符(您的编译器警告已建议这样做)。

scanf ("%lf", &Celcius);   

样品运行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

警告格式指定类型为int,但参数的类型为long

未为参数类型Double,int定义运算符^

无法使用类型为“([Int])”的参数列表调用类型“Double”的初始化程序

C - 格式指定类型 'int' 但参数类型为 'char *' [-Wformat]

类型为“ int”的参数与类型为“ int *”的参数不兼容

警告:格式为'c'的参数类型为'int',但是参数2的类型为'char'

错误:格式为'%s'的参数类型为'char *',但参数2的类型为'int'[-Wformat =]

格式'%d'期望类型为'int'的参数

警告:格式'%x'期望类型为'unsigned int'的参数

格式“%lf”需要类型为“double”的参数,但参数 2 的类型为“double *”

警告:格式'%d'期望的参数类型为'int',但是参数2的类型类型为'long int'[-Wformat =]

预期为“ double **”,但参数的类型为“ double(*)[2]”

格式 '%d' 需要类型为 'int' 的参数,但参数 3 的类型为 'int *'

使用scala.math.pow折叠List [Int]时,类型不匹配错误:找到Double,必需为Int

“类型为“ int(*)()”的参数与类型为int的参数不兼容”错误?

错误:类型为'bar <int>&'的参数的默认参数的类型为'bar <int>'

什么是警告警告,以及如何解决此警告:格式'%p'期望类型为'void *'的参数,但是在输出时参数2的类型为'int *'[-Wformat =]

强制类型为Int的绑定<Double>-Swift UI

不能使用类型为“Double”的参数为“[Double]”类型的值添加下标

当参数为int时为什么会出现编译错误“无法在...的参数中使用...作为uint8类型”

C学生作业,'%f'期望参数类型为'float *',但是参数2的类型类型为'double *'

*预期为'int *',但参数的类型为'int **'* *在c语言中为警告

错误无法初始化右值类型为'int'的类型为'int *'的参数

Haskell - Num 与 Double、Int 类型

函数具有类型为“int”的参数,但传递了类型为“int*”的参数

二进制运算符的类型为'int'和'double(double *,double *,int)'的无效操作数

错误消息“格式'%ld'期望类型为'long int'的参数,但是参数2具有类型'int'”

预期为“ const char * __restrict__”,但参数的类型为“ int”

注意:预期为“ float *”,但参数的类型为“ int *”