重新審視C語言: 型別(Type)(下)

Author Avatar
高宇哲 5月 17, 2017

浮點型別

依照IEC 60559標準,下表呈現浮點型別不同的特性

型別儲存大小值範圍最小正值精確度
float4 bytes正負3.4E+381.2E-386 digits
double8 bytes正負1.7E+3082.3E-30815 digits
long double10 bytes正負1.1E+49323.4E-493219 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);