重新審視C語言: 型別(Type)(下)
浮點型別
依照IEC 60559標準,下表呈現浮點型別不同的特性
型別 | 儲存大小 | 值範圍 | 最小正值 | 精確度 |
---|---|---|---|---|
float | 4 bytes | 正負3.4E+38 | 1.2E-38 | 6 digits |
double | 8 bytes | 正負1.7E+308 | 2.3E-308 | 15 digits |
long double | 10 bytes | 正負1.1E+4932 | 3.4E-4932 | 19 digits |
考慮以下程式碼
#include<stdio.h>
#include<float.h>
int main()
{
puts("\nCharacteristics of the type float\n");
printf("Storage size: %d bytes\n"
"Smallest positive value: %E\n"
"Greatest positive value: %E\n"
"Precision: %d decimal digits\n",
sizeof(float), FLT_MIN, FLT_MAX, FLT_DIG);
puts("\nAn example of float precision:\n");
double d_var = 12345.6;
float f_var = (float)d_var;
printf("The floating-point number"
"%18.10f\n", d_var);
printf("has been stored in a variable of type float as the value %18.10f\n", f_var);
printf("The rouding error is"
"%18.10f\n", d_var - f_var);
return 0;
}
最後結果會是
Characteristics of the type float
Storage size: 4 bytes
Smallest positive value: 1.175494E-38
Greatest positive value: 3.402823E+38
Precision: 6 decimal digits
An example of float precision:
The floating-point number 12345.6000000000
has been stored in a variable of type float as the value 12345.5996093750
The rouding error is 0.0003906250
列舉型別
基本格式
enum [ 識別字 ] { 列舉清單 };
例如:
enunm color { black, red, green, yellow, blue , white=7, gray };
void型別
指向void的pointer
記憶體管理函式就是簡單的例子:
- void *malloc(size_t size);
- void realloc(void ptr, size_t size);
- void free(void *ptr);