1

A few days back I installed Eclipse ide and also the CDT plugin therein for C and C++ development. Also made the necessary settings. Also tried a simple C program of adding two integers. Surprisingly, no output was shown in the terminal, but when the program was terminated it showed a wrong output of 0.

The program was this:

#include <stdio.h>

int main(){
  int a, b;
  printf("Enter an integer: "); scanf("%d", &a);
  printf("Enter another integer: "); scanf("%d", &b);
  printf("Sum = %d", (a+b));
  return 0;
}

This same program showed the correct output when it was ran using the command prompt:

 gcc -g add.c -o add.exe

Have I made any mistake in the code? Can anyone suggest me what do I need to do to run it in Wclipse?

howlger
  • 31,050
  • 11
  • 59
  • 99
Sandip Nath
  • 622
  • 2
  • 8
  • 18
  • You can try to compile directly with gcc for instance to see if is some syntactically error. If you try that you see that your short program is correct. – Mihai8 Dec 23 '17 at 10:25
  • You're probably running into the same problem as in [this question](https://stackoverflow.com/questions/34604947/using-fgetschar-c-int-i-file-f-with-printf-for-c-in-eclipse-cdt-the-o/43160445). Please see my answer there. – HighCommander4 Dec 23 '17 at 18:23
  • when calling any of the `scanf()` family of functions: Always check the returned value (not the parameter values) to assure the operation was successful – user3629249 Dec 24 '17 at 04:04
  • regarding: `printf("Sum = %d", (a+b));` stream I/O is buffered. So, currently, the data is not pushed to the terminal until the program exits. To correct that problem, use the format string: `"Sum = %d\n"` Note the trailing newline sequence, which causes the stdout stream to be flushed to the terminal., – user3629249 Dec 24 '17 at 04:07
  • for ease of readability and understanding: follow the axiom: *only one statement per line and (at most) one variable declaration per statement. – user3629249 Dec 24 '17 at 04:14
  • Tried every piece of your advises, nothing worked. – Sandip Nath Dec 25 '17 at 06:00

1 Answers1

0

have you build the project in the first place? Go to "Project" tab in main tool bar and click on build project. If successful setup up a new run configuration for C under "Run" tab. And in the C/C++ application field -> "Search Project" and choose your project's binary from the list.

Now try running your custom run/debug configuration.

HariUserX
  • 1,341
  • 1
  • 9
  • 17