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.