英語版 Introduction to C Language that Takes You to Heaven: Hexadecimal Structures ver. 2.2117
When printf("%d¥n", 1) is executed, 1 is stored in integer format, 1.0 is stored in double format.
When printf("%d¥n", 1) is executed, 1 is stored in integer format, 1.0 is stored in double format.
When printf("%d\n", 1) is executed, the numerical data 1 is automatically stored in memory in integer format, while numerical data 1.0 is stored in double format. This is the basic mechanism of the printf function.
Solar: "Today, I would like to learn about the basic and fundamental mechanism of the printf function."
Aretha: "You mean the basic and fundamental mechanism of the printf function, Solar?"
Solar: "Yes, that's right. First, please take a look at the following program."👇
#include <stdio.h>
int main(void)
{
printf("%d\n", 1);
return 0;
}
Build and execution result:
1
Solar: "It appeared! It's a basic program 😊. When this program is executed, the following code is executed:
printf("%d\n", 1);
Then, the number 1 is displayed on the command prompt screen.
When printf("%d\n", 1); is executed, the integer data 1 is automatically stored in memory in int format. This is the basic mechanism of the printf function."
Aretha: "Solar, when printf("%d\n", 1); is executed, does the numerical data 1 first get stored automatically in memory in int format and then displayed as 1 on the command prompt screen?"
Solar: "Yes, that's right. The key point is that the numerical data 1 is first stored in memory in int format and then displayed on the screen using printf function."
Aretha: "Solar, what happens when printf("%d\n", 100); is executed? Is 100 automatically stored in memory in int format?"
Solar: "Yes, that's right. Basically, integer data is stored in memory in int format."
Solar: "Now, please take a look at the following program."👇
#include <stdio.h>
int main(void)
{
printf("%f\n", 1.0);
return 0;
}
Build and execution result:
1.000000
Solar: "When this program is executed, printf("%f\n", 1.0) is executed and 1.000000 is displayed on the command prompt screen. At this time, the numerical data 1.0 is automatically stored in memory in double format."
Arisa: "Integer data 1 is in the format of int type, and real number data 1.0 is in the format of double type and stored in memory, isn't it?"
Solar: "Let's play a little here. I changed the %f output conversion specifier of the current program to %d output conversion specifier and will execute the program. Here's the program."
👇
#include <stdio.h>
int main(void) {
printf("%d\n", 1.0); //I changed %f output conversion specifier to %d output conversion specifier
return 0;
}
The warning
'printf': format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'double'
is displayed, and '0' is displayed on the command prompt screen.
It indicates that when the first argument of printf is %d output conversion specifier, the second argument to be used must be data stored in memory in int type format, that is,
integer data
or
int type variable that stores integer data."
Arisa: "If we observe closely, is it because we used %d output conversion specifier instead of %f output conversion specifier to display the real number data 1.0 that caused 0 to be displayed on the command prompt screen?"
Solar: "Yes, that's right. And pay attention to the warning message. 👇
Warning C4477 'printf': format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'double'
Let's focus on 'variadic argument 1 has type 'double''. First, do you know what a variadic argument is?"
Arisa: "Variadic argument... Is it the one that can change the number of arguments?"
Solar: "Well done! That's correct. .
When using the printf function,
the format is usually written as follows:
printf("%d\n", 1);
The part of the first argument enclosed in double quotation marks " " must always be written as follows:
printf("The first argument is required.");
In other words, the printf function always has one argument.
Also, when using the printf function,
you can write it as follows:
printf("%d\n,%d\n,%d\n", 1,2,3);
The part of the first argument enclosed in double quotation marks " " is absolutely necessary,
but the second argument 1, third argument 2, and fourth argument 3
may or may not be necessary depending on the situation.
This means that the number of arguments can vary.
So,
The second argument is 1, the third argument is 2, and the fourth argument is 3, which are called variable-length arguments.
Now, in this program
printf("%d\n", 1.0);
variable-length argument 1.0 is of type 'double.'
This means that the variable-length argument 1.0 in the printf("%d\n", 1.0) statement is stored in memory in double format.
When printf("%d\n", 1.0) is executed, the numerical data 1.0 is automatically stored in memory in double format.
Alesa: "So that's what that warning message meant."
Alesa: "Solar, for example, with int format, you can store integer data ranging from -2147483648 to 2147483647 in memory,
but what happens if you try to store integer data 10000000000, which is larger than 2147483647,
like in the printf("%d\n", 10000000000) statement?
What format will this numerical data 10000000000 be stored in memory?"
Solar: "Ha ha, that's a nice and timely question, Alesa.
Let's try running the program with printf("%d\n", 10000000000);"
👇
#include<stdio.h>
int main(void)
{
printf("%d\n", 10000000000);//I changed %f output conversion specifier to %d output conversion specifier.
return 0;
}
A warning message
Warning C4477 'printf': Format string '%d' requires an argument of type 'int', but variadic argument 1 has type '__int64'
is displayed, and
1410065408
is displayed on the command prompt screen.
Solar: "In this case, the numerical data 10000000000 is stored in the __int64 type. However, with the %d output conversion specifier, you cannot correctly display the numerical data stored in the __int64 type on the command prompt screen. That's why 1410065408 is displayed on the command prompt screen. By the way, the __int64 type is a t data storage type that was developed by Microsoft. There are other data storage types,
such as
__int8,
__int16,
__int32.
and so on,
It's a type you may not have seen before."
The __int8 type has a data storage capacity of 1 byte, which is equivalent to the char type, and can store integer data ranging from -128 to 127.
The __int16 type has a data storage capacity of 2 bytes, which is equivalent to the short type, and can store integer data ranging from -32,768 to 32,767.
The __int32 type has a data storage capacity of 4 bytes, which is equivalent to the int type, and can store integer data ranging from -2,147,483,648 to 2,147,483,647.
And the __int64 type has a data storage capacity of 8 bytes, which is equivalent to the long long type, and can store integer data ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Alesa: "So __int64 can store integer data up to about 922 quadrillion. That means that 10000000000, which is much smaller than that, can be stored easily (^^)."
新規登録で充実の読書を
- マイページ
- 読書の状況から作品を自動で分類して簡単に管理できる
- 小説の未読話数がひと目でわかり前回の続きから読める
- フォローしたユーザーの活動を追える
- 通知
- 小説の更新や作者の新作の情報を受け取れる
- 閲覧履歴
- 以前読んだ小説が一覧で見つけやすい
アカウントをお持ちの方はログイン
ビューワー設定
文字サイズ
背景色
フォント
組み方向
機能をオンにすると、画面の下部をタップする度に自動的にスクロールして読み進められます。
応援すると応援コメントも書けます