此代码中的第二个scanf将无法读取。 (C-代码)

时间:2014-11-10 22:24:00

标签: c double scanf

我刚刚回到C,经过我所做的所有搜索后,我没有为此代码找到一个有效的解决方案:

double a = 0, c = 0;
printf("Please enter 2 numbers! Number 1: \n");
scanf(" %d", &a); 

printf("Please enter number 2 now!\n");     
scanf(" %d", &c);  /*This scanf is problematic*/
printf("Thank you! The numbers you have entered are %i and %i", a, c);

第二个scanf(有或没有空格)不读取任何数字,而是给出它初始化的0。第一个scanf工作正常。我希望我只是忽略了一些事情,但我已经尝试了几个错误。

1 个答案:

答案 0 :(得分:2)

您只需将%i%d更改为%lf,就像这样:

double a=0, c=0;

printf("Please enter 2 numbers! Number 1: \n");

scanf(" %lf", &a);

printf("Please enter number 2 now!\n");

scanf(" %lf", &c);

printf("Thank you! The numbers you have entered are %lf and %lf", a, c);