No, you should not get "12 97". That's not the way %d
works. %d
expects to see digits, and digits only.
If you modify your program like this:
int r1, r2;
printf("enter number\n");
r1 = scanf("%d",&a);
printf("enter number\n");
r2 = scanf("%d",&b);
printf("scanfs returned %d %d\n", r1, r2);
and type "12 a" again, you will see
scanfs returned 1 0
indicating that the second scanf
call did not successfully read or convert anything. (When you use scanf
, it's always a good idea to check the return value to make sure all your conversions worked.)
If you want to read a character as a character, you can use %c
:
char b;
r2 = scanf(" %c",&b);
Beware, though, that %c
is a little different from most of the other scanf
format specifiers. You may need an explicit leading space, as I have shown here. See this other question for more information. (Thanks to @user202729 for the link.)