scanf函数错误消息;漂浮和双打

时间:2015-09-23 08:24:15

标签: c scanf

这个小程序应该提示用户 两个数字(猜测和数字)和计算 基于数字的近似平方根 关于Newton-Raphson迭代算法。

我遇到了scanf的问题。 这是我收到的警告信息:

AES-256-CBC

我不明白为什么我会遇到这个问题。 这是程序:

prog7-8.c: In function ‘approx_sqrt’:
prog7-8.c:19: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
prog7-8.c:19: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
prog7-8.c: In function ‘main’:
prog7-8.c:33: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
prog7-8.c:33: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’

谢谢! PS我处于初始阶段,还没有 足够的知识,以复杂的scanf使用; 所以,我只想尝试使用基本功能 这个功能。

3 个答案:

答案 0 :(得分:1)

main -

if(scanf ("%f", number) == 1)       // %f requires address of variable
               ^ & missing here  (add it)

在函数float approx_sqrt (float x) -

if (scanf ("%f", guess) == 1)
                ^ & missing   (same here add &)

答案 1 :(得分:0)

你应该将变量的引用或地址传递给scanf而不仅仅是变量

//if (scanf ("%f", guess) == 1) change
if (scanf ("%f", &guess) == 1)


//if(scanf ("%f", number) == 1) change
if(scanf ("%f", &number) == 1)

答案 2 :(得分:0)

您的问题标题清楚地表明您没有阅读编译器给您的错误消息。它没有说"期望类型浮动",它说"期望类型浮动*"。请注意*?编译器不会在错误消息中写入随机字符。正确阅读您的错误消息!

现在想一想:scanf对float参数做了什么?还是一个双重论点?这对scanf来说完全没用。 scanf需要一个指针到一个变量,它可以存储它扫描的值。