В какой библиотеке содержится uint32 t

от admin

Заголовочный файл cstdint (stdint.h)

Заголовочный файл cstdint описывает целочисленные типы данных с установленными диапазонами представления чисел. Вместе с типами данных, в этом файле определены макросы с указанием верхних и нижних границ представляемых значений и макро-функции для формирования диапазонов представляемых значений для каждого типа данных. Ниже, в таблице, приводятся целочисленные типы данных и их описание.

Целочисленные типы данных

Целые типы данных имеют размеры 8, 16, 32 и 64 бит. Никаких других целочисленных типов данных с меньшим размером в С++ не существует. Ниже приведена таблица целочисленных типов данных, с константами и описанием.

Некоторые из этих типов данных могут обозначать одни и те же типы данных. Таким образом, перегруженные функции не должны полагаться на эти типы данных.

Обратите внимание, что некоторые типы данных не являются обязательными (и, следовательно, не могут быть переносимыми). Некоторые реализации библиотеки могут также определить дополнительные типы данных, с другой шириной диапазонов для совместимости со своей системой.

Ниже показаны ограничения типов данных заголовочного файла cstdint .

Макросы

Макрос Описание Диапазон
INTMAX_MIN Минимальное значение типа данных intmax_t -(2 63 ), или меньше
INTMAX_MAX Максимальное значение типа данных intmax_t 2 63 -1, или больше
UINTMAX_MAX Максимальное значение типа данных uintmax_t 2 64 -1, или больше
INTN_MIN Минимальное значение знакового типа данных N (8, 16, 32, 64) -2 (N-1)
INTN_MAX Максимальное значение знакового типа данных N (8, 16, 32, 64) 2 (N-1) -1
UINTN_MAX Максимальное значение беззнакового типа данных N (8, 16, 32, 64) 2 N -1
INT_LEASTN_MIN Минимальное значение знакового типа данных N, с минимальной шириной диапазона. -(2 (N-1) -1), или меньше
INT_LEASTN_MAX Максимальное значение знакового типа данных N, с минимальной шириной диапазона. 2 (N-1) -1, или больше
UINT_LEASTN_MAX Максимальное значение беззнакового типа данных N, с минимальной шириной диапазона. 2 N -1, или больше
INT_FASTN_MIN Минимальное значение быстрого знакового типа данных. -(2 (N-1) -1), или меньше
INT_FASTN_MAX Максимальное значение быстрого знакового типа данных. 2 (N-1) -1, или больше
UINT_FASTN_MAX Максимальное значение быстрого беззнакового типа данных. 2 N -1, или больше
INTPTR_MIN Минимальное значение intptr_t . -(2 15 -1), или меньше
INTPTR_MAX Максимальное значение intptr_t . 2 15 -1, или больше
UINTPTR_MAX Максимальное значение uintptr_t . 2 16 -1, или больше

Вместо N подставляйте количество битов — 8, 16, 32, 64.

Ограничение остальных целочисленных типов данных, показаны ниже.

Макросы границ целочисленных типов данных

Макросы Описание Ширина диапазона
SIZE_MAX Максимальное значение типа данных size_t 2 64 -1, или больше
PTRDIFF_MIN Минимальное значение типа данных ptrdiff_t -(2 16 -1), или меньше
PTRDIFF_MAX Максимальное значение типа данных ptrdiff_t 2 16 -1, или больше
SIG_ATOMIC_MIN Минимальное значение типа данных sig_atomic_t если sig_atomic_t знаковый: -127, или больше. если sig_atomic_t беззнаковый: 0
SIG_ATOMIC_MAX Максимальное значение типа данных sig_atomic_t если sig_atomic_t знаковый: 127, или больше если sig_atomic_t беззнаковый: 255, или меньше
WCHAR_MIN Минимальное значение типа данных wchar_t если wchar_t знаковый: -127, или меньше если wchar_t беззнаковый: 0
WCHAR_MAX Максимальное значение типа данных wchar_t если wchar_t знаковый: 127, или больше если wchar_t беззнаковый: 255, или больше
WINT_MIN Минимальное значение типа данных wint_t если wint_t знаковый: -32767, или меньше если wchar_t беззнаковый: 0
WINT_MAX Максимальное значение типа данных wint_t если wint_t знаковый: 32767, или больше если wchar_t беззнаковый: 65535, или больше

