stdint.h, inttypes.h, limits.h

This page was translated by a robot.

An integer is defined in the C and C++ languages ​​as a related set of bits that encode an integer . Depending on the application, the integer is also to be interpreted as signed or unsigned. The number of bits required for coding defines the range of values ​​of the type.

The libraries stdintand inttypeswere introduced with the C99 standard, which allow precise definition of the required bits.

Integer Types with a Specific Number of Bits

C
C++
#include <stdint.h>
#include <cstdint>

The stdintlibrary provides integer types that have a different number of bits and are either signed or unsigned. Depending on the area of ​​application, a programmer wants the desired number of bits to be adhered to exactly, it is only a minimum requirement, or that it corresponds to the type that can be processed fastest by the processor.

intN_t          signed-Integer with exactly N bits.
uintN_t         unsigned-Integer with exactly N bits.
int_leastN_t    signed-Integer with at least N bits.
uint_leastN_t   unsigned-Integer with at least N bits.
int_fastN_t     signed-Integer with at least N bits and fastest processing.
uint_fastN_t    unsigned-Integer with at least N bits and fastest processing.

The N stands for the desired number of bits. Every compiler according to the C99 standard must provide at least the integer types with 8, 16, 32 and 64 bits. However, a compiler is free to provide other types, for example with 24 bits..

For example, a 32-bit integer can be declared in several ways, as follows:

#include <stdint.h>

int main (void)
{
  int32_t        myint1;
  uint32_t       myint2;
  int_least32_t  myint3;
  uint_least32_t myint4;
  int_fast32_t   myint5;
  uint_fast32_t  myint6;
  return 0;
}

Caution: The above information must be treated with caution on certain systems. The types with an exact number of bits, for example, only exist if the system is capable of storing them without padding bits at all and the signedvariant is also encoded with the two's complement . It is therefore only guaranteed for the exact types that the signed numbers are encoded in two's complement. In modern systems, however, it can generally be assumed that this is the case.

The author generally recommends using these types instead of the traditional type modifiers (see below). On modern systems and with modern compilers, the above types are all defined and lead to platform-independent code.

stdintOther types are defined in the library. However, these are not (for the time being) listed on this page.

Minimum and Maximum Values (Limits)

For all the types that are defined according to the list above, various macros are offered at the same time in order to query the available minimum and maximum values:

INTN_MIN            Minimum value of type intN_t
INTN_MAX            Maximum value of type intN_t
UINTN_MAX           Maximum value of type uintN_t
INT_LEASTN_MIN      Minimum value of type int_leastN_t
INT_LEASTN_MAX      Maximum value of type int_leastN_t
UINT_LEASTN_MAX     Maximum value of type uint_leastN_t
INT_FASTN_MIN       Minimum value of type int_fastN_t
INT_FASTN_MAX       Maximum value of type int_fastN_t
UINT_FASTN_MAX      Maximum value of type uint_fastN_t

Again, N stands for the number of bits of the corresponding type. The macro for the minimum value of a unsignedtype is missing since it is always 0defined as (null)..

The values ​​of the exact types are listed below for illustrative purposes. These values ​​should be useful for most users:





-128
 127
 255
-32768
 32767
 65535
-2147483648
 2147483647
 4294967295
-9223372036854775808
 9223372036854775807
 18446744073709551615
#include <stdio.h>
#include <stdint.h>

int main(){
  printf("%i\n",    INT8_MIN);
  printf(" %i\n",   INT8_MAX);
  printf(" %u\n",   UINT8_MAX);
  printf("%i\n",    INT16_MIN);
  printf(" %i\n",   INT16_MAX);
  printf(" %u\n",   UINT16_MAX);
  printf("%i\n",    INT32_MIN);
  printf(" %i\n",   INT32_MAX);
  printf(" %u\n",   UINT32_MAX);
  printf("%lli\n",  INT64_MIN);
  printf(" %lli\n", INT64_MAX);
  printf(" %llu\n", UINT64_MAX);
  return 0;
}

These minimum and maximum values ​​are also referred to as limits because they could be queried in the traditional way through the limitslibrary. See the section below for more information.

The stdintlibrary defines a few more macros, which are not (for the time being) listed here on this page.

Makros for printf and scanf

For input and output using printfand scanf, formatting characters (e.g. "%hhd") must be used to specify how a passed argument is to be interpreted. With the introduction of the new integer types in the stdintlibrary, a corresponding macro was defined for each new type, which defines the corresponding formatting characters. All of these macros are available to the programmer when the following library is included:

C
C++
#include <inttypes.h>
#include <cinttypes>

