求不同資料型別在記憶體中占多大位元組?
#include <stdio.h>
int main()
{
short a;
int b;
long c;
long long d;
printf("The size of short is %d byte\n", sizeof(a));
printf("The size of int is %d byte\n", sizeof(b));
printf("The size of long is %d byte\n", sizeof(c));
printf("The size of long long is %d byte\n", sizeof(d));
return 0;
}

總結:1.sizefo ()算變數或型別的大小
2.資料型別:
(1)整形:char(1byte),short(2byte),int(4byte),
long(4byte),long long(8byte)
(2)浮點型:float(4byte,精確到小數點后后7位)
double(8byte,精確到小數點后16位)
例題:

由于int a,int b定義了a,b為整形所以輸出結果也為整形
改進:將輸出的a,b強制轉化為浮點型,并將列印出整形%d改為列印出浮點型%lf其運行結果如下

總結:%d 列印 int
%lld 列印 long long
%f 列印 float
%lf 列印 double
%c 列印 char
%.2lf 列印 結果保留小數點后2位
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/224830.html
標籤:其他
