C/C++ has very loose definitions on its basic integer data types (char, short, int, long, and long long). The language guarantees that they can represent at least some range of values, but any particular platform (compiler, operating system, hardware) might be larger than that.
A good example is long. On one machine, it might be 32 bits (the minimum required by C). On another, it’s 64 bits. What do you do if you want an integer type that’s precisely 32 bits long? That’s where int32_t comes in: it’s an alias for whatever integer type your particular system has that is exactly 32 bits.
intN_t or uintN_t Where N is width of integer which can be 8, 16, 32, 64 or any other type width supported by the library.
In function 'int main()': 19:7: warning: large integer implicitly truncated to unsigned type [-Woverflow] i = 2436; ^ Minimum value of i : 0 Maximum value of i : 255 Beyond range value of i : 132
1. Fixed width unsigned 8 bit integer: uint8_t
It means give me an unsigned int of exactly 8 bits.
2. Minimum width unsigned 8 bit integer: uint_least8_t
It means give me the smallest type of unsigned int which has at least 8 bits. Optimized for memory consumption.
3. Fastest minimum width unsigned 8 bit integer: uint_fast8_t
It means give me an unsigned int of at least 8 bits which will make my program faster. It may pick larger data type because of alignment considerations. Optimized for speed.
Thus a uint8_t is guaranteed to be exactly 8 bits wide. A uint_least8_t is the smallest integer guaranteed to be at least 8 bits wide. An uint_fast8_t is the fastest integer guaranteed to be at least 8 bits wide.
So the extended integral types help us in writing portable and efficient code.
This article is contributed by Rohit Kasle. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between fundamental data types and derived data types
- Get the stack size and set the stack size of thread attribute in C
- Macros and its types in C/C++
- Data Types in C
- C++ Data Types
- C | Data Types | Question 9
- C | Data Types | Question 2
- C | Data Types | Question 5
- C | Data Types | Question 1
- C | Data Types | Question 6
- Derived Data Types in C++
- Enumerated Types or Enums in C++
- Types of Literals in C/C++ with Examples
- Types of Operator Overloading in C++
- Uninitialized primitive data types in C/C++
- User defined Data Types in C++
- Runtime Polymorphism in various types of Inheritance in C++
- Calculate range of data types using C++
- Introduction of Smart Pointers in C++ and It's Types
- C++ default constructor | Built-in types