-3

I have no problem understanding the following, because in both cases they're really stored as int types.

int a; // combination 1
scanf("%d", &a); // suppose input is 65
printf("%c", a); // prints 'A'
printf("%d", a); // prints 65

char a; // combination 2
scanf("%c", &a); // suppose input is 'A'
printf("%c", a); // prints 'A'
printf("%d", a); // prints 65

I also understand this, because char can only store the first digit, in the form of a character, so anything after the first '6' during the input will be ignored.

char a; // combination 3
scanf("%c", &a); // suppose input is 65
printf("%c", a); // prints '6'
printf("%d", a); // prints 54 which is the ASCII value of '6'

But I have an issue with understanding what's happening here:

int a; // combination 4
scanf("%d", &a); // suppose input is 'A' (in fact any letter, upper or lower case)
printf("%c", a); // prints this symbol ╗
printf("%d", a); // prints 2

As mentioned its showing the same printf results regardless of which letters or upper/lower cases is being inputted. So I'm pretty confused!

I should also mention the reason I care about combination 4 is because I'm trying to define an int variable for switch which can compare cases of both letters and numbers, thus hoping to convert everything into int form. Using a char variable kind of works, but it's not ideal as it treats any 2-digit number the same as the first digit in that number.

EDIT: So I see that basically scanf does nothing in combination 4. What I'd like to know is a detailed explanation of why scanf can't be used this way when the other three combinations work.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Joe
  • 27
  • 6
  • scanf failed, it didn't read anything, check it's return value. `a` variable is uninitialized, scanf didn't touch it. – KamilCuk Sep 24 '18 at 09:55
  • Check what [`scanf`](http://en.cppreference.com/w/c/io/fscanf) ***returns***! In the last case it should return `0` indicating that it didn't actually read and convert anything. – Some programmer dude Sep 24 '18 at 09:55
  • 2
    Possible duplicate of [Scanf a char using %d](https://stackoverflow.com/questions/44660548/scanf-a-char-using-d) – msc Sep 24 '18 at 10:10
  • Sorry I'm new. How to check scanf returns? What exactly is the code? Below is my full code... #include #include int main() { int a; scanf("%d", &a); printf(" %c \n", a); printf(" %d \n", a); return 0; } – Joe Sep 24 '18 at 10:22
  • `int i = scanf("%d", &a);` - Here check the value of `i` after the scan. In this case, it should return 1 for success and 0 for a fail. – Superman Sep 24 '18 at 10:36
  • If you had checked for errors on functions which return one as recommended, you wouldn't have had to ask. – too honest for this site Sep 24 '18 at 11:36

1 Answers1

0

In this case, scanf skips reading Characters. run this simple test.

int a; // combination 4
scanf("%d", &a); // suppose input is 'A' (in fact any letter, upper or lower case)
if(scanf("%d", &a) == 1) //to check character read or not
    printf("input: %d\n", a);
else 
    printf("Character skipped");
printf("%c", a); // prints this symbol ╗
printf("%d", a); // prints 2

output: Character skipped0

  • So I see that basically scanf does nothing in combination 4. What I'd like to know is a detailed explanation of why scanf can't be used this way when the other three combinations work. – Joe Sep 24 '18 at 11:08
  • by giving input as %d, you're defining it as a Single Decimal integer input. The range is -32768 to +32767. Large integers. A large integer is a binary integer with a precision of 31 bits. The range is -2147483648 to +2147483647. So you can not give any input of a character while defining them as %d. Scanf checks against %d or %f or %c and so on... so you defining the variable as the character but calling the input method as an integer, which is a logical misunderstanding may be. – Shahriar Biggo Sep 24 '18 at 11:29
  • My confusion is why doesn't it convert 'A' to 65 and store it in `a` ? Because when you do `scanf("%c", &a)` and gives it a number it stores it fine. So the reverse of `scan("%d", &a)` and giving it a character should work? If doing so doesn't work, I'd like to know which function can allow that to work (ie. convert a character into its ASCII number). – Joe Sep 26 '18 at 08:19