Site Search

Introduction

Hello. It's guu.

Last time, we learned that if we input 1 with getchar( ) and output it with printf ( “ %d ” );, 1 is not printed.

 

This time, I would like to approach the mystery that 49 was output even though 1 was input.

Points are ASCII codes.

example

まず例として、「 a 」を入力した場合を考えてみましょう。

In the program below, "a" is assigned to the char type variable ch.

I will try two output methods: printf (“%c”); and printf (“%d”);.

#include <stdio.h> int main (void) { char ch; // char型の変数 ch を定義 printf ( “ 文字を入力してください ” );     //コマンドラインから文字( a )を入力 ch = getchar(); //入力した文字( a )を ch に代入 printf ( “ch/c : %c ” . ch); //文字として ch を出力 printf( “ ¥n ” ); printf ( “ch/d : %d ” , ch); // 10進数で ch を出力 return 0; }

What values will be printed when you run this program?

 

 

Output result

The first output "a" was output as a character.

The second output "97" indicates the ASCII code of a.

 

Now let's dive a little deeper into the ASCII code.

Why do we need ASCII code in the first place?

 

Inside the computer is the world of bits. It can only be expressed as 0 or 1.

But we use various characters and symbols besides 0 and 1.

 

“How can we express characters and symbols in the world of bits?”

 

“…It would be nice to apply a code made up of only 0s and 1s to letters and symbols!”

 

Yes, this is the "ASCII code"!

 

But wait! !

The ASCII code of "a" output by the previous program is "97".

It is neither 0 nor 1. that? that's strange… (-_-;)

 

Don't worry! This is because printf ( “ %d ” ); is a decimal representation.

In fact, a code (binary number) made up of only 0s and 1s is applied.

Up to this point, the term "ASCII code" has been used a lot.

I think some people just don't understand it...

 

Seeing is believing for those who are like that and those who are not!

Please actually look at the ASCII code table (^^)

Click here for the ASCII code table♪

main subject

Let's get back to the topic.

Why is "49" output when the number "1" is input to the char type variable num?

 

Again, if you look at the ASCII code table, you should be able to understand why.

Check the ASCII code immediately!

 

it is!

The ASCII code corresponding to "1" entered as a character was "49".

Now that we have solved the mystery that 49 is output, let's look back at the previous question.

 

[Problem] Assign an integer to the variable num defined as a char type and output it with printf ( “ %d ” );

However, make sure that the output value is 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 question.

It's okay if you don't know as usual! Let's think together (^▽^)/

 

But that's all for now!

We will tackle this issue in our next blog! Do not miss it (@_@)

Summary

When you output what you input with getchar( ) with printf ( “ %d ” );, the ASCII code is displayed in decimal.

Guu's tweet

The ASCII code was an indispensable and important thing in the bit world.

bridge to next time

Think about what "hint: subtract ' 0 ' from num" means!

bonus

The ASCII code table includes not only decimal numbers but also hexadecimal ASCII codes.

Hexadecimal representation is the mainstream in the world of bits, so it would be nice to check this as well.

Check out the "Actual Realities of ASCII Codes" series! !

Situation of ASCII code [Part 1] ~ 49 even though it is 1 ? Input/output format ~

When I tried to output 1, it turned out to be 49! ? When I searched for the cause, I found that it had something to do with the ASCII code.

 

ASCII Code Situation [Part 2] ~Correspondence between ASCII Codes and Characters~

We will approach the mystery that 1 has become 49!

 

ASCII Code Situation [Part 3] - Subtract '0' from num? ~

Finally, the meaning of "subtracting '0' from num" becomes clear! !