1

I have a specific example below, which works perfectly fine if integers are inputted (see output1), when I try to scan a char using %d specifier in scanf function call I get the output2 below.

So, my question is if input a char I hope the type specifier should convert it to an equivalent int value, if not a junk value, even in the either case it should print/segfault. But, here I'm getting continuous prints which I feel is wrong as scanf is getting bypassed every single time. I'm pretty unsure what's happening in the background and would like to know the same.

#include <stdio.h>

int main()
{

   int a;

   while (1){
       printf("enter a number:");
       scanf("%d", &a);
       printf("entered number is %d\n", a);
   }

return 0;
}

Output1:

>     enter a number:1
>     entered number is 1
>     enter a number:
>     3
>     entered number is 3
>     enter a number:4
>     entered number is 4
>     enter a number:5
>     entered number is 5 enter a number:

Output2: for input a

>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767

PS: I know this is a stupid question of asking what happens in an invalid case where a type specifier unintended (%d in this case) is used for different type, but I would like to know what happens in the background, if any. Thanks

geek
  • 77
  • 3
  • 12
  • 2
    Short story: If the input doesn't match the format then [`scanf`](http://en.cppreference.com/w/c/io/fscanf) returns early (check what the call returns! always!) leaving the input still in the buffer. – Some programmer dude Jun 20 '17 at 18:32
  • In the background (actually in right in front of you, but you ignore it) scanf tells you that it did not successfully read an integer. The return value carries that information. – Yunnosch Jun 20 '17 at 18:33
  • 3
    `"%d"` expects numeric input like `(white-spaces)(sign)[digits]`. If the user does not type that, nothing is read and text remains in `stdin` for the next input function - With your code, bad input leads to an endless loop as the offending text is never consumed. – chux - Reinstate Monica Jun 20 '17 at 18:35
  • %d will not read characters. You will need to use %c for that. – anon Jun 20 '17 at 18:35
  • "if input a char" ---> Tip: By "char", I think you mean letters. In C, a `char` is a type of 1 byte. `scanf()` does not read char, but _characters_. Some _characters_ are letters, digits, punctuation, controls, etc. – chux - Reinstate Monica Jun 20 '17 at 18:42
  • So you intentionally invoke undefined behaviour and wonder why you see undefined behaviour … Good question, you are only the 100000th asker. Didn't the other 99999 provide the exact answer you want to see? – too honest for this site Jun 20 '17 at 18:44
  • *I would like to know what happens in the background* - As for any undefined behavior - the implementation does not bother to handle these cases, so you get some residuals and junk from the other processing. – Eugene Sh. Jun 20 '17 at 19:04
  • "I hope the type specifier should convert it to an equivalent int value." But as The Man in Black said, "Prepare to be disappointed." When you have `int x = 'c';`, then yes, C will automatically convert the character to an integer for you. But that's in an an ordinary assignment. When you 're using `scanf` and a `%d` format, on the other hand, such a conversion is simply *not* performed. – Steve Summit Jun 20 '17 at 19:57

1 Answers1

4

You may check scanf as @Some programmer dude. You may compare the count arguments succesfully filled (Thanks to @chux)

In your case, scanf didn't find any integer value, reached the end of the input and returned EOF, keeping the a variable untouched.

On failure it'll return EOF (read http://www.cplusplus.com/reference/cstdio/scanf/#return).

if(scanf("%d", &a) == 1) //Check if exactly one parameter was read. 
    printf("entered number is %d\n", a);

For characters, you better use getch() or at least, ask for "%c" on scanf:

if(scanf("%c", &a) == 1)
    printf("entered key was %d\n", a);

The "junk" value you recieve is what was in your program memory, because a is not initialized.

leosf6308
  • 109
  • 6