Open In App

C++ 11 – <cstdint> Header

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

<cstdint> header in C++ ensures the portability of integer types with specialized width and signedness across various systems. The absence of standardization for integer types caused issues in coding and constructing portable programs before the advent of. In this article, we will explore the header and its application in C++11.

Types in <cstdint> Header

These are the types defined in <cstdint> header mentioned below: 

  • int8_t: a signed 8-bit integer
  • uint8_t: an unsigned 8-bit integer
  • int16_t: a signed 16-bit integer
  • uint16_t: an unsigned 16-bit integer
  • int32_t: a signed 32-bit integer
  • uint32_t: an unsigned 32-bit integer
  • int64_t: a signed 64-bit integer
  • uint64_t: an unsigned 64-bit integer

Usage of <cstdint> Header

To use the <cstdint> header, simply include it at the beginning of your code file. 

Syntax

#include <cstdint>

Example 1:

C++14




// C++ Program to demonstrate the use of the <cstdint>
// Header.
#include <cstdint>
#include <iostream>
  
// driver function
int main()
{
    int32_t myInt = 42;
    uint16_t myUInt = 65535;
  
    std::cout << "Size of int32_t: " << sizeof(myInt)
              << " bytes\n";
    std::cout << "Size of uint16_t: " << sizeof(myUInt)
              << " bytes\n";
  
    std::cout << "The value of myInt is: " << myInt << "\n";
    std::cout << "The value of myUInt is: " << myUInt
              << "\n";
  
    return 0;
}


Output

Size of int32_t: 4 bytes
Size of uint16_t: 2 bytes
The value of myInt is: 42
The value of myUInt is: 65535

Explanation:

We create a variable called myInt with a value of 42 represented as int32_t, and another variable called myUInt with a value of 65535 represented as uint16_t, which is the utmost value that can be represented by a 16-bit unsigned integer. After that, the code displays the byte size of myInt and myUInt alongside their individual values.

Example 2: 

C++




// C++ Program to demonstrate the use of the <cstdint>
// Header.
#include <cstdint>
#include <iostream>
  
int main()
{
    std::int32_t x = 100;
    std::int64_t y = 1000000000000;
    std::uint16_t z = 255;
  
    std::cout << "x = " << x << std::endl;
    std::cout << "y = " << y << std::endl;
    std::cout << "z = " << z << std::endl;
  
    return 0;
}


Output

x = 100
y = 1000000000000
z = 255

Advantages of using <cstdint> Header

There are certain advantages of using <cstdint> header are mentioned below:

  • Portability: When it comes to writing code that can work seamlessly across various platforms, the header plays a significant role in ensuring that integer types maintain a consistent width and signedness. This feature eliminates the chances of encountering unexpected behavior caused by differences in integer types, thus guaranteeing that the code is portable.
  • Clarity: The utilization of ‘s integer types has the potential to improve code readability and comprehension. Opting for unambiguous integer types that ensure specific widths and signedness can prevent misconceptions and minimize errors related to integer types.
  • Improved performance: Optimizing code for better performance can be achieved by utilizing integer types with a confirmed width. By doing so, the compiler can operate more efficiently since it can make assumptions about the size of said integer types, ultimately resulting in more efficient code.
  • Better error handling: Opting for certain integer types can simplify the process of detecting errors linked to integer overflow or underflow. These kinds of types can also avert unintentional actions resulting from implicit type conversions.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads