歡迎訪問我的新博客: http://www.milkcu.com/blog/
原文地址: http://www.milkcu.com/blog/archives/1367305920.html
前言
聲明一個變量,經(jīng)常要考慮的問題是這個類型的變量能不能裝的下。今天 MilkCu 就總結(jié)下吧,以解除后顧之憂。
關(guān)于變量取值范圍的問題,在Kernighan的《C程序設(shè)計語言》第28頁練習(xí)2-1就提到過。
編寫一個程序以確定分別由signed及unsigned限定的char、short、int與long類型變量的取值范圍。采用打印標準頭文件中的相應(yīng)值以及直接計算兩種方式實現(xiàn)。后一種方式的實現(xiàn)較困難一些,因為要確定各種浮點類型的取值范圍。
這書是針對C89的,隨著C99及C++的廣泛應(yīng)用long long int類型也是一種美妙的選擇。
實現(xiàn)方案
標準規(guī)定:各種類型的取值范圍必須在頭文件<limits.h>中定義。不同類型在不同的硬件上有不同的長度,所以它們在不同機器上的取值范圍也往往會不同。我們可以通過頭文件確定類型取值范圍。
函數(shù)sizeof()
可以通過函數(shù)sizeof()用字節(jié)計算參數(shù)并返回的字節(jié)數(shù),間接得到類型寬度,源代碼如下:
# include <stdio.h> int main(void) { printf("byte of short = %d\n", sizeof(short)); printf("byte of int = %d\n", sizeof(int)); printf("byte of long = %d\n", sizeof(long)); printf("byte of long long int = %d\n", sizeof(long long int)); return 0; }
打印常量
可以通過打印頭文件的方法獲得每種類型的取值范圍:
# include <stdio.h> # include <limits.h> //determine ranges of types int main(void) { //signed types printf("signed char min = %d\n", SCHAR_MIN); printf("signed char max = %d\n", SCHAR_MAX); printf("signed short min = %d\n", SHRT_MIN); printf("signed short max = %d\n", SHRT_MIN); printf("signed short min = %d\n", INT_MIN); printf("signed long min = %ld\n", LONG_MIN); printf("signed long max = %ld\n", LONG_MAX); printf("signed long long int min = %lld\n", LONG_LONG_MIN); printf("signed long long int max = %lld\n", LONG_LONG_MAX); //unsigned types printf("unsigned char max = %u\n", UCHAR_MAX); printf("unsigned short max = %u\n", USHRT_MAX); printf("unsigned int max = %u\n", UINT_MAX); printf("unsigned long max = %lu\n", ULONG_MAX); printf("unsigned long long int max = %llu\n", ULONG_LONG_MAX); return 0; }
打印結(jié)果如下:
signed char min = -128 signed char max = 127 signed short min = -32768 signed short max = -32768 signed short min = -2147483648 signed long min = -2147483648 signed long max = 2147483647 signed long long int min = -9223372036854775808 signed long long int max = 9223372036854775807 unsigned char max = 255 unsigned short max = 65535 unsigned int max = 4294967295 unsigned long max = 4294967295 unsigned long long int max = 18446744073709551615
頭文件
可以從頭文件<limits.h>中更詳細的看到它們的宏定義,<limits.h>(來自 Dev-Cpp 5.4.0 MinGW 4.7.2 )如下:
/* * limits.h * This file has no copyright assigned and is placed in the Public Domain. * This file is a part of the mingw-runtime package. * No warranty is given; refer to the file DISCLAIMER within the package. * * Functions for manipulating paths and directories (included from io.h) * plus functions for setting the current drive. * * Defines constants for the sizes of integral types. * * NOTE: GCC should supply a version of this header and it should be safe to * use that version instead of this one (maybe safer). * */ #ifndef _LIMITS_H_ #define _LIMITS_H_ /* All the headers include this file. */ #include <_mingw.h> /* * File system limits * * TODO: NAME_MAX and OPEN_MAX are file system limits or not? Are they the * same as FILENAME_MAX and FOPEN_MAX from stdio.h? * NOTE: PATH_MAX is the POSIX equivalent for Microsoft's MAX_PATH; the two * are semantically identical, with a limit of 259 characters for the * path name, plus one for a terminating NUL, for a total of 260. */ #define PATH_MAX 260 /* * Characteristics of the char data type. * * TODO: Is MB_LEN_MAX correct? */ #define CHAR_BIT 8 #define MB_LEN_MAX 2 #define SCHAR_MIN (-128) #define SCHAR_MAX 127 #define UCHAR_MAX 255 /* TODO: Is this safe? I think it might just be testing the preprocessor, * not the compiler itself... */ #if ('\x80' < 0) #define CHAR_MIN SCHAR_MIN #define CHAR_MAX SCHAR_MAX #else #define CHAR_MIN 0 #define CHAR_MAX UCHAR_MAX #endif /* * Maximum and minimum values for ints. */ #define INT_MAX 2147483647 #define INT_MIN (-INT_MAX-1) #define UINT_MAX 0xffffffff /* * Maximum and minimum values for shorts. */ #define SHRT_MAX 32767 #define SHRT_MIN (-SHRT_MAX-1) #define USHRT_MAX 0xffff /* * Maximum and minimum values for longs and unsigned longs. * * TODO: This is not correct for Alphas, which have 64 bit longs. */ #define LONG_MAX 2147483647L #define LONG_MIN (-LONG_MAX-1) #define ULONG_MAX 0xffffffffUL #ifndef __STRICT_ANSI__ /* POSIX wants this. */ #define SSIZE_MAX LONG_MAX #endif #if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \ || !defined(__STRICT_ANSI__) /* ISO C9x macro names */ #define LLONG_MAX 9223372036854775807LL #define LLONG_MIN (-LLONG_MAX - 1) #define ULLONG_MAX (2ULL * LLONG_MAX + 1) #endif /* * The GNU C compiler also allows 'long long int' */ #if !defined(__STRICT_ANSI__) && defined(__GNUC__) #define LONG_LONG_MAX 9223372036854775807LL #define LONG_LONG_MIN (-LONG_LONG_MAX-1) #define ULONG_LONG_MAX (2ULL * LONG_LONG_MAX + 1) /* MSVC compatibility */ #define _I64_MIN LONG_LONG_MIN #define _I64_MAX LONG_LONG_MAX #define _UI64_MAX ULONG_LONG_MAX #endif /* Not Strict ANSI and GNU C compiler */ #endif /* not _LIMITS_H_ */
閱讀頭文件,在注釋的幫助下,我們可以更明確的看到數(shù)據(jù)類型的定義。
總結(jié)
簡而言之,對于MinGW32:
int類型,能完整表示9位;
unsigned long long int類型,能完整表示19位。
后記
又一輪新生活開始了,要充實快樂每一天。
借用奧斯托洛夫斯基在 《鋼鐵是怎樣煉成》 中的幾句話吧:
人最寶貴的東西是生命
生命屬于人只有一次
一個人的生命是應(yīng)該這樣度過的
當(dāng)他回首往事的時候
他不會因虛度年華而悔恨
也不會因碌碌無為而羞恥
這樣在臨死的時候
他才能夠說:“我的生命和全部的經(jīng)歷
都獻給世界上最壯麗的事業(yè)——為人類的解放而斗爭”。
(全文完)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
