0
#include <stdio.h>
int main()
{
    int a,b;
    printf("enter number\n");
    scanf("%d",&a);
    printf("enter number\n");
    scanf("%d",&b);
    printf("Numbera are %d %d",a,b);
    return 0;
}

Running the Program

Input 12 a
Outptut 12 0

Should not I get output 12 97(ASCII value of a)

Jignasha Royala
  • 1,032
  • 10
  • 27
user1204320
  • 341
  • 1
  • 3
  • 8

2 Answers2

4

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.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
0

You cannot enter a character when scanning for a number and have it automatically translated. You would need to read it into a char and convert to int.

hekzu
  • 53
  • 5