c_cpp C中整数类型的大小和范围

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C中整数类型的大小和范围相关的知识,希望对你有一定的参考价值。

#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

int main()
{
	printf("%19s: %3zu bits, %20d - %20d\n",     "bool",                sizeof(_Bool)               * CHAR_BIT,           0,           1);
	printf("%19s: %3zu bits, %20d - %20d\n",     "char",                sizeof(         char)       * CHAR_BIT,    CHAR_MIN,    CHAR_MAX);
	printf("%19s: %3zu bits, %20d - %20d\n",     "signed char",         sizeof(  signed char)       * CHAR_BIT,   SCHAR_MIN,   SCHAR_MAX);
	printf("%19s: %3zu bits, %20u - %20u\n",     "unsigned char",       sizeof(unsigned char)       * CHAR_BIT,           0,   UCHAR_MAX);
	printf("%19s: %3zu bits, %20d - %20d\n",     "short",               sizeof(         short)      * CHAR_BIT,    SHRT_MIN,    SHRT_MAX);
	printf("%19s: %3zu bits, %20u - %20u\n",     "unsigned short",      sizeof(unsigned short)      * CHAR_BIT,           0,    SHRT_MAX);
	printf("%19s: %3zu bits, %20d - %20d\n",     "int",                 sizeof(         int)        * CHAR_BIT,     INT_MIN,     INT_MAX);
	printf("%19s: %3zu bits, %20u - %20u\n",     "unsigned int",        sizeof(unsigned int)        * CHAR_BIT,           0,    UINT_MAX);
	printf("%19s: %3zu bits, %20ld - %20ld\n",   "long",                sizeof(         long)       * CHAR_BIT,    LONG_MIN,    LONG_MAX);
	printf("%19s: %3zu bits, %20lu - %20lu\n",   "unsigned long",       sizeof(unsigned long)       * CHAR_BIT,         0ul,   ULONG_MAX);
	printf("%19s: %3zu bits, %20lld - %20lld\n", "long long",           sizeof(         long long)  * CHAR_BIT,   LLONG_MIN,   LLONG_MAX);
	printf("%19s: %3zu bits, %20llu - %20llu\n", "unsigned long long",  sizeof(unsigned long long)  * CHAR_BIT,        0ull,  ULLONG_MAX);
	printf("\n");
	printf("%19s: %3zu bits, %20zu - %20zu\n",   "size_t",              sizeof(size_t)              * CHAR_BIT,  (size_t) 0,    SIZE_MAX);
	printf("%19s: %3zu bits, %20td - %20td\n",   "ptrdiff_t",           sizeof(ptrdiff_t)           * CHAR_BIT, PTRDIFF_MIN, PTRDIFF_MAX);
	printf("\n");
#ifdef INT8_MAX
	printf("%19s: %3zu bits, %20" PRId8  " - %20" PRId8  "\n", "int8_t",   sizeof(  int8_t) * CHAR_BIT,     INT8_MIN,   INT8_MAX);
#endif
#ifdef UINT8_MAX
	printf("%19s: %3zu bits, %20" PRIu8  " - %20" PRIu8  "\n", "uint8_t",  sizeof( uint8_t) * CHAR_BIT,  (uint8_t) 0,  UINT8_MAX);
#endif
#ifdef INT16_MAX
	printf("%19s: %3zu bits, %20" PRId16 " - %20" PRId16 "\n", "int16_t",  sizeof( int16_t) * CHAR_BIT,    INT16_MIN,  INT16_MAX);
#endif
#ifdef UINT16_MAX
	printf("%19s: %3zu bits, %20" PRIu16 " - %20" PRIu16 "\n", "uint16_t", sizeof(uint16_t) * CHAR_BIT, (uint16_t) 0, UINT16_MAX);
#endif
#ifdef INT32_MAX
	printf("%19s: %3zu bits, %20" PRId32 " - %20" PRId32 "\n", "int32_t",  sizeof( int32_t) * CHAR_BIT,    INT32_MIN,  INT32_MAX);
#endif
#ifdef UINT32_MAX
	printf("%19s: %3zu bits, %20" PRIu32 " - %20" PRIu32 "\n", "uint32_t", sizeof(uint32_t) * CHAR_BIT, (uint32_t) 0, UINT32_MAX);
#endif
#ifdef INT64_MAX
	printf("%19s: %3zu bits, %20" PRId64 " - %20" PRId64 "\n", "int64_t",  sizeof( int64_t) * CHAR_BIT,    INT64_MIN,  INT64_MAX);
#endif
#ifdef UINT64_MAX
	printf("%19s: %3zu bits, %20" PRIu64 " - %20" PRIu64 "\n", "uint64_t", sizeof(uint64_t) * CHAR_BIT, (uint64_t) 0, UINT64_MAX);
#endif
#ifdef INT128_MAX
	printf("%19s: %3zu bits, %20" PRId128 " - %20" PRId128 "\n", "int128_t",  sizeof( int128_t) * CHAR_BIT,    INT128_MIN,  INT128_MAX);
#endif
#ifdef UINT128_MAX
	printf("%19s: %3zu bits, %20" PRIu128 " - %20" PRIu128 "\n", "uint128_t", sizeof(uint128_t) * CHAR_BIT, (uint128_t) 0, UINT128_MAX);
#endif
	printf("\n");
	printf("%19s: %3zu bits, %20" PRIdLEAST8  " - %20" PRIdLEAST8  "\n",   "int_least8_t", sizeof(  int_least8_t) * CHAR_BIT,     INT_LEAST8_MIN,   INT_LEAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIuLEAST8  " - %20" PRIuLEAST8  "\n",  "uint_least8_t", sizeof( uint_least8_t) * CHAR_BIT,  (uint_least8_t) 0,  UINT_LEAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIdLEAST16 " - %20" PRIdLEAST16 "\n",  "int_least16_t", sizeof( int_least16_t) * CHAR_BIT,    INT_LEAST16_MIN,  INT_LEAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIuLEAST16 " - %20" PRIuLEAST16 "\n", "uint_least16_t", sizeof(uint_least16_t) * CHAR_BIT, (uint_least16_t) 0, UINT_LEAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIdLEAST32 " - %20" PRIdLEAST32 "\n",  "int_least32_t", sizeof( int_least32_t) * CHAR_BIT,    INT_LEAST32_MIN,  INT_LEAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIuLEAST32 " - %20" PRIuLEAST32 "\n", "uint_least32_t", sizeof(uint_least32_t) * CHAR_BIT, (uint_least32_t) 0, UINT_LEAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIdLEAST64 " - %20" PRIdLEAST64 "\n",  "int_least64_t", sizeof( int_least64_t) * CHAR_BIT,    INT_LEAST64_MIN,  INT_LEAST64_MAX);
	printf("%19s: %3zu bits, %20" PRIuLEAST64 " - %20" PRIuLEAST64 "\n", "uint_least64_t", sizeof(uint_least64_t) * CHAR_BIT, (uint_least64_t) 0, UINT_LEAST64_MAX);
#ifdef INT_LEAST128_MAX
	printf("%19s: %3zu bits, %20" PRIdLEAST128 " - %20" PRIdLEAST128 "\n",  "int_least128_t", sizeof( int_least128_t) * CHAR_BIT,    INT_LEAST128_MIN,  INT_LEAST128_MAX);
#endif
#ifdef UINT_LEAST128_MAX
	printf("%19s: %3zu bits, %20" PRIuLEAST128 " - %20" PRIuLEAST128 "\n", "uint_least128_t", sizeof(uint_least128_t) * CHAR_BIT, (uint_least128_t) 0, UINT_LEAST128_MAX);
#endif
	printf("\n");
	printf("%19s: %3zu bits, %20" PRIdFAST8  " - %20" PRIdFAST8  "\n",   "int_fast8_t", sizeof(  int_fast8_t) * CHAR_BIT,     INT_FAST8_MIN,   INT_FAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST8  " - %20" PRIuFAST8  "\n",  "uint_fast8_t", sizeof( uint_fast8_t) * CHAR_BIT,  (uint_fast8_t) 0,  UINT_FAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST16 " - %20" PRIdFAST16 "\n",  "int_fast16_t", sizeof( int_fast16_t) * CHAR_BIT,    INT_FAST16_MIN,  INT_FAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST16 " - %20" PRIuFAST16 "\n", "uint_fast16_t", sizeof(uint_fast16_t) * CHAR_BIT, (uint_fast16_t) 0, UINT_FAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST32 " - %20" PRIdFAST32 "\n",  "int_fast32_t", sizeof( int_fast32_t) * CHAR_BIT,    INT_FAST32_MIN,  INT_FAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST32 " - %20" PRIuFAST32 "\n", "uint_fast32_t", sizeof(uint_fast32_t) * CHAR_BIT, (uint_fast32_t) 0, UINT_FAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST64 " - %20" PRIdFAST64 "\n",  "int_fast64_t", sizeof( int_fast64_t) * CHAR_BIT,    INT_FAST64_MIN,  INT_FAST64_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST64 " - %20" PRIuFAST64 "\n", "uint_fast64_t", sizeof(uint_fast64_t) * CHAR_BIT, (uint_fast64_t) 0, UINT_FAST64_MAX);
#ifdef INT_FAST128_MAX
	printf("%19s: %3zu bits, %20" PRIdFAST128 " - %20" PRIdFAST128 "\n",  "int_fast128_t", sizeof( int_fast128_t) * CHAR_BIT,    INT_FAST128_MIN,  INT_FAST128_MAX);
#endif
#ifdef UINT_FAST128_MAX
	printf("%19s: %3zu bits, %20" PRIuFAST128 " - %20" PRIuFAST128 "\n", "uint_fast128_t", sizeof(uint_fast128_t) * CHAR_BIT, (uint_fast128_t) 0, UINT_FAST128_MAX);
#endif
	printf("\n");
	printf("%19s: %3zu bits, %20" PRIdFAST8  " - %20" PRIdFAST8  "\n",   "int_fast8_t", sizeof(  int_fast8_t) * CHAR_BIT,     INT_FAST8_MIN,   INT_FAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST8  " - %20" PRIuFAST8  "\n",  "uint_fast8_t", sizeof( uint_fast8_t) * CHAR_BIT,  (uint_fast8_t) 0,  UINT_FAST8_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST16 " - %20" PRIdFAST16 "\n",  "int_fast16_t", sizeof( int_fast16_t) * CHAR_BIT,    INT_FAST16_MIN,  INT_FAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST16 " - %20" PRIuFAST16 "\n", "uint_fast16_t", sizeof(uint_fast16_t) * CHAR_BIT, (uint_fast16_t) 0, UINT_FAST16_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST32 " - %20" PRIdFAST32 "\n",  "int_fast32_t", sizeof( int_fast32_t) * CHAR_BIT,    INT_FAST32_MIN,  INT_FAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST32 " - %20" PRIuFAST32 "\n", "uint_fast32_t", sizeof(uint_fast32_t) * CHAR_BIT, (uint_fast32_t) 0, UINT_FAST32_MAX);
	printf("%19s: %3zu bits, %20" PRIdFAST64 " - %20" PRIdFAST64 "\n",  "int_fast64_t", sizeof( int_fast64_t) * CHAR_BIT,    INT_FAST64_MIN,  INT_FAST64_MAX);
	printf("%19s: %3zu bits, %20" PRIuFAST64 " - %20" PRIuFAST64 "\n", "uint_fast64_t", sizeof(uint_fast64_t) * CHAR_BIT, (uint_fast64_t) 0, UINT_FAST64_MAX);
#ifdef INT_FAST128_MAX
	printf("%19s: %3zu bits, %20" PRIdFAST128 " - %20" PRIdFAST128 "\n",  "int_fast128_t", sizeof( int_fast128_t) * CHAR_BIT,    INT_FAST128_MIN,  INT_FAST128_MAX);
#endif
#ifdef UINT_FAST128_MAX
	printf("%19s: %3zu bits, %20" PRIuFAST128 " - %20" PRIuFAST128 "\n", "uint_fast128_t", sizeof(uint_fast128_t) * CHAR_BIT, (uint_fast128_t) 0, UINT_FAST128_MAX);
#endif
	printf("\n");
#ifdef INTPTR_MAX
	printf("%19s: %3zu bits, %20" PRIdPTR " - %20" PRIdPTR "\n",  "intptr_t", sizeof( intptr_t) * CHAR_BIT,    INTPTR_MIN,  INTPTR_MAX);
#endif
#ifdef UINTPTR_MAX
	printf("%19s: %3zu bits, %20" PRIuPTR " - %20" PRIuPTR "\n", "uintptr_t", sizeof(uintptr_t) * CHAR_BIT, (uintptr_t) 0, UINTPTR_MAX);
#endif
	printf("%19s: %3zu bits, %20" PRIdMAX " - %20" PRIdMAX "\n",  "intmax_t", sizeof( intmax_t) * CHAR_BIT,    INTMAX_MIN,  INTMAX_MAX);
	printf("%19s: %3zu bits, %20" PRIuMAX " - %20" PRIuMAX "\n", "uintmax_t", sizeof(uintmax_t) * CHAR_BIT, (uintmax_t) 0, UINTMAX_MAX);
}

以上是关于c_cpp C中整数类型的大小和范围的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 这个简单的代码片段显示了如何使用有符号整数在C中完成插值。 for()循环确定要插入的范围

c_cpp [bs] [array]搜索范围。给定排序的整数数组,找到给定目标值的起始位置和结束位置。 O(log n)

c_cpp 类型擦除的C ++范围

c语言中长整型和整型的范围

c 中的数据类型

long long 在c语言中是啥类型