Introduction
Hello. My name is Guu.
In the last issue, we learned that "printf ( " %d " )"; does not print out the 1 entered by "getchar( )".
This time, I would like to solve the mystery of why 49 is printed when 1 is inputted.
The point is the ASCII code.
Example
First of all, let's consider the case where "a" is entered as an example.
The following program assigns "a" to the char variable "ch".
Try two output methods: "printf ( " %c " )"; and "printf ( " %d " )";.
#include <stdio.h>
int main (void)
{
char ch; // define char type variable ch
printf(" Please input character "); // input character ( a ) from command line
ch = getchar(); // assign input character ( a ) to ch
printf("ch/c : %c", ch); //output ch as a character
printf(" ³ ");
printf("ch/d : %d", ch); //output ch as a decimal number
return 0;
}
What value will be output when this program is executed?
Output results
The first output "a" is the one output as a character.
The second output "97" shows the ASCII code for a.
Let us consider ASCII codes in some depth.
Why are ASCII codes necessary in the first place?
The inside of a computer is a world of bits. We can only express with 0s and 1s.
But we use many other characters and symbols besides 0s and 1s.
How can we express letters and symbols in the world of bits?
"...why don't we just apply a code consisting of only 0s and 1s to the letters and symbols?
Yes, this is what "ASCII code" is!
But wait a minute!
The ASCII code for "a" output in the previous program is "97".
It is neither 0 nor 1. What's wrong? That's not right... (-_-;)
Don't worry! This is because "printf ( " %d " )"; prints decimal numbers.
In fact, a code made up of only 0s and 1s (binary numbers) is applied.
I've heard the term "ASCII code" used a lot so far,
Some of you may only have a vague idea of what it is....
For those who are familiar with ASCII codes and those who are not, a picture is worth a thousand words!
Please take a look at the ASCII code table (^^)
Click here to see the ASCII code table
The main subject
Now, let's return to the main subject.
Why is the number "49" output when the number "1" is input to the variable "num" of the char type?
The ASCII code table will tell you the reason.
Check the ASCII code!
That's right!
The ASCII code corresponding to the character "1" is "49.
Now that we have solved the mystery of the "49" output, let's look back at the previous problem.
Assign an integer to the variable num defined in char type and print it out with printf ( " %d " );.
However, the output value should be the same as the input value.
Hint: Subtract ' 0 ' from num.
If you read the last and this blog, you may know the answer to this problem.
If you still don't understand well, it's okay! Let's think about it together (^▽^)/.
But that's all for this time!
In my next blog, I will finally tackle this problem! Don't miss it (@_@)
Summary
When the input with getchar( ) is printed out with printf ( " %d " );, its ASCII code is displayed in decimal.
Goo's tweet
I guess ASCII codes were an indispensable and important part of the bit world.
A bridge to the next time
Let's consider the meaning of "Hint: subtract ' 0 ' from num!
Extra
In addition to the decimal representation, the ASCII code table also includes ASCII codes in hexadecimal representation.
In the bit world, hexadecimal display is the mainstream, so it is good to check this as well.
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