0
#include <stdio.h>

int main(void) {
    int n,m=5;
    char c;
    while(m>0)
    {
        m--;
        scanf("%d",&n);
        if(n==42)
        break;
        printf("%d",n);
        fflush(stdin);
        scanf("%c",&c);
        puts(&c);       
    }
    return 0;
}

Although I know that when a enter is pressed after entering a number at first scanf, it(enter) is taken as input by the second scanf, but my doubt is that, when I give 5ttttt as input , the output is

5t
5t
5t
5t
5t

as there is no integer in input buffer, why it is not asking for input of integer| Second question is, even if we follow the above behavior, then on giving input of 5t and then pressing enter should take two characters as buffer ('t' and 'enter') but only t is taken in buffer and the output is

5t

but I expected the output

5t
5

as 'enter' would be taken in buffer and it will not ask for integer input in the second iteration of loop.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ayush
  • 3
  • 1
  • 2
    Welcome to Stack Overflow! [`fflush(stdin);` is undefined behaviour, don't do it.](https://stackoverflow.com/a/38325926/2173917) – Sourav Ghosh May 31 '17 at 06:41
  • Which platform are you working on? If you're on Windows, you will get different behaviour than if you're on a Unix-like system. See [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin) for the details. You should check the return value from `scanf()` each time you use it. – Jonathan Leffler May 31 '17 at 06:43
  • @JonathanLeffler i am working on Mac ,i also checked my input on ideone.com – ayush May 31 '17 at 06:45
  • If you're on a Mac, then `fflush(stdin)` is at best a no-op. After reading the `5`, the `scanf("%c", &c);` reads a `t`; the next `scanf("%d", &n);` fails (but you ignored this) without changing `n`, and the `scanf("%c", &c);` reads a `t`. This continues for 5 iterations, and then the loop terminates because of the count (`m`) dropping to 0. You'd get better enlightenment if you entered `5tuvwx`. – Jonathan Leffler May 31 '17 at 06:46
  • @JonathanLeffler that's exactly what i am asking , why the next `scanf("%d",&n);` fails and i tried the input 5tuvwx . – ayush May 31 '17 at 06:54
  • The `scanf("%d", &n)` fails because the next character is `t` (or `u`) and neither of those is a valid integer. Did you see `5t / 5u / 5v / 5w / 5x` when you typed `5tuvwx`? – Jonathan Leffler May 31 '17 at 06:57
  • @JonathanLeffler , ya i saw `5t / 5u / 5v / 5w / 5x` , so if there are no valid integer , in the buffer , and we need input of integer , then we have to flush the input buffer , there is no other way.? – ayush May 31 '17 at 07:11
  • You can't `fflush()` the input buffer on Unix systems — you can on Windows (see the question I referenced earlier). You could read until the newline: `int c; while ((c = getchar()) != EOF && c != '\n') ;`. You could read the line with `fgets()` and then scan it with `sscanf()` or `strtol()` or some other suitable function. Either way works; what you're trying doesn't. If you typed `5tuv`, then you'd get a chance to enter another integer afterwards. – Jonathan Leffler May 31 '17 at 07:14

2 Answers2

2

First of all, see this thread about fflush(stdin); being undefined behaviour, simply don't do it.

You can roll your own function go get this done. Something along the line of

  while ((c = getchar()) != '\n' && c != EOF) { }

should do the job nicely.

That said,

   puts(&c);  

also causes undefined behavior, as &c does not point to a string. Variable c is of type char, it's not a char array with a mandatory null-termination required to be considered as string. so, due to memory overrun, you're essentially accessing out of bound memory which causes the UB.

Probably you want to use putchar() instead.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-1

Avoid fflush(stdin). Instead of fflush(stdin) you can use getchar(). After giving an integer input by scanf("%d", &n) you'll press an Enter, right? That \n (new line character) will be taken by getchar() and you can safely take a character input by scanf("%c", &c). Otherwise the \n will be taken by c.

Najat
  • 167
  • 14