In int a=1; 1 stored in memory in the form of an integer is stored in the integer variable "a".

Solar: "Today, I would like to take a look at the mechanism when a very basic and important instruction, 🌞int a = 1;🌞, is executed."



#include <stdio.h>


int main(void){

int a = 1;

return 0;

}


Build Execution Result: 1


Solar: "Everyone, the variable declaration and initialization of variable a,


🌞int a = 1;🌞,


is a basic instruction that you often see. In this instruction, first, the numeric data 1 is stored in memory in int format. After that, the numeric data 1 stored in int format in memory is stored in variable a, which is also in int format. It is not the case that the numeric data 1 is directly assigned to variable a in int format."


Aresa: "😊 So, there is such a mechanism in a short instruction."


Solar: "Yes (^_-)-☆. Now, take a look at the following program.



#include <stdio.h>


int main(void) {

int a = 1.12345;

printf("%d\n", a);

return 0;

}


Warning message: "warning C4244: '=': conversion from 'double' to 'int', possible loss of data"


Build Execution Result:


1


Solar: "Please pay attention to the instruction, 🌞int a = 1.12345;🌞, in this program. When this instruction is executed, first, the numeric data 1.12345 is stored in memory in double format. Then, the numeric data 1.12345 stored in memory in double format is assigned to variable a in int format.


At this time, the numeric data 1.12345 stored in memory in double format cannot be directly assigned to variable a in int format.


Therefore, the type conversion of numeric data is performed, and the fractional part of 1.12345 is truncated, resulting in 1 being assigned to variable a in int format.


At this time, due to the data conversion from 1.12345 to 1, the data information of the decimal part of 1.12345 is lost.


The compiler warns us of this by displaying the warning message: 'warning C4244: '=': conversion from 'double' to 'int', possible loss of data'.


From this warning message, it can be understood that the numeric data 1.12345 is stored in memory in double format when the instruction 🌞int a = 1.12345;🌞 is executed."


Solar: "Furthermore, take a look at the following program:



#include <stdio.h>


int main(void) {

float a = 1;

printf("%f\n", a);

return 0;

}


Build Execution Result: 1.000000


Solar: "This time, no warning message is displayed. In this program, first, the numeric data 1 is stored in memory in int format. Then, the numeric data 1 stored in memory in int format is assigned to variable a in float format. Since the numeric data 1 stored in memory in int format is assigned to variable a in float format, a type conversion of numeric data occurs, and 1 is assigned to variable a in float format.

  • Xで共有
  • Facebookで共有
  • はてなブックマークでブックマーク

作者を応援しよう!

ハートをクリックで、簡単に応援の気持ちを伝えられます。(ログインが必要です)

応援したユーザー

応援すると応援コメントも書けます

新規登録で充実の読書を

マイページ
読書の状況から作品を自動で分類して簡単に管理できる
小説の未読話数がひと目でわかり前回の続きから読める
フォローしたユーザーの活動を追える
通知
小説の更新や作者の新作の情報を受け取れる
閲覧履歴
以前読んだ小説が一覧で見つけやすい
新規ユーザー登録無料

アカウントをお持ちの方はログイン

カクヨムで可能な読書体験をくわしく知る