The size and range of the integer value types in C++ are platform specific. Values found on most 32-bit systems can be found here. How do you determine what the actual size and range are for your specific system?
views:
2002answers:
7Why not just be sure and use boost's numeric types?
ie:
boost::uint32_t
boost::int32_t
etc
C Style
limits.h contains the min and max values for ints as well as other data types which 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 list of constants and their common values check out: Wikipedia - limits.h
C++ Style
There is a template based C++ method as other commenters have mentioned using:
#include <limits>
std::numeric_limits
which looks like:
std::numeric_limits<int>::max();
and it can even do craftier things like determine the number of digits possible 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;
Use the sizeof()
operator in C++ to determine the size (in bytes) of a value type. The standard library header file limits.h contains the range limits for integer value types. You can run the following program to learn the size and range limits for integer types 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 you are using C++), which are part of the C99 standard. It defines types with such names as *int32_t*, *uint8_t*, *int64_t*, an so on, which are guaranteed to be portable and platform independent.
For more information: stdint.h
You can get the range of any data type by applying the following formulla:
[-2 power (N-1)] to { [+2 power (N-1)] - 1 }
Where "N" is the width of data type, for example in JAVA the width of int is 32,hence N = 32.
Try this out you will get it.
From:
Prathap Kumar SV