C++ 11 - <cstdint> Header
Last Updated :
10 May, 2023
<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;
}
OutputSize 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;
}
Outputx = 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.
Similar Reads
C++ 11 - <cinttypes> Header 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
2 min read
C++17 - <charconv> Header The C++ <charconv> header provides many functions for converting the character sequences to numerical values and vice-versa. It is considered better than the <cstdlib> header file functions for the same purpose. The functions provided by the <charconv> header file are generally fas
5 min read
C++ <cstdio> The <cstdio> header file is a part of the C++ standard library collection that provides the input and output methods of <stdio.h> library of C language. We can use the traditional C-style input and output method in C++ using the <cstdio> library. It also contains all the functions
7 min read
C++ <cstring> The <cstring> library is a part of the standard C++ library collection that provides the commonly used methods for C-Style string manipulation. It is inherited from the <string.h> library of C language. We can import the <cstring> header file using #include preprocessor directive a
5 min read
C++23 <print> Header C++ has long been a powerful language, known for its versatility and performance. With each new standard release, the language evolves, introducing features that enhance developer productivity and code readability. One of these additions of the C++ 23 standard is the introduction of <print> he
3 min read
C++ 11 Standard C++ 11, officially known as ISO/IEC 14882:2011, is a significant version of the C++ programming language standard, published in 2011. It marked a major fix up of the language, introducing various features and enhancements that improved the usability, performance, and safety of C++ code. Before C++ 1
5 min read