簡體   English   中英

在具有GDB的Eclipse CDT中,Scanf似乎無法在調試模式下工作

[英]Scanf doesn't appear to work in debug mode in Eclipse CDT with GDB

在調試模式下運行此代碼時:

#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;
}

該程序不會請求任何用戶輸入,而只會輸出:

輸入的值:18 78 2130026496

似乎問題是由GDB在運行scanf之前將stdin寫入以下行引起的:

18個列表線程組-可用

scanf("%d%d%d", &a, &b, &c); 將該行解釋為int而不是等待用戶輸入。

我當前使用的解決方案是使用以下命令在程序開頭清除stdin

int ch;
while ((ch = getchar()) != '\n' && ch != EOF);

我知道這很容易破解,但是我搜索了一個多小時才找到解決方案,但找不到任何解決方案。 我希望這可以幫助別人。

我有同樣的問題。 弄清楚如果使用換行符或使用輸入功能,則必須清除輸出緩沖區。 所以,這樣做。

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM