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

Author Avatar
高宇哲 3月 24, 2017

基本定義

物件(Object): 記憶體裡的某個位置,它的內容可以用來表示某些值
變數(Variable): 有名稱的物件
型別(Type): 物件占用記憶體的空間,及其值得編碼方式

C語言型別的分類

1.基本型別

  • 標準浮點數型別
  • 擴充浮點數型別
  • 實數浮點數型別
  • 複數浮點數型別

2.列舉型別(Enumerated types)

3.void型別

4.衍生型別

  • 指標型別(Pointer types)
  • 陣列型別(Array types)
  • 結構型別(Structure types)
  • 聯合型別(Union types)
  • 函式型別(Function types)

整數型別

標準有號型別 & 標準無號型別

下表列出了有號以及無號的標準型別,每個有號型別都有對應的無號型別,其中_Bool為C99加入用來表示布林值的無號型別。

有號無號
型別同義型別同義
signed charunsigned char
intsigned, signed intunsinged intunsigned
shortshort int, singed int, signed short, signed short intunsigned shortunsigned short int
longlong int, signed long, signed long intunsinged longunsinged long int
long long (C99)long long int, signed long long, singed long long intunsinged long long (C99)unsinged long long int
_Bool (C99)bool (stdbool.h)

你可以自行決定是否要將char變數裡的數字解譯成字元碼或是其他東西,以以下程式碼為例

#include<stdio.h>
int main()
{
    char ch='A'; // A variable with type char.
    printf("The character %c has the character code %d.\n", ch, ch);
    for ( ; ch <= 'Z'; ++ch )
        printf("%2c", ch);
    return 0;
}

此程式碼結果將會是

The character A has the character code 65.
 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

值得注意的是,若需要char保有小於0或大於127的值,則應該改用signed char或unsinged char

C只對其他標準型別定義最小儲存空間(Minimum Storage Sizes),雖然整數型別可能比它們的最小儲存空間還大,但實作上必須以以下順序為原則

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

在C compiler中,limits.h標頭檔裡可以找到整數型別的值範圍,其中也定義了INT_MIN、INT_MAX、UNIT_MAX等巨集,以以下程式碼為例

// limits.c: Display the value ranges of char and int.
// ---------------------------------------------------
#include <stdio.h>
#include <limits.h> // Contains the macros CHAR_MIN, INT_MIN, etc.

int main( )
{
    printf("Storage sizes and value ranges of the types char and int\n\n");
    printf("The type char is %s.\n\n", CHAR_MIN < 0 ? "signed" :"unsigned");
    printf(" Type Size (in bytes) Minimum Maximum\n"
    "---------------------------------------------------\n");
    printf(" char %8d %20d %15d\n", sizeof(char), CHAR_MIN, CHAR_MAX );
    printf(" int  %8d %20d %15d\n", sizeof(int), INT_MIN, INT_MAX );
    return 0;
}

編譯後執行結果將會顯示char以及int的最小和最大值

Storage sizes and value ranges of the types char and int

The type char is signed.

 Type Size (in bytes) Minimum Maximum
---------------------------------------------------
 char        1                 -128             127
 int         4          -2147483648      2147483647

tutorialspoint幫我們整理了limits.h還定義了哪些巨集

擁有精確寬度的整數型別(C99)

ISO/IEC 9899第225頁定義了stdint.h的所有規格,其中定義了許多已知寬度需求的整數型別

以下擷取規格書中描述stdint.h所定義的種類

  • integer types having certain exact widths
  • integer types having at least certain specified widths
  • fastest integer types having at least certain specified widths
  • integer types wide enough to hold pointers to objects
  • integer types having greatest width
型別意義C99實作
intN_t uintN_t寬度必須為N位元的整數型別not required to provide the types
int_leastN_t uint_leastN_t寬度至少為N位元的整數型別Required for N = 8, 16, 32, 64
int_fastN_t uint_fastN_t寬度至少為N位元且處理最快的型別Required for N = 8, 16, 32, 64
intmax_t uintmax_t所實作的最寬整數型別Required
intptr_t uintptr_t足以儲存指標值的整數型別not required to provide the types