龙空技术网

如何确定 C 语言内置类型的最大值和最小值

加菲团子 97

前言:

如今兄弟们对“c语言最大值与最小值差”大体比较重视,朋友们都想要了解一些“c语言最大值与最小值差”的相关资讯。那么小编也在网上收集了一些有关“c语言最大值与最小值差””的相关内容,希望你们能喜欢,小伙伴们一起来了解一下吧!

C语言的数据类型长度在不同字长的机器下不是固定值,char至少有8位,shor, int至少有16位, long至少有32位,无符号类型和有符号类型的大小一致

GNU C limits.h

想确定类型的最大值和最小值,可以使用C语言标准规定的 limits.h 里定义的各种类型的取值范围,具体定义如下:

/* 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_ */ /* Get the compiler's limits.h, which defines almost all the ISO constants.    We put this #include_next outside the double inclusion check because    it should be possible to include this file more than once and still get    the definitions from gcc's header.  */#if defined __GNUC__ && !defined _GCC_LIMITS_H_/* `_GCC_LIMITS_H_' is what GCC's file defines.  */# include_next <limits.h>#endif#ifndef _LIBC_LIMITS_H_#define _LIBC_LIMITS_H_	1/* 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

标签: #c语言最大值与最小值差 #c语音求最大值最小值 #c语言找出最大值和最小值 #c语言中如何取最大值和最小值 #c语言中最大值怎么表示