I was trying to debug a C program that requires user inputs, using Eclipse IDE and GDB debugger. Every time I entered a user input in the console, my input was displayed twice. In other words, my input was echoed with the same value. For example, if I debug the following C program:
#include <stdio.h>
int main(void) {
int length, width, height;
printf("Enter length: ");
scanf("%d", &length);
printf("Enter width: ");
scanf("%d", &width);
printf("Enter height: ");
scanf("%d", &height);
printf("Volume = %d\n", length*width*height);
return 0;
}
, then the console will look something like this:
Enter length: 2
2
Enter width: 3
3
Enter height: 4
4
Volume = 24
.
As you can see, the values 2, 3 and 4 are displayed twice respectively. Running this program (instead of debugging) gives the expected result:
Enter length: 2
Enter width: 3
Enter height: 4
Volume = 24
In addition, I notice that the execution of printf()
statements are delayed: although I have already clicked "step over", there is nothing shown in the console. To make this issue more obvious, let us change the code to:
#include <stdio.h>
int main(void) {
int length, width, height;
printf("Enter length: ");
printf("Enter length: "); // newly added line
printf("Enter length: "); // newly added line
scanf("%d", &length);
printf("Enter width: ");
scanf("%d", &width);
printf("Enter height: ");
scanf("%d", &height);
printf("Volume = %d\n", length*width*height);
return 0;
}
In this case, the string "Enter length: Enter length: Enter length: " will be displayed all at once, after I stepped over line 9: scanf("%d", &length);
. However, if I add a new line character '\n' at the end every time I call printf()
, then there will be no problem and the strings will be immediately printed to the console.
I know that these two issues are trivial, but being OCD I really want to know what is going on here. I guess it has something to do with how Eclipse handles input and output? But then again I don't see these issues when debugging java programs in Eclipse.
PS. I am running on Mac OS X El Capitan, Eclipse (Mars) IDE for C/C++ Developers, and I installed GDB with Homebrew according to the instruction given here: http://ntraft.com/installing-gdb-on-os-x-mavericks/.
Thanks in advance for answering.