The macros for printfbegin with PRIand the macros for scanfbegin with SCN. The other information relates to the input and output format and the type of argument.

PRIiN   PRIiLEASTN   PRIiFASTN   integer
PRIdN   PRIdLEASTN   PRIdFASTN   signed decimal number
PRIuN   PRIuLEASTN   PRIuFASTN   unsigned decimal number
PRIoN   PRIoLEASTN   PRIoFASTN   octal number
PRIxN   PRIxLEASTN   PRIxFASTN   Lower case hexadecimal number
PRIXN   PRIXLEASTN   PRIXFASTN   Upper case hexadecimal number

SCNiN   SCNiLEASTN   SCNiFASTN   integer
SCNdN   SCNdLEASTN   SCNdFASTN   signed decimal number
SCNuN   SCNuLEASTN   SCNuFASTN   unsigned decimal number
SCNoN   SCNoLEASTN   SCNoFASTN   octal number
SCNxN   SCNxLEASTN   SCNxFASTN   Lower case hexadecimal number
SCNXN   SCNXLEASTN   SCNXFASTN   Upper case hexadecimal number

The distinction between dand ionly makes scanfsense with . With printfboth specifications have the same effect.

These macros are used by writing them to the desired positions by concatenating the format string:






123
50000
75bcd15
34327724461477777
int8_t         myint1 = 123;
uint_least16_t myint2 = 50000;
int32_t        myint3 = 123456789;
int_fast64_t   myint4 = 999999999999999;

printf("%" PRId8       "\n", myint1);
printf("%" PRIuLEAST16 "\n", myint2);
printf("%" PRIx32      "\n", myint3);
printf("%" PRIoFAST64  "\n", myint4);

Note that the macros %are defined WITHOUT the percent sign. This makes it possible to add additional formatting characters:

123
   123
000123
printf("%"   PRIi32 "\n", 123);
printf("%6"  PRIi32 "\n", 123);
printf("%06" PRIi32 "\n", 123);

The inttypeslibrary defines a few more macros, which are not (for the time being) listed here on this page.

The Traditional Way: The limits Library

charThe basic types , short, int, longand built into the languages ​​C and C++ long longhave a different number of bits depending on the system and can accordingly cover a more or less large range of numbers. The minimum and maximum values ​​of the basic types available in the currently used compiler are limitsdefined as constants in the library.

C
C++
#include <limits.h>
#include <climits>

The author recommends, whenever possible, to limitsstop using the library and to work with the library instead stdint. See above.

The following sample program should suffice for listing the macros:





-128
 127
 255
-32768
 32767
 65535
-2147483648
 2147483647
 4294967295
-2147483648
 2147483647
 4294967295
-9223372036854775808
 9223372036854775807
 18446744073709551615
#include <stdio.h>
#include <limits.h>

int main(){
  printf("%hhi\n",  SCHAR_MIN);
  printf(" %hhi\n", SCHAR_MAX);
  printf(" %hhu\n", UCHAR_MAX);
  printf("%hi\n",   SHRT_MIN);
  printf(" %hi\n",  SHRT_MAX);
  printf(" %hu\n",  USHRT_MAX);
  printf("%i\n",    INT_MIN);
  printf(" %i\n",   INT_MAX);
  printf(" %u\n",   UINT_MAX);
  printf("%i\n",    LONG_MIN);
  printf(" %i\n",   LONG_MAX);
  printf(" %u\n",   ULONG_MAX);
  printf("%lli\n",  LLONG_MIN);
  printf(" %lli\n", LLONG_MAX);
  printf(" %llu\n", ULLONG_MAX);
  return 0;
}

It should be noted that, depending on the system, the output values ​​may differ, or certain macros may not be defined at all. The values ​​given on this page are those that were output on the author's system.

It should also be noted that there are no macros for the minimum value of a unsignedtype. In any case, this is 0(zero).

The limitslibrary also contains a few more macros, but these are hardly relevant for everyday programming. They are listed in the following table along with the author-defined values, but are not discussed further on this page. The interested reader may look up the meanings in the limitslibrary or other sources.

CHAR_BIT        8    Number of bits in a char
MB_LEN_MAX      6    Maximum number of bytes of a multi-byte character
CHAR_MIN     -128    Minimum value of char without signed or unsigned modifier
CHAR_MAX      127    Maximum value of char without signed or unsigned modifier

Furthermore, certain implementations define additional macros, which, however, introduce less new functionality than simply assigning new symbols to the existing ones.

LONG_LONG_MIN     The same as LLONG_MIN
LONG_LONG_MAX     The same as LLONG_MAX
ULONG_LONG_MAX    The same as ULLONG_MAX