2

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.

7
  • Try a better IDE: CLion from JetBrains. I don't understand why anybody uses Eclipse. Commented Mar 20, 2016 at 13:40
  • @duffymo: Because it's free? :) I use Eclipse for java so I tried their C/C++ version. I actually use text editors like Sublime Text for C/C++.. Commented Mar 20, 2016 at 13:48
  • It's the only reason I ever used it. Glad I learned about something better. Commented Mar 20, 2016 at 13:53
  • If you're on mac, can't you use x-code? Otherwise, have a look at Qt creator. Commented Mar 20, 2016 at 14:16
  • In the Run Configurations window, is there a checkbox labeled Connect process input & output to a terminal? Is it checked? Commented Mar 21, 2016 at 4:57

1 Answer 1

1

The output is being buffered. A new line character flushes the buffer and displays the result on the screen. That's why you're not having the issule when you add \n.

Alternatively, you can try adding fflush(stdout); after your printf statements.

Also, I would also suggest you to use Xcode for Mac instead of Eclipse.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.