英語版 Introduction to C Language that Takes You to Heaven: Hexadecimal Structures ver. 2.2117
When displaying numerical data using the %c in printf, the data will be converted to an int type
When displaying numerical data using the %c in printf, the data will be converted to an int type
When displaying numerical data using the %c output conversion specifier in printf, regardless of whether it is stored as an int or char type, the data will be converted to an int type and the image data (character) will be displayed on the command prompt screen.
"When variable a is printed using printf with the %c output conversion specifier, the program displays:"
↓
#include <stdio.h>
int main(void)
{
char a = 109;
printf("%c\n", a);
return 0;
}
Program Output:
m
The variable "a" of type char is assigned the numerical value 109, which is stored in memory as 01101101 in 1 byte.
However, when a is passed as an argument to printf, as shown in the following code:
printf("%c\n", a);
Due to data processing, the value 109 will be converted and stored in 4 bytes of memory as follows:
↓
00000000 00000000 00000000 01101101
In other words, it will be converted and stored in the format of an int type (4 bytes).
Next, if you assign the numeric data 109 to an integer variable a and print the value of a using the %c conversion specifier with printf, the program will output:
#include<stdio.h>
int main(void)
{
int a = 109;
printf("%c\n", a);
return 0;
}
Output:
m
Furthermore, if you directly assign the numeric data 109 to the second argument of printf and print it using the %c conversion specifier, as shown in the following program:
#include<stdio.h>
int main(void)
{
printf("%c\n", 109);
return 0;
}
Output:
m
Even when assigning the numeric data 109 directly to the second argument of printf, the data is automatically stored in memory in the int format (32 bits) due to data processing reasons.
Solar: "I see. Anyway, when you output the numerical data 109 using the %c output conversion specifier with printf, the character 'm' is displayed.
Conversely, to make the compiler recognize ☆m☆ as character data, it is enclosed in single quotes ' ' and assigned to a variable of type char named a, and when a is printed using the %d output conversion specifier, 109 is displayed.
#include <stdio.h>
int main()
{
char a = 'm';
printf("%d\n", a);
return 0;
}
Program output:
109
When 'm' is assigned to a variable of type char, it is stored in 1 byte of memory as 01101101 (numerical data 109). However, when a is passed as an argument to printf, due to data processing, 01101101 (109) is stored in 4 bytes of memory as 00000000 00000000 00000000 01101101, which means it is stored in int format (32 bits).
Furthermore, even if you store the character data 'm' directly in the second argument of the printf function and output it using the %d output conversion specifier:
#include <stdio.h>
int main()
{
printf("%d\n", 'm');
return 0;
}
Program output:
109
As you can see from the output of this program, 109 is displayed. In this case as well, the character data 'm' is first stored in 1 byte of memory as 01101101 (numerical data 109), but when the character data 'm' is passed as an argument to printf, due to data processing, 01101101 (numerical data 109) is stored in 4 bytes of memory as 00000000 00000000 00000000 01101101, which means it is stored in int format (32 bits).
Solar: "I see. So to summarize what we have learned so far, when you use the %c output conversion specifier to print the numerical data 109 (or a variable storing the numerical data 109) with printf, the character ☆m☆ (the image data of the character) is displayed on the command prompt screen. In this case, 109 is the ASCII code assigned to the image data of the character ☆m☆. (An ASCII code is a number assigned to the image data of a character, which the computer uses to call up the image data of the character). Conversely, if you want to know the ASCII code of that character, you can use the %d output conversion specifier in printf that character data, which in this case is the character 'm', and the ASCII code (number) corresponding to that character data, which is 109 in this case, will be displayed on the command prompt screen."
新規登録で充実の読書を
- マイページ
- 読書の状況から作品を自動で分類して簡単に管理できる
- 小説の未読話数がひと目でわかり前回の続きから読める
- フォローしたユーザーの活動を追える
- 通知
- 小説の更新や作者の新作の情報を受け取れる
- 閲覧履歴
- 以前読んだ小説が一覧で見つけやすい
アカウントをお持ちの方はログイン
ビューワー設定
文字サイズ
背景色
フォント
組み方向
機能をオンにすると、画面の下部をタップする度に自動的にスクロールして読み進められます。
応援すると応援コメントも書けます