Open In App

C++ 11 – <cinttypes> Header

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures. 

<cinttypes> header

The <cinttypes> header presents numerous type aliases that are utilized to signify integer types of precise sizes and signedness. These aliases are defined in relation to their correspondent integer types to demonstrate the use of the <cinttypes> Header. They furnish an effortless method of specifying integer types with specific sizes, without concerning oneself with the details that are contingent on the platform.

Aliases in <cinttypes> header

Here are some examples of the type aliases defined in <cinttypes> are mentioned below:

  • int8_t: A signed integer type that is exactly 8 bits wide.
  • int16_t: A signed integer type that is exactly 16 bits wide.
  • int32_t: A signed integer type that is exactly 32 bits wide.
  • int64_t: A signed integer type that is exactly 64 bits wide.
  • uint8_t: An unsigned integer type that is exactly 8 bits wide.
  • uint16_t: An unsigned integer type that is exactly 16 bits wide.
  • uint32_t: An unsigned integer type that is exactly 32 bits wide.
  • uint64_t: An unsigned integer type that is exactly 64 bits wide.

Syntax in <cinttypes>:

#include <cinttypes>

Examples in <cinttypes> header:

Example 1: 

C++14




// C++ Program to demonstrate the use of the <cinttypes>
// Header.
#include <cinttypes>
#include <iostream>
  
int main()
{
    std::int64_t myInt = 1234567890123456;
    std::cout << "My integer: %" PRId64 "\n", myInt;
    return 0;
}


Output:

My integer: 1234567890123456

Explanation

To display a message on the console using an int64_t variable, we use the PRId64 format specifier preceded by the % sign. It is defined in the cinttypes header and instructs the formatting of the output implemented by the cout object. The message is printed through the use of the std::cout object.

Example 2: In this code, we will make use of the above syntax 

C++




// C++ Program to demonstrate the use of the <cinttypes>
// Header.
#include <cinttypes>
#include <iostream>
  
int main()
{
    uint8_t myByte = 0xFF;
    std::printf("My byte: %" PRIu8 "\n", myByte);
    return 0;
}


Output:

My byte: 255

Explanation

In this example, a value of 255, signified by 0xFF, is assigned to a variable called myByte during initialization. The PRIu8 format specifier, created for showcasing unsigned 8-bit integers, is used in conjunction with the printf function to display the value of myByte.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads