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
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.
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:
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:
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
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:
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.
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
Note that the macros %are defined WITHOUT the percent sign. This makes it possible to add additional formatting characters:
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.
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
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.
Furthermore, certain implementations define additional macros, which, however, introduce less new functionality than simply assigning new symbols to the existing ones.