limits.h

limits.h專門用於檢測整型數據數據類型的表達值範圍。

基本介紹

  • 中文名:極限頭檔案
  • 外文名:<limits.h>或<climits>
  • 用途:檢測整型數據數據類型表達值範圍
  • 判斷類型:可以容納的最大值或最小值
  • 性質:檢測表達值
基本釋義,補充內容,

基本釋義

limits.h包含內容(包括注釋)
/*
* 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: Apparently the actual size of PATH_MAX is 260, but a space is
* required for the NUL. TODO: Test?
*/
#define PATH_MAX (259)
/*
* 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
/*
* The GNU C compiler also allows 'long long int'
*/
#if !defined(__STRICT_ANSI__) && defined(__GNUC__)
#define LONG_LONG_MAX 9223370L
#define LONG_LONG_MIN (-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX (2ULL * LONG_LONG_MAX + 1)
/* ISO C9x macro names */
#define LLONG_MAX LONG_LONG_MAX
#define LLONG_MIN LONG_LONG_MIN
#define ULLONG_MAX ULONG_LONG_MAX
/* 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_ */

補充內容

要判斷某種特定類型可以容納的最大值或最小值,一種簡便的方法是使用ANSI標準頭檔案limits.h中的預定義值。該檔案包含一些很有用的常量,它們定義了各種類型所能容納的值,下表列出了這些常量:
常 量 描 述
CHAR_BIT char的二進制位數(bit)
CHAR_MAX char的有符號整數最大值
CHAR_MIN char的有符號整數最小值
MB_LEN_MAX 多位元組字元的最大位元組(byte)數
INT_MAX int的有符號最大值
INT_MIN int的有符號最小值
LONG_MAX long的十進制最大值
LONG_MIN long的十進制最小值
SCHAR_MAX signedchar的十進制整數最大值
SCHAR_MIN signedchar的十進制整數最小值
SHRT_MIN short的十進制最小值
SHRT_MAX short的十進制最大值
UCHAR_MAX unsignedchar的十進制整數最大值
UINT_MAX unsignedint的十進制最大值
ULONG_MAX unsignedlongint的十進制最大值
USHRT_MAX unsignedshortint的十進制最大值
對於整數類型,在使用2的補碼運算的機器(你將使用的機器幾乎都屬此類)上,一個有符號類型可以容納的數字範圍為[- 2^(位數-1) ]到[+ 2^(位數 -1)-1],一個無符號類型可以容納的數字範圍為0到(+ 2^位數 )。例如,一個16位有符號整數可以容納的數字範圍為-2^15(即-32768)到(+2^15-1)(即+32767)。而16為無符號整數可容納的最大值為(2^位數-1)或表示為彙編形式0xffff。

相關詞條

熱門詞條

聯絡我們