[英]Scanf doesn't appear to work in debug mode in Eclipse CDT with GDB
When running this code in debug mode: 在调试模式下运行此代码时:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
printf("Values entered: %d %d %d\n", a, b, c);
return EXIT_SUCCESS;
}
The program would not request any user input and would just output: 该程序不会请求任何用户输入,而只会输出:
Values entered: 18 78 2130026496
输入的值:18 78 2130026496
It seems the problem was caused by GDB
writing to stdin
the following line before scanf
was run: 似乎问题是由
GDB
在运行scanf
之前将stdin
写入以下行引起的:
18-list-thread-groups --available
18个列表线程组-可用
And scanf("%d%d%d", &a, &b, &c);
和
scanf("%d%d%d", &a, &b, &c);
was interpreting that line as int's instead of waiting for user input. 将该行解释为int而不是等待用户输入。
The current solution I use is to clear stdin
at the beginning of the program using: 我当前使用的解决方案是使用以下命令在程序开头清除
stdin
:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
I know that it is kind of a hack but I searched for over an hour for a solution and I couldn't find any. 我知道这很容易破解,但是我搜索了一个多小时才找到解决方案,但找不到任何解决方案。 I hope this helps someone.
我希望这可以帮助别人。
I had the same problem. 我有同样的问题。 Figured out that you have to clear output buffer if a newline character is used or if an input function is used.
弄清楚如果使用换行符或使用输入功能,则必须清除输出缓冲区。 So, do this way..
所以,这样做。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c;
fflush(stdout);//Clears the stdout buffer
scanf("%d%d%d", &a, &b, &c);
printf("Values entered: %d %d %d\n", a, b, c);
return EXIT_SUCCESS;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.