官术网_书友最值得收藏!

建議2:防止整數(shù)類型產(chǎn)生回繞與溢出

到C99為止,C語言為我們提供了12個相關(guān)的數(shù)據(jù)類型關(guān)鍵字來表達各種數(shù)據(jù)類型。如表1-2所示,K&R C提供了7個,C89/C90新增了2個,C99新增了3個。

表1-2 C的數(shù)據(jù)類型關(guān)鍵字

整型是C語言最基本的數(shù)據(jù)類型,它以二進制編碼的方式進行存儲,具體可以包括字符、短整型、整型和長整型等。例如,整數(shù)2的二進制表示為10,它在8位與32位的操作系統(tǒng)中存儲方式如圖1-3所示。

圖1-3 整數(shù)2的二進制編碼存儲方式

雖然在計算機中整數(shù)是以二進制編碼方式進行存儲的,但為了便于表達,有時候又會用十六進制編碼方式表示(例如,在32位操作系統(tǒng)下,整數(shù)2的十六進制編碼方式為0x00000002),二進制和十六進制之間能夠很方便地進行轉(zhuǎn)換。

與此同時,整數(shù)類型又可分為有符號(signed)和無符號(unsigned)兩種類型,limits.h文件定義了整型數(shù)據(jù)類型的表達值范圍。在GCC 4.8.3中,limits.h文件定義如下:


/*
 *      ISO C99 Standard: 7.10/5.2.4.2.1 Sizes of integer types <limits.h>
 */
#ifndef _LIBC_LIMITS_H_
#define _LIBC_LIMITS_H_      1
#include <features.h>
/* Maximum length of any multibyte character in any locale.
   We define this value here since the gcc header does not define
   the correct value.  */
#define MB_LEN_MAX 16
/* If we are not using GNU CC we have to define all the symbols ourself.
   Otherwise use gcc's definitions (see below).  */
#if !defined __GNUC__ || __GNUC__ < 2
/* We only protect from multiple inclusion here, because all the other 
   #include's protect themselves, and in GCC 2 we may #include_next through 
   multiple copies of this file before we get to GCC's.  */
# ifndef _LIMITS_H
#  define _LIMITS_H 1
#include <bits/wordsize.h>
/* We don't have #include_next.
   Define ANSI <limits.h> for standard 32-bit words.  */
/* These assume 8-bit `char's, 16-bit `short int's,
   and 32-bit `int's and `long int's.  */
/* Number of bits in a `char'.      */
#  define CHAR_BIT 8
/* Minimum and maximum values a `signed char' can hold.  */
#  define SCHAR_MIN      (-128)
#  define SCHAR_MAX      127
/* Maximum value an `unsigned char' can hold.  (Minimum is 0.)  */
#  define UCHAR_MAX      255
/* Minimum and maximum values a `char' can hold.  */
#  ifdef __CHAR_UNSIGNED__
#   define CHAR_MIN 0
#   define CHAR_MAX      UCHAR_MAX
#  else
#   define CHAR_MIN      SCHAR_MIN
#   define CHAR_MAX      SCHAR_MAX
#  endif
/* Minimum and maximum values a `signed short int' can hold.  */
#  define SHRT_MIN      (-32768)
#  define SHRT_MAX      32767
/* Maximum value an `unsigned short int' can hold.  (Minimum is 0.)  */
#  define USHRT_MAX      65535
/* Minimum and maximum values a `signed int' can hold.  */
#  define INT_MIN      (-INT_MAX - 1)
#  define INT_MAX      2147483647
/* Maximum value an `unsigned int' can hold.  (Minimum is 0.)  */
#  define UINT_MAX      4294967295U
/* Minimum and maximum values a `signed long int' can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX      9223372036854775807L
#  else
#   define LONG_MAX      2147483647L
#  endif
#  define LONG_MIN      (-LONG_MAX - 1L)
/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX      18446744073709551615UL
#  else
#   define ULONG_MAX      4294967295UL
#  endif
#  ifdef __USE_ISOC99
/* Minimum and maximum values a `signed long long int' can hold.  */
#   define LLONG_MAX      9223372036854775807LL
#   define LLONG_MIN      (-LLONG_MAX - 1LL)
/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0.)  */
#   define ULLONG_MAX      18446744073709551615ULL
#  endif /* ISO C99 */
# endif      /* limits.h  */
#endif      /* GCC 2.  */
#endif      /* !_LIBC_LIMITS_H_ */
/* The <limits.h> files in some gcc versions don't define LLONG_MIN,LLONG_MAX,
   and ULLONG_MAX.  Instead only the values gcc defined for ages are   available. */
#if defined __USE_ISOC99 && defined __GNUC__
# ifndef LLONG_MIN
#  define LLONG_MIN      (-LLONG_MAX-1)
# endif
# ifndef LLONG_MAX
#  define LLONG_MAX      __LONG_LONG_MAX__
# endif
# ifndef ULLONG_MAX
#  define ULLONG_MAX      (LLONG_MAX * 2ULL + 1)
# endif
#endif

表1-3描述了以ANSI標(biāo)準(zhǔn)定義的整數(shù)類型。

表1-3 ANSI標(biāo)準(zhǔn)定義的整數(shù)類型

簡單地講,有符號和無符號整數(shù)間的區(qū)別在于怎樣解釋整數(shù)的最高位。如果定義一個有符號整數(shù),則C編譯程序生成的代碼認為該數(shù)最高位是符號標(biāo)志:符號標(biāo)志為0,則該數(shù)為正;符號標(biāo)志為1,則該數(shù)為負

負數(shù)采用2的補碼的形式來表示,即對原碼各位求反(符號位除外),再將求反的結(jié)果加1,最后將符號位設(shè)置為1。例如,在32位操作系統(tǒng)中,有符號整數(shù)-2的存儲方法如下。

第一步:取絕對值2的二進制編碼。

00000000 00000000 00000000 00000010

第二步:求反(符號位除外)。

01111111 11111111 11111111 11111101

第三步:將求反的結(jié)果加1。

01111111 11111111 11111111 11111110

第四步:將符號位設(shè)置為1。

11111111 11111111 11111111 11111110

因此,有符號整數(shù)-2的二進制編碼為11111111 11111111 11111111 11111110,十六進制編碼為0xFFFFFFFE。

最后還需要說明的是,當(dāng)類型修飾符被自身使用時(即它不在基本類型之前時),假定其為int型。也就是說,表1-4的兩種類型是等效的。

表1-4 等效的整數(shù)類型

主站蜘蛛池模板: 新干县| 信阳市| 忻城县| 伊春市| 临沧市| 漯河市| 佛山市| 石渠县| 海淀区| 门源| 张家港市| 宣恩县| 四川省| 鲁山县| 永福县| 浏阳市| 金寨县| 吴忠市| 日土县| 南乐县| 华亭县| 牙克石市| 深州市| 福贡县| 寿宁县| 文化| 罗定市| 冀州市| 五寨县| 阳新县| 巴里| 安远县| 土默特左旗| 格尔木市| 灌阳县| 台州市| 买车| 启东市| 奉节县| 治多县| 家居|