Макро-функции

Эти функции преобразовывают передаваемые им значения в целочисленные константные выражения:

<cstdint> (stdint.h)

The following are typedefs of fundamental integral types or extended integral types.

signed type unsigned type description
intmax_t uintmax_t Integer type with the maximum width supported.
int8_t uint8_t Integer type with a width of exactly 8, 16, 32, or 64 bits.
For signed types, negative values are represented using 2’s complement.
No padding bits.
Optional: These typedefs are not defined if no types with such characteristics exist.*
int16_t uint16_t
int32_t uint32_t
int64_t uint64_t
int_least8_t uint_least8_t Integer type with a minimum of 8, 16, 32, or 64 bits.
No other integer type exists with lesser size and at least the specified width.
int_least16_t uint_least16_t
int_least32_t uint_least32_t
int_least64_t uint_least64_t
int_fast8_t uint_fast8_t Integer type with a minimum of 8, 16, 32, or 64 bits.
At least as fast as any other integer type with at least the specified width.
int_fast16_t uint_fast16_t
int_fast32_t uint_fast32_t
int_fast64_t uint_fast64_t
intptr_t uintptr_t Integer type capable of holding a value converted from a void pointer and then be converted back to that type with a value that compares equal to the original pointer.
Optional: These typedefs may not be defined in some library implementations.*

Some of these typedefs may denote the same types. Therefore, function overloads should not rely on these being different.

* Notice that some types are optional (and thus, with no portability guarantees). A particular library implementation may also define additional types with other widths supported by its system. In any case, if either the signed or the unsigned version is defined, both the signed and unsigned versions are defined.

Macros

Limits of cstdint types
Macro description defined as
INTMAX_MIN Minimum value of intmax_t -(2 63 -1), or lower
INTMAX_MAX Maximum value of intmax_t 2 63 -1, or higher
UINTMAX_MAX Maximum value of uintmax_t 2 64 -1, or higher
INT N _MIN Minimum value of exact-width signed type Exactly -2 (N-1)
INT N _MAX Maximum value of exact-width signed type Exactly 2 (N-1) -1
UINT N _MAX Maximum value of exact-width unsigned type Exactly 2 N -1
INT_LEAST N _MIN Minimum value of minimum-width signed type -(2 (N-1) -1), or lower
INT_LEAST N _MAX Maximum value of minimum-width signed type 2 (N-1) -1, or higher
UINT_LEAST N _MAX Maximum value of minimum-width unsigned type 2 N -1, or higher
INT_FAST N _MIN Minimum value of fastest minimum-width signed type -(2 (N-1) -1), or lower
INT_FAST N _MAX Maximum value of fastest minimum-width signed type 2 (N-1) -1, or higher
UINT_FAST N _MAX Maximum value of fastest minimum-width unsigned type 2 N -1, or higher
INTPTR_MIN Minimum value of intptr_t -(2 15 -1), or lower
INTPTR_MAX Maximum value of intptr_t 2 15 -1, or higher
UINTPTR_MAX Maximum value of uintptr_t 2 16 -1, or higher

Where N is one in 8, 16, 32, 64, or any other type width supported by the library.

Only the macros corresponding to types supported by the library are defined.

Are types like uint32, int32, uint64, int64 defined in any stdlib header?

I often see source code using types like uint32, uint64 and I wonder if they should be defined by the programmer in the application code or if they are defined in a standard lib header.

What’s the best way to have these types on my application source code?

4 Answers 4

The C99 stdint.h defines these:

  • int8_t
  • int16_t
  • int32_t
  • uint8_t
  • uint16_t
  • uint32_t

And, if the architecture supports them:

  • int64_t
  • uint64_t

There are various other integer typedefs in stdint.h as well.

If you’re stuck without a C99 environment then you should probably supply your own typedefs and use the C99 ones anyway.

