Search code examples
cgdbeclipse-cdtscanf

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


Solution

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