SCANF 在 Eclipse 中不接受调试模式下的输入?



首先,还有另一个标题相同的问题,但那里提供的解决方案对我不起作用。其他问题

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a, b, c;
    fflush(stdout);//suggested solution in other question
    scanf("%d%d%d", &a, &b, &c);
    printf("Values entered: %d %d %dn", a, b, c);
    return 0;
}

当我正常运行时,代码工作正常。

输出

1 2 3
Values entered: 1 2 3

但是当我在调试模式下运行时,不会打印任何内容。当我将鼠标悬停在变量上时,它们具有这些值。

A : 56

B : 6422420

C : 6422420

建议的另一种解决方案是将此代码放在 main 方法的开头。

    int ch;
    while ((ch = getchar()) != 'n' && ch != EOF); //suggested solution #1

帖子中建议的两种解决方案都对我不起作用。我分别尝试了它们。

编辑

操作系统 : 视窗 10

编译器 : MinGW

我建议在scanf格式中使用空格,并测试其返回计数,因此:

a = b = c = 0; // better to clear variables, helpful for debugging at least
int cnt = 0;
if ((cnt=scanf(" %d %d %d", &a, &b, &c)) == 3) {
  /// successful input
  printf("Values entered: %d %d %dn", a, b, c);
}
else { 
  /// failed input
  printf("scanf failure cnt=%d error %sn", cnt, strerror(errno));
}
fflush(NULL); // probably not needed, at least when stdout is a terminal

在使用之前,请仔细阅读 scanf(以及每个库函数(的文档。

顺便说一句,Eclipse只是一个IDE或美化的源代码编辑器,你的编译器可以是GCC或Clang(你可以配置你的IDE来将适当的选项传递给你的编译器(。scanf本身是在 C 标准库中实现的(在操作系统内核之上(。

但是你真的需要在编译器中启用所有警告和调试信息(所以如果使用GCC gcc -Wall -Wextra -g编译(,并学习如何使用调试器gdb(断点,分步,变量查询,回溯......

你可能想使用 fflush,你应该在终端中编译和运行你的程序(而不是在 Eclipse 下,它隐藏了很多对你有用的东西(。

最新更新