C Programming/stdint.h

stdint.h is a header file in the C standard library introduced in the C99 standard library section 7.18 to allow programmers to write more portable code by providing a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type, using macros [1] . This header is particularly useful for embedded programming which often involves considerable manipulation of hardware specific I/O registers requiring integer data of fixed widths, specific locations and exact alignments. stdint.h (for C or C++), and cstdint (for C++) can be downloaded or quickly created if they are not provided.

The naming convention for exact-width integer types is intN_t for signed int and uintN_t for unsigned int [1] . For example int8_t and uint64_t amongst others could be declared together with defining their corresponding ranges INT8_MIN to INT8_MAX and 0 (zero) to UINT64_MAX ; again using a similar but upper case naming convention. In addition stdint.h defines limits of integer types capable of holding object pointers such as UINTPTR_MAX , the value of which depends on the processor and its address range [1] .

The exact-width types and their corresponding ranges are only included in that header if they exist for that specific compiler/processor. Note that even on the same processor, two different compiler implementations can differ. The use of #if or #ifdef would allow the inclusion or exclusion of types by the use of compilers preprocessor so that the correct exact-width set is selected for a compiler and its processor target.

The related include file <limits.h> provides macros values for the range limits of common integer variable types. In C <limits.h> is already included in <stdint.h> , but in contrast to <stdint.h> which is implementation independent; all maximum and minimum integer values defined in <limits.h> are compiler implementation specific. For example a compiler generating 32 bit executables will define LONG_MIN as −2,147,483,648 [−2 31 ] however for 64 bit processors targets, LONG_MIN can be −9,223,372,036,854,775,808 [−2 63 ].

Contents

Background [ edit | edit source ]

Corresponding integer types [ edit | edit source ]

The C standard has a notion of «corresponding integer types». Informally, what this means is for any integer type T:

the type A and the type B are said to be corresponding integer types (note: typedef doesn’t create a new type, it creates a new identifier as a synonym for the given type). This is important for two reasons:

  • corresponding types are friendly to aliasing and type puns
  • corresponding types have a similar object representation

Both of these combined require code like:

to have defined behavior by the standard (as opposed to being undefined in the general case). There are many caveats to how far you can push this, so it’s important to actually read the C standard to see what’s legal or not (the bulk of this has to deal with padding bits and out of range representations).

Representation [ edit | edit source ]

The C99 standard elaborated the difference between value representations and object representations.

The object representation of an integer consists of 0 or more padding bits, 1 or more value bits [1] , and either 0 or 1 sign bits (this doesn’t count as a value bit) depending on the signedness of the integer type.

Читать:
Как снять монитор с подставки

The value representation is a conceptual representation of an integer. The value representation ignores any padding bits and does a (possible) rearrangement to the bits so that the integer is ordered sequentially from most significant value bit to least significant value bit. Most programmers deal with this representation because it allows them easily to write portable code by only dealing with −0 and out of range values as opposed to both of those in addition to tricky aliasing rules and trap representations if they choose to deal with the object representation directly.

Signed representation [ edit | edit source ]

The C standard allows for only three signed integer representations specified by the compiler writer:

  • sign and magnitude
  • one’s complement
  • two’s complement (the most widely used)

Integer types [ edit | edit source ]

The types <something>_t and u<something>_t are required to be corresponding signed and unsigned integer types. For the types that are marked optional, an implementation must either define both <something>_t and u<something>_t or neither of the two. The limits of these types shall be defined with macros with a similar name in the same fashion as described below.

If a type is of the form [u]<something>N_t (or similarly for a preprocessor define), N must be a positive decimal integer with no leading 0’s.

Exact-width integer types [ edit | edit source ]

These are of the form intN_t and uintN_t . Both types must be represented by exactly N bits with no padding bits. intN_t must be encoded as a two’s complement signed integer and uintN_t as an unsigned integer. These types are optional unless the implementation supports types with widths of 8, 16, 32 or 64, then it shall typedef them to the corresponding types with corresponding N. Any other N is optional [1] .

Specific integral type limits

