1

First of all there is another question with the same title but the solutions offered there didn't work for me. Other question

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c;
    fflush(stdout);//suggested solution in other question
    scanf("%d%d%d", &a, &b, &c);
    printf("Values entered: %d %d %d\n", a, b, c);
    return 0;
}

The code works fine when I run it normally.

Output

1 2 3
Values entered: 1 2 3

But when I run in debug mode nothing is printed. When I hover over the variables they have these values.

a : 56

b : 6422420

c : 6422420

Another solution suggested was to put this code in the start of the main method.

    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF); //suggested solution #1

Both solutions suggested in the post didn't work for me. I tried them both separately.

EDIT

OS : Windows 10

Compiler : MinGW

Enzio
  • 799
  • 13
  • 32

1 Answers1

0

I would recommend to use spaces in your scanf format, and to test its return count, so:

a = b = c = 0; // better to clear variables, helpful for debugging at least
int cnt = 0;
if ((cnt=scanf(" %d %d %d", &a, &b, &c)) == 3) {
  /// successful input
  printf("Values entered: %d %d %d\n", a, b, c);
}
else { 
  /// failed input
  printf("scanf failure cnt=%d error %s\n", cnt, strerror(errno));
}
fflush(NULL); // probably not needed, at least when stdout is a terminal

Read carefully the documentation of scanf (and of every library function) before using it.

BTW, Eclipse is just an IDE or glorified source code editor, your compiler could be GCC or Clang (and you probably can configure your IDE to pass appropriate options to your compiler). And scanf itself is implemented in your C standard library (above your operating system kernel).

But you really need to enable all warnings & debug info in your compiler (so compile with gcc -Wall -Wextra -g if using GCC) and to learn how to use your debugger gdb (breakpoints, step by step, variable querying, backtrace...)

You may want to use fflush and you should compile and run your program in a terminal (not under Eclipse, which is hiding a lot of useful things to you).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I tried your code. It skips to the `perror("scanf failure");` part without entering the if condition. Note : works find normally. This only happens in debug mode. – Enzio May 25 '17 at 11:53
  • But did you read carefully the documentation of `scanf` ? BTW, I slightly improved my code. – Basile Starynkevitch May 25 '17 at 12:02