C 數(shù)據(jù)類型限制

2018-05-20 15:26 更新

學(xué)習(xí)C - C數(shù)據(jù)類型限制

下表顯示了與每個(gè)有符號(hào)整數(shù)類型的限制相對(duì)應(yīng)的符號(hào)名稱。

類型下限上限
charCHAR_MINCHAR_MAX
shortSHRT_MINSHRT_MAX
intINT_MININT_MAX
longLONG_MINLONG_MAX
long longLLONG_MINLLONG_MAX

無(wú)符號(hào)整數(shù)類型的下限全部為0,因此沒(méi)有符號(hào)。

對(duì)應(yīng)于無(wú)符號(hào)整數(shù)類型的上限的符號(hào)是UCHAR_MAX,USHRT_MAX,UINT_MAX,ULONG_MAX和ULLONG_MAX。

為了能夠在程序中使用任何這些符號(hào),您必須在源文件中的limits.h頭文件中使用#include指令:

#include  

您可以初始化一個(gè)類型為int的最大可能值的變量,如下所示:

int number = INT_MAX;

此語(yǔ)句將數(shù)值的值設(shè)置為盡可能大的值,無(wú)論編譯器用于編譯代碼的可能性如何。


例子

下表列出了表示浮點(diǎn)類型的范圍限制的符號(hào)

類型下限上限
floatFLT_MINFLT_MAX
doubleDBL_MINDBL_MAX
long doubleLDBL_MINLDBL_MAX

注意

該程序輸出與頭文件中定義的符號(hào)對(duì)應(yīng)的值。


    #include <stdio.h>                          // For command line input and output 
    #include <limits.h>                         // For limits on integer types 
    #include <float.h>                          // For limits on floating-point types 

    int main(void) 
    { 
      printf("Variables of type char store values from %d to %d\n", CHAR_MIN, CHAR_MAX); 
      printf("Variables of type unsigned char store values from 0 to %u\n", UCHAR_MAX); 
      printf("Variables of type short store values from %d to %d\n", SHRT_MIN, SHRT_MAX); 
      printf("Variables of type unsigned short store values from 0 to %u\n", USHRT_MAX); 
      printf("Variables of type int store values from %d to %d\n", INT_MIN,  INT_MAX); 
      printf("Variables of type unsigned int store values from 0 to %u\n", UINT_MAX); 
      printf("Variables of type long store values from %ld to %ld\n", LONG_MIN, LONG_MAX); 
      printf("Variables of type unsigned long store values from 0 to %lu\n", ULONG_MAX); 
      printf("Variables of type long long store values from %lld to %lld\n", LLONG_MIN, LLONG_MAX); 
      printf("Variables of type unsigned long long store values from 0 to %llu\n", ULLONG_MAX); 
      printf("\nThe size of the smallest positive non-zero value of type float is %.3e\n", FLT_MIN); 
      printf("The size of the largest value of type float is %.3e\n", FLT_MAX); 
      printf("The size of the smallest non-zero value of type double is %.3e\n", DBL_MIN); 
      printf("The size of the largest value of type double is %.3e\n", DBL_MAX); 
      printf("The size of the smallest non-zero value of type long double is %.3Le\n", LDBL_MIN); 
      printf("The size of the largest value of type long double is %.3Le\n",  LDBL_MAX); 
      printf("\n Variables of type float provide %u decimal digits precision. \n", FLT_DIG); 
      printf("Variables of type double provide %u decimal digits precision. \n", DBL_DIG); 
      printf("Variables of type long double provide %u decimal digits precision. \n", 
                                                                            LDBL_DIG); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。


sizeof運(yùn)算符

您可以使用sizeof運(yùn)算符找出給定類型占用的字節(jié)數(shù)。

表達(dá)式sizeof(int)將導(dǎo)致類型為int的變量占用的字節(jié)數(shù),結(jié)果是size_t類型的整數(shù)。

類型size_t在標(biāo)準(zhǔn)頭文件stddef.h中定義,并將對(duì)應(yīng)于基本整數(shù)類型之一。

您可以存儲(chǔ)應(yīng)用sizeof運(yùn)算符產(chǎn)生的值:

size_t size = sizeof(long long);

該程序?qū)⑤敵雒總€(gè)數(shù)字類型占用的字節(jié)數(shù):


    #include <stdio.h> 

    int main(void) 
    { 
      printf("Variables of type char occupy %u bytes\n", sizeof(char)); 
      printf("Variables of type short occupy %u bytes\n", sizeof(short)); 
      printf("Variables of type int occupy %u bytes\n", sizeof(int)); 
      printf("Variables of type long occupy %u bytes\n", sizeof(long)); 
      printf("Variables of type long long occupy %u bytes\n", sizeof(long long)); 
      printf("Variables of type float occupy %u bytes\n", sizeof(float)); 
      printf("Variables of type double occupy %u bytes\n", sizeof(double)); 
      printf("Variables of type long double occupy %u bytes\n", sizeof(long double)); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)