Introduction
Hello. My name is Guu.
This time, I would like to share with you an episode about ASCII code, which is one of the essential knowledge you need to know when learning C language.
One day during my training, I came across ASCII code.
Explanation: "ASCII code is an integer representation of numbers, alphabets, symbols, etc."
Guu "Hmmm..."
When I first learned about ASCII codes, I had only this impression.
However, I later found out that ASCII codes are actually very important.
Consider the following 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.
Im not sure, so I made the following program for now.
#include <stdio.h>
int main(void)
{
char num; // define variable num of char type
printf("Please input integer"); // input integer from command line
num = getchar(); // assign input integer to num
printf("num : %d", num); // print num in decimal
return 0;
}
Now that the program is ready, compile and run it!
As a test, I input 1 from the keyboard.
Now, what value will be output?
----------------------------------------------------- Output result
-----------------------------------------------------
Guu "???????????"
Why was the number 49 output instead of 1?
I panicked at once.
Explanation
The point this time is that "1" was input with getchar( ) and output with printf ( " %d " );.
Generally, printf ( " %c " ); is used to output what was inputted by getchar( ).
%c prints the input value (character) as a character.
However, in this problem, we must use printf ( " %d " ); as output.
%d prints the input value as a decimal (number).
In other words...
If you input "1" in the following program, "1" will be output as a character.
#include <stdio.h>
int main(void)
{
char num; // define variable num of char type
printf("Please input an integer"); // input an integer from command line
num = getchar(); // assign input integer to num
printf("num : %c", num ); // print num as a character
return 0;
}
In this problem, "1" was input with getchar( ) and output as a decimal number using printf ( " %d " );.
Because the input and output formats were different, the input and output values did not match.
Guu - "But why did it output 49 when I input 1?"
This is where ASCII codes come in.
But, I'm not sure!
That's all for this time.
In my next blog entry, I will reveal the reason why 49 was output!
Enjoy (^^)
Summary
When you assign a number to a char variable via getchar( ), it is treated as a "character.
When input as a character is output with printf ( " %d " );, the input value and the output value do not match
Guu's Musings
I didn't really understand the format of input/output, but I guess it was very important.
I'll try to be conscious of it and look at the code from now on!
Bridge to next time
Let's look at the ASCII code table!
Check out the "The reality of ASCII Codes" series in one place!
The reality of ASCII codes [Part 1] - 1 but 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 ASCII Code Situation [Part 3] - Subtracting '0' from num?
Finally, the meaning of "num minus '0'" is revealed!
New Engineer's Blush Blog Articles