# [ACCEPTED]-How do you find the range of values that integer types can represent in C++?-types

## C Style

limits.h contains the min and max values 8 for ints as well as other data types which 7 should be exactly what you need:

```
#include <limits.h> // C header
#include <climits> // C++ header
// Constant containing the minimum value of a signed integer (–2,147,483,648)
INT_MIN;
// Constant containing the maximum value of a signed integer (+2,147,483,647)
INT_MAX;
```

For a complete 6 list of constants and their common values 5 check out: Wikipedia - limits.h

## C++ Style

There is a template based C++ method 4 as other commenters have mentioned using:

```
#include <limits>
std::numeric_limits
```

which 3 looks like:

```
std::numeric_limits<int>::max();
```

and it can even do craftier things 2 like determine the number of digits possible 1 or whether the data type is signed or not:

```
// Number of digits for decimal (base 10)
std::numeric_limits<char>::digits10;
// Number of digits for binary
std::numeric_limits<char>::digits;
std::numeric_limits<unsigned int>::is_signed;
```

Take a look at `std::numeric_limits`

0

Why not just be sure and use boost's numeric 1 types?

ie:

```
boost::uint32_t
boost::int32_t
```

etc

Use the `sizeof()`

operator in C++ to determine the 6 size (in bytes) of a value type. The standard 5 library header file limits.h contains the 4 range limits for integer value types. You 3 can run the following program to learn the 2 size and range limits for integer types 1 on your system.

```
#include <stdlib.h>
#include <iostream>
#include <limits>
using namespace std;
int main(int argc, char** argv) {
cout << "\nCharacter Types" << endl;
cout << "Size of character type is " << sizeof(char) << " byte." << endl;
cout << "Signed char min: " << SCHAR_MIN << endl;
cout << "Signed char max: " << SCHAR_MAX << endl;
cout << "Unsigned char min: 0" << endl;
cout << "Unsigned char max: " << UCHAR_MAX << endl;
cout << "\nShort Int Types" << endl;
cout << "Size of short int type is " << sizeof(short) << " bytes." << endl;
cout << "Signed short min: " << SHRT_MIN << endl;
cout << "Signed short max: " << SHRT_MAX << endl;
cout << "Unsigned short min: 0" << endl;
cout << "Unsigned short max: " << USHRT_MAX << endl;
cout << "\nInt Types" << endl;
cout << "Size of int type is " << sizeof(int) << " bytes." << endl;
cout << "Signed int min: " << INT_MIN << endl;
cout << "Signed int max: " << INT_MAX << endl;
cout << "Unsigned int min: 0" << endl;
cout << "Unsigned int max: " << UINT_MAX << endl;
cout << "\nLong Int Types" << endl;
cout << "Size of long int type is " << sizeof(long) << " bytes." << endl;
cout << "Signed long min: " << LONG_MIN << endl;
cout << "Signed long max: " << LONG_MAX << endl;
cout << "Unsigned long min: 0" << endl;
cout << "Unsigned long max: " << ULONG_MAX << endl;
return (EXIT_SUCCESS);
}
```

You can use the types defined in *stdint.h* (or *cstdint*, if 5 you are using C++), which are part of the 4 C99 standard. It defines types with such 3 names as *int32_t*, *uint8_t*, *int64_t*, an so on, which are guaranteed 2 to be portable and platform independent.

For 1 more information: stdint.h

You can get the range of any data type by 4 applying the following formulla:

**[-2 power (N-1)] to { [+2 power (N-1)] - 1 }**

Where "N" is 3 the width of data type, for example in JAVA 2 the width of int is 32,hence N = 32.

Try 1 this out you will get it.

```
#include<stdio.h>
#include<limits.h>
void main()
{
printf(" signed data types " );
printf(" int min : %d ", INT_MIN); // INT_MIN, INT_MAX, SCHAR_MIN, SCHAR_MAX ....etc
printf(" int max : %d ",INT_MAX);// pre defined constants to get the values of datatypes
printf(" signed char min : %d ", SCHAR_MIN);
printf(" signed char max : %d ", SCHAR_MAX);
// similarly for un_signed
// use %u for control charter, and UINT_MAX, UCHAR_MAX, USHRT_MAX, ULONG_MAX.
}
```

0

Bitwise operations can be used to find the 3 number of bits and range of int in a platform. Here 2 is a sample I wrote to test the range of 1 int on my machine.

```
#include <iostream>
using namespace std;
void print_int_range() {
int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (1 << (nOfBits - 1)) << ", maximum int: " << ~(1 << (nOfBits - 1)) << endl;
}
void print_unsigned_int_range() {
unsigned int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "unsigned int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (0) << ", maximum int: " << (unsigned int) (~0) << endl;
}
int main() {
print_int_range();
print_unsigned_int_range();
}
```

And here is my output:

```
int has 32 bits
mininum int: -2147483648, maximum int: 2147483647
unsigned int has 32 bits
mininum int: 0, maximum int: 4294967295
```

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.