Introduction
Hello, my name is Guu.
This is the final installment of "The Reality of ASCII Codes".
This time, I'd like to challenge the problem in "[Part 1] - 1 and yet 49 ? Input/output format"
[Problem]
Assign an integer to the variable num defined in char type, and print it out with printf ( " %d " ).
However, the output value and the input value should be the same.
Hint: Subtract '0' from num.
"[Part 1] - 1 and yet 49 ? Input/output format" and
"[Part 2] - Correspondence between ASCII codes and characters",
If you input "1", simply "printf ( " %d " )" will print "49".
This was caused by the fact that the ASCII code (49) corresponding to
"1" is printed when "1" entered with getchar() is printed with "printf ( " %d " )".
So how do you input "1" and output "1"?
The key is the ASCII code and the clue to the problem.
Explanation
Subtracting '0' from 'num' (now 1)
Normally, subtracting 0 from a number does not change its value.
However, in this problem, we are dealing with a number that was entered "as a character".
That means ... they are converted to ASCII code.
Let's check the ASCII code table!
As you can see from the ASCII code table,
ASCII codes are applied to letters, numbers, and symbols in order.
For example, when a letter increases from "0 to 1" or "1 to 2," the corresponding ASCII code also increases by 1.
Therefore, subtracting the ASCII code for 0 (48) from the ASCII code for 1 (49) yields "1".
In this way, "printf (" %d ")" outputs the same value as the input value.
If we modify our code to take this into account...
#include <stdio.h>
int main(void)
{
char num; // define char type variable ch
printf("Please input integer"); //input integer from command line
num = getchar(); // assign input integer to num
num = num - ' 0 '; // subtract num from character ( a )
printf("num : %d", num); // print num in decimal
return 0;
}
Now let's run it!
----------------------------------------------------- Output result
-----------------------------------------------------
Yay!
Finally, "1" is output!
Here is one more thing to notice!
If you look closely at the code, you will see that the "0" is followed by a " ' '".
This is not a decoration!
When subtracting 0 from num, the 0 must be an ASCII code.
The ' ' is necessary to make the "0" be recognized as an ASCII code!
Conclusion
Since ASCII codes are sequential, subtracting ' 0 ' from num allows the input value to match the output value.
Guu's Musings
I learned a lot from one problem!
Let's review it well so we don't forget!
Check out the "The reality of ASCII Codes" series in one place!
The reality of ASCII codes [Part 1] - 1 and yet 49 ? Input/output format
I tried to output 1, but it turned out to be 49! The cause of this problem has something to do with ASCII codes....
The reality of ASCII codes [Part 2] - Correspondence between ASCII codes and characters
We will look into the mystery of why 1 became 49..!
The reality of ASCII codes [Part 1] ~ Subtracting ' 0 ' from num? ~
Finally, the meaning of "num minus '0'" is revealed!
New Engineer's Blush Blog Articles