Integer Limits in C++
In C++, integers are fundamental data types used to represent whole numbers. Each integer type has a specific range and size, which are crucial to understand for efficient memory usage and to prevent overflow errors. These limits are defined in the C standard header file <limits.h>
, and in C++, the <limits>
header includes <climits>
, which in turn includes <limits.h>
.
Integer Types and Their Limits Table
The following table summarizes the minimum and maximum values for various integer types in C++:
Type | Minimum Value | Maximum Value |
---|---|---|
signed char | -128 | 127 |
unsigned char | 0 | 255 |
char | -128 (0 if /J option used) | 127 (255 if /J option used) |
short | -32,768 | 32,767 |
unsigned short | 0 | 65,535 |
int | -2,147,483,648 | 2,147,483,647 |
unsigned int | 0 | 4,294,967,295 |
long | -2,147,483,648 | 2,147,483,647 |
unsigned long | 0 | 4,294,967,295 |
long long | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
unsigned long long | 0 | 18,446,744,073,709,551,615 |
Special Considerations
Note: The /J
compiler option changes the default char
type from signed char
to unsigned char
, affecting the range of char
. Additionally, if a value exceeds the largest integer representation, the Microsoft compiler generates an error.