Integer types in C/C ++ that are irrelevant to the platform

Source: Internet
Author: User

In C/C ++, the length of an integer is related to the compiler. The implementation of the compiler depends on the CPU. For example, TC ++ is an application under DOS16 and DOS16 is a 16-bit operating system. Therefore, sizeof (int) = 16 in TC ++ is the same as sizeof (int) in win32) = 32.

The C99 standard defines a header file named <stdint. h>, which defines a series of integer types of typedef names. Although the High-Speed C ++ tool has been supporting this header file for some time, it has not been officially included in the C ++ standard. Therefore, before using this header file, you should first read your compiler document to see if it supports this file. In some C ++ compilers, such as GNC, in order to use these new features of the C99 standard. h> This file <stdint. h>.

Fixed Integer type

In the type definition of the header file <stdint. h>, the C99 standard has a set of integer types with fixed characters:

Int8_t int16_t int32_t int64_t

The unsigned integer types with fixed character lengths include:

Uint8_t uint16_t uint32_t uint64_t

Their names are intuitive. For example, int8_t is a signed integer type with a fixed length of 8 bits, while uint8_t is a signed integer type with a fixed length of 8 bits. When you need to ensure that the length of the integer data is fixed on different platforms, you can use these typedef names. A more detailed description of the expected nature is allowed. For example, some types are named int_least8_t, which has at least 8 bits and int32_t, which happens to be 32 bits.

The C99 standard ensures that at least 8-bit, 16-bit, 32-bit, and 64-bit access is allowed. The exact width type is not guaranteed. Do not use this type unless you must be unable to accept a larger type. Another optional type is the new intptr_t type, which is an integer large enough to hold a pointer. Not all systems provide such a type (although all current Linux and BSD implementations do ).

Fast Integer type with the smallest Length

The header file also defines another set of typedef names, that is, the "Fast Integer type with the minimum specified length ". Each of these typedef names represents an integer type, which provides the fastest processing speed when the length is no less than a specified length. These integer types are named int_fastX_t (Signed) or uint_fastX_t (unsigned). "X" indicates the minimum length. For example, int_fast32_t indicates a fast signed integer with a character length of at least 32 bits. The minimum character length and quick integer types include:

Int_fast8_t int_fast16_t int_fast32_t int_fast64_t

The corresponding unsigned integer types include:

Uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t

Under what circumstances will these typedef names be used?

Suppose you need a cyclic counter with a character length of not less than 16 bits, then you want the Counter type to always be the integer type that is best operated by the current computer CPU, int_fast16_t ensures that the compiler on any platform always chooses the fastest Integer type with a character length of not less than 16 bits.

# Include <inttypes. h>
For (int_fast16_t n = 0; n <30000; ++ n)
{
// .. Do something
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.