scanf 函数的问题 (C)
Troubles with scanf function (C)
我有以下代码:
struct punto richiedi_punto () {
static int count=1;
struct punto point;
do {
printf("Inserire coordinate del punto %i:", count);
scanf("%d;%d",&point.x,&point.y);
} while (point.x<0 || point.x>9 || point.y<0 || point.y>9);
count++;
return point;
}
Gcc 未发现错误,但我收到此警告:
Warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
我试图在 google 上找到解决方案,但我不明白导致此警告的原因。
提前致谢。
编辑:我刚才注意到,如果我 运行 我在 MonoDevelop 控制台中的程序,我无法插入我的坐标(为什么?),但是如果我 运行 它在 gnome-terminal 中它工作通常。
scanf()
returns转换成功的字段数,供您查看
int fields;
do {
printf("Inserire coordinate del punto %i:", count);
fields = scanf("%d;%d",&point.x,&point.y);
} while (fields != 2 || point.x<0 || point.x>9 || point.y<0 || point.y>9);
正如@chux 指出的那样,以上内容并不好。这是一个使用 sscanf
而不是 scanf
的版本。
#include <stdio.h>
int main()
{
int fields, x, y;
char inp[100];
do {
printf("Inserire coordinate:");
fgets(inp, 100, stdin);
fields = sscanf(inp, "%d;%d",&x,&y);
} while (fields != 2 || x<0 || x>9 || y<0 || y>9);
printf("x=%d, y=%d\n", x, y);
return 0;
}
Scanf
return no.of 输入成功从用户获取的值。警告是您忽略了 return 值。
所以你可以这样使用,
int ret;
ret=scanf("%d;%d",&point.x,&point.y);
否则,
(void*)scanf("%d;%d",&point.x,&point.y);
表示不检查scanf的return值。
如果仅设置了 point.x 或 point.y,则 scanf 可以 return 1,如果未设置 point.x 或 point.y,则可以 return 0。
你可以检查 return scanf 的值来删除这个警告
int ret = scanf("%d;%d",&point.x,&point.y);
if (ret != 2)
{
printf("Error whith scanf");
return 0;
}
看看 scanf manual :函数的 return 值是您知道函数是否成功的唯一途径。
在这里,您的编译器不喜欢您的代码,因为您甚至不查看 return 值,因此即使函数失败,您的代码也会继续。
这里很容易失败,例如,如果输入不是数字,或者不包含“;”如预期,或其他。
所以只需用类似的东西替换 scanf 行:
if (scanf("%d;%d",&point.x,&point.y) != 2)) {}
应该让 Gcc 放心,向他表明您关心 return 值。
但最干净的解决方案是储存 return 值并根据以下内容做一些事情,请查看手册的 "return value" 部分以获取更多信息。
我有以下代码:
struct punto richiedi_punto () {
static int count=1;
struct punto point;
do {
printf("Inserire coordinate del punto %i:", count);
scanf("%d;%d",&point.x,&point.y);
} while (point.x<0 || point.x>9 || point.y<0 || point.y>9);
count++;
return point;
}
Gcc 未发现错误,但我收到此警告:
Warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
我试图在 google 上找到解决方案,但我不明白导致此警告的原因。
提前致谢。
编辑:我刚才注意到,如果我 运行 我在 MonoDevelop 控制台中的程序,我无法插入我的坐标(为什么?),但是如果我 运行 它在 gnome-terminal 中它工作通常。
scanf()
returns转换成功的字段数,供您查看
int fields;
do {
printf("Inserire coordinate del punto %i:", count);
fields = scanf("%d;%d",&point.x,&point.y);
} while (fields != 2 || point.x<0 || point.x>9 || point.y<0 || point.y>9);
正如@chux 指出的那样,以上内容并不好。这是一个使用 sscanf
而不是 scanf
的版本。
#include <stdio.h>
int main()
{
int fields, x, y;
char inp[100];
do {
printf("Inserire coordinate:");
fgets(inp, 100, stdin);
fields = sscanf(inp, "%d;%d",&x,&y);
} while (fields != 2 || x<0 || x>9 || y<0 || y>9);
printf("x=%d, y=%d\n", x, y);
return 0;
}
Scanf
return no.of 输入成功从用户获取的值。警告是您忽略了 return 值。
所以你可以这样使用,
int ret;
ret=scanf("%d;%d",&point.x,&point.y);
否则,
(void*)scanf("%d;%d",&point.x,&point.y);
表示不检查scanf的return值。
如果仅设置了 point.x 或 point.y,则 scanf 可以 return 1,如果未设置 point.x 或 point.y,则可以 return 0。
你可以检查 return scanf 的值来删除这个警告
int ret = scanf("%d;%d",&point.x,&point.y);
if (ret != 2)
{
printf("Error whith scanf");
return 0;
}
看看 scanf manual :函数的 return 值是您知道函数是否成功的唯一途径。
在这里,您的编译器不喜欢您的代码,因为您甚至不查看 return 值,因此即使函数失败,您的代码也会继续。
这里很容易失败,例如,如果输入不是数字,或者不包含“;”如预期,或其他。
所以只需用类似的东西替换 scanf 行:
if (scanf("%d;%d",&point.x,&point.y) != 2)) {}
应该让 Gcc 放心,向他表明您关心 return 值。 但最干净的解决方案是储存 return 值并根据以下内容做一些事情,请查看手册的 "return value" 部分以获取更多信息。