Specifier Signing Bits Bytes Minimum Value Maximum Value
int8_t Signed 8 1 −2 7 which equals −128 2 7 − 1 which is equal to 127
uint8_t Unsigned 8 1 0 2 8 − 1 which equals 255
int16_t Signed 16 2 −2 15 which equals −32,768 2 15 − 1 which equals 32,767
uint16_t Unsigned 16 2 0 2 16 − 1 which equals 65,535
int32_t Signed 32 4 −2 31 which equals −2,147,483,648 2 31 − 1 which equals 2,147,483,647
uint32_t Unsigned 32 4 0 2 32 − 1 which equals 4,294,967,295
int64_t Signed 64 8 −2 63 which equals −9,223,372,036,854,775,808 2 63 − 1 which equals 9,223,372,036,854,775,807
uint64_t Unsigned 64 8 0 2 64 − 1 which equals 18,446,744,073,709,551,615

The limits of these types are defined with macros with the following formats:

  • INTN_MAX is the maximum value (2 N−1 − 1) of the signed version of intN_t.
  • INTN_MIN is the minimum value (−2 N−1 ) of the signed version of intN_t.
  • UINTN_MAX is the maximum value (2 N – 1) of the unsigned version of uintN_t.

Minimum-width integer types [ edit | edit source ]

These are of the form int_leastN_t and uint_leastN_t . int_leastN_t is a signed integer and uint_leastN_t is an unsigned integer [1] .

The standard mandates that these have widths greater than or equal to N, and that no smaller type with the same signedness has N or more bits. For example, if a system provided only a uint32_t and uint64_t , uint_least16_t must be equivalent to uint32_t .

An implementation is required to define these for the following N: 8, 16, 32, 64. Any other N is optional.

The limits of these types are defined with macros with the following formats:

  • INT_LEASTN_MAX is the maximum value (2 N−1 − 1 or greater) of the signed version of int_leastN_t .
  • INT_LEASTN_MIN is the minimum value (−2 N−1 + 1 or less) of the signed version of int_leastN_t .
  • UINT_LEASTN_MAX is the maximum value (2 N − 1 or greater) of the unsigned version of uint_leastN_t .

stdint.h should also define macros which will convert constant decimal, octal or hexadecimal value which are guaranteed to be suitable for the corresponding types and to be usable with the #if :

  • INTN_C(value) is substituted for a value suitable for int_leastN_t . For example if int_least64_t is «typedefed» to signed long long int , INT64_C(123) corresponds to 123LL .
  • UINTN_C(value) is substituted for a value suitable for uint_leastN_t .

Fastest minimum-width integer types [ edit | edit source ]

These are of the form int_fastN_t and uint_fastN_t .

The standard does not mandate anything about these types except that their widths must be greater than or equal to N. It also leaves it up to the implementer to decide what it means to be a «fast» integer type.

An implementation is required to define these for the following N: 8, 16, 32, 64 [2] .

The limits of these types are defined with macros with the following formats:

  • INT_FASTN_MAX is the maximum value (2 N−1 − 1 or greater) of the signed version of int_fastN_t .
  • INT_FASTN_MIN is the minimum value (−2 N−1 + 1 or less) of the signed version of int_fastN_t .
  • UINT_FASTN_MAX is the maximum value (2 N − 1 or greater) of the unsigned version of uint_fastN_t [1] .

Integers wide enough to hold pointers [ edit | edit source ]

intptr_t and uintptr_t are, respectively, signed and unsigned integers which are guaranteed to be able to hold the value of a pointer. These two types are optional.

The limits of these types are defined with the following macros:

  • INTPTR_MIN is the minimum value (−32,767 [−2 15 + 1] or less) of intptr_t .
  • INTPTR_MAX is the maximum value (32,767 [2 15 − 1] or greater) of intptr_t .
  • UINTPTR_MAX is the maximum value (65,535 [2 16 − 1] or greater) of uintptr_t [3] .

Greatest-width integer types [ edit | edit source ]

intmax_t and uintmax_t is a signed and unsigned integer which are of the greatest supported width. They are, in other words, the integer types which have the greatest limits.

The limits of these types are defined with macros with the following formats:

  • INTMAX_MAX is the maximum value (9,223,372,036,854,775,807 [2 63 − 1] or greater) of the signed version of intmax_t .
  • INTMAX_MIN is the minimum value (−9,223,372,036,854,775,807 [−2 63 + 1] or less) of the signed version of intmax_t .
  • UINTMAX_MAX is the maximum value (18,446,744,073,709,551,615 [2 64 − 1] or greater) of the unsigned version of uintmax_t .

Macros which will convert constant decimal, octal or hexadecimal value which will suit the corresponding type are also defined:

  • INTMAX_C(value) is substituted for a value suitable for intmax_t .
  • UINTMAX_C(value) is substituted for a value suitable for uintmax_t [1] .

Other integer limits [ edit | edit source ]

  • PTRDIFF_MIN is the minimum value of ptrdiff_t .
  • PTRDIFF_MAX is the maximum value of ptrdiff_t .
  • SIZE_MAX is the maximum value (2 16 − 1 or greater) of size_t .
  • WCHAR_MIN is the minimum value of wchar_t .
  • WCHAR_MAX is the maximum value of wchar_t .
  • WINT_MIN is the minimum value of wint_t .
  • WINT_MAX is the maximum value of wint_t .
  • SIG_ATOMIC_MIN is the minimum value of sig_atomic_t .
  • SIG_ATOMIC_MAX is the maximum value of sig_atomic_t .

Criticisms and caveats [ edit | edit source ]

  • Some (non-conforming) implementations tack C99 support on top of a C89 runtime library. [citation needed] One of the consequences of this is that the new printf and scanf specifiers aren’t recognized and will probably lead to something undefined. The typical ways of working around this are:
    • The most common (and the most wrong) way is to use the long or unsigned long types as an intermediate step and pass these types into printf or scanf . This works reasonably well for the exact, minimum, and fast integer types less than 32-bits but may cause trouble with ptrdiff_t and size_t and the types larger than 32-bits, typically on platforms that use 32-bit long s and 64-bit pointers.
    • Not using scanf directly but manually reading in a buffer, calling strto[i|u]max , and then converting it to the desired type. This doesn’t help with printing out integers though.
    • Using a 3rd-party printf and scanf library that is C99 compatible.
    • Using the C99 standard printing format specifiers. PRId64 for example. These are declared in inttypes.h.
    • The rules for integer rank and corresponding integer types may force implementers to choose the lesser of two evils in not supporting an integer type, making a bad compromise, or supporting an integer type a non-conforming way.
      • For example, there are machines that either have special support for an extremely large signed integer register or an extremely large unsigned integer register without supporting the other type. [citation needed] An implementation can either choose not to expose this to a C implementation, synthesize a slow type as the corresponding integer type, synthesizing a weird corresponding integer type, or expose the integer to the programmer without setting it to the [u]intmax_t types or synthesizing a corresponding integer type.
      • The [u]intN_t types are a compromise between the desire to have guaranteed two’s complement integer types and the desire to have guaranteed types with no padding bits (as opposed to a more fine grained approach which would define more types). Because of the «all or nothing» approach to the [u]intN_t types, an implementation might have to play the same sort of games described above depending on whether they care about speed, programmer convenience, or standards conformance.

      See also [ edit | edit source ]

      Notes and references [ edit | edit source ]

      1. abcdefgh technically, it actually allows 0 or more value bits, but the only way you can construct this is with a single-bit bit-field of a signed integer
      2. ↑http://www.tuxgraphics.org/common/src2/article09043/avr-libc-user-manual-1.6.4/group__avr__stdint.html
      3. ↑http://linux.die.net/man/3/intptr_t
      • stdint.h : integer types – Base Definitions Reference, The Single UNIX® Specification, Issue 7 from The Open Group

      External links [ edit | edit source ]

      As stdint.h is not shipped with older C++ compilers and Visual Studio C++ products prior to Visual Studio 2010, third-party implementations are available:

Похожие статьи