2

In brief my code is,

#include <stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    while(n != 0)
    {
        scanf("%d", &n);
        printf("%d\n", n);
    }
    return 0;
}

It was written for integer input. But if I input a character instead (although n is decleared as integer), the loop goes infinite and prints the last integer input value. If I input a character at first it seems like it prints a memory address. My question is, what is happening here if I input a character instead an integer?

salmanwahed
  • 9,450
  • 7
  • 32
  • 55
  • @ricky2527 why you think that? it has a breaking condition. – salmanwahed Sep 20 '14 at 06:14
  • The program seems to terminate itself when character is input, are you sure the code is right? – nj-ath Sep 20 '14 at 06:26
  • @darknight yes I am sure. Try running it in `gcc` compiler from terminal. – salmanwahed Sep 20 '14 at 06:29
  • 1
    @salmanwahed, I recommend you to check return code of `scanf()` to have more control: On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file. – Ilya Sep 20 '14 at 06:34
  • 2
    Please read this answer: http://stackoverflow.com/questions/20655381/what-happens-if-c-tries-to-scan-character-in-integer-variable – Satyapriya Krishna Sep 20 '14 at 06:44
  • 1
    I'd prefer this to reference [`scanf()` fails why?](http://stackoverflow.com/questions/4016073/scanf-fails-why/) because the answer to this question covers testing the return value from `fscanf()` explicitly, whereas the current duplicate does not really do that. – Jonathan Leffler Sep 20 '14 at 07:13

3 Answers3

4

When scanf fails, it does not remove the character from input buffer (It does remove data from buffer when it succeeds). So, the next time scanf is triggered in the loop, it will not wait for user input at all (since it has an unread character in its buffer). But it fails again, and again (since everytime it fails) and hence will go into an infinite loop.

I strongly believe the first scanf contains %c as format specifier.
You can get rid of infinite loop by

if(scanf("%d",&n)==0) break;

nj-ath
  • 3,028
  • 2
  • 25
  • 41
2

Actually when the other character was inserted it won't taken as a input, it is present in the input buffer, it can't read by the control string.

The %d control string is used to read the character of integer until other characters.

While inserting the other character due to read nothing the loop is continuously running and print the exist value of the variable.

Chandru
  • 1,306
  • 13
  • 21
1

scanf("%d", &n); means only get number data from stdin, such as 1, 2.

So, if you type char in second input, such as 'a', 'b', the input char is not stored in &n. n still has first input number data.

Therefore, infinite loop comes.

hyun
  • 2,135
  • 2
  • 18
  • 20
  • If at all the character isn't stored, then input buffer should contain zero which means while loop is not at all executed. – nj-ath Sep 20 '14 at 06:30