No Console Output in Eclipse with CDT in Windows

In Windows the Console doesn’t display any output from printf. Let’s see an example.

int main(void) 
{
    int i;

    printf("Enter a number: ");
    scanf("%d", &i);
    printf("You entered %d\n", i);
    return EXIT_SUCCESS;
}

The text “Enter a number: ” is not shown in the Console windows of Eclipse with CDT. This is due to the fact all the stdout and stderr ouputs in Windows are buffered. So we can simply flush stdout and stderr everytime you use printf. But it is not practical solution. You may add these two lines of code in the first part of your main function.

  • setvbuf(stdout, NULL, _IONBF, 0);
  • setvbuf(stderr, NULL, _IONBF, 0);

These lines enforce stdout/stderr not to have buffers which means that all the output will be happening as soon as it takes place. One serious problem of this solution is that the program will be crashed if you start the debugger and try to step over any of printfs. So in the Debug mode, starting a new command prompt console is a good idea. Let’s me summary this workaround.

Use setvbuf for Run mode

Put this function in your main function in the first place.

void set_std_buffer_off()
{
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
}
int main(void)
{
    // turn off the std buffering
    set_std_buffer_off();

    // start your own code.
    ....
}

You now see outputs from printfs in your code when you run the program.

Debug mode

For the debug mode, use GDB command file.

Create a text file and named it as “gdbinit.txt” The file name is not important anyway. You may use any proper name. Open the text file and put this single line into the file and save it.

<span style="line-height:1.5em">set new-console on</span>

This line simply means that new console will be started when the debugger starts a debugging session.

Open “Preferences” (Window > Preferences). Set the text file for “GDB command file:” in C/C++ –> Debug –> GDB. Click the “Apply” button and the “OK” button.

Try to debug your program. Then you will see a command prompt when the debugger starts. All outputs and inputs will be happening in the new command prompt without having any troubles.

Leave a Reply

Your email address will not be published. Required fields are marked *