c++中limits头文件

limits.h专门用于检测整型数据数据类型的表达值范围。

基本释义

编辑
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。

你可能感兴趣的:(c++中limits头文件)