我正在编写一个 C 程序,其工作是将摄氏温度转换为华氏温度,反之亦然。我希望我的用户在表单中提供输入
double, char
其中字符可以是“F”(表示华氏温度到摄氏温度的转换),也可以是“C”(表示摄氏温度到华氏温度的转换)。 我写了这个程序来做到这一点。 计划
#include <stdio.h>
int main( )
{
char in[10];
int i;
printf("Welcome to the Tempurate Conversion Enter a ");
scanf("%s %d", i, &in);
printf(i);
printf(in);
return 0;
}
当我编译这个程序时,我收到以下警告: 警告
CtoF.c: In function 'main':
CtoF.c:8:4: warning: passing argument 1 of 'printf' makes pointer from integer w
ithout a cast [enabled by default]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include/stdio.h:294:37: note:
expected 'const char *' but argument is of type 'int'
我跑到程序并给出了以下输入: 输入
3 C
并得到以下输出: 输出
3@
我想知道警告消息的含义以及我可以采取哪些措施来解决它。
最佳答案
问题出在 scanf
和 printf
中的格式说明符。
scanf("%s %d", i, &in);
应该是
scanf("%d %s", &i, in);
和
printf(i);
printf(in);
应该是
printf("%d", i);
printf("%s", in);
<小时/>
还有一个旁注:
I wanted my user to provide input in the form
double, char
为此,您应该将 int i
更改为
double i;
以及 scanf
中的转换说明符 %d
到 %lf
:
scanf("%lf %s", &i, in);
关于c - 不正确的输出和警告消息 '' 从 int '' 创建指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038022/