tags:

views:

2002

answers:

7

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?

+6  A: 

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

ie:

boost::uint32_t
boost::int32_t

etc

Doug T.
That's a good alternative.
Bill the Lizard
That's good, I'd say the only real problem is the dependency on the boost library.
BobbyShaftoe
+23  A: 

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;
Adam
+1 for being wickedly fast. I had my answer ready but got distracted for a minute. :)
Bill the Lizard
I would find the route of using numeric_limits to be preferable.
Jon Trauntvein
beware when using ::digits. you will get the amount of bits (excluding the sign bit), that is the dual digits :). ::digits10 will give you the num of decimal digits.
Johannes Schaub - litb
beware in win32 if you also include windows.h, max is defined as a macro and you will get strange compilation behavior.
Doug T.
@Doug T.: Use (std::numeric_limits<int>::max)() instead, or undefine max before this call.
Jasper Bekkers
+9  A: 

Take a look at std::numeric_limits

Nemanja Trifunovic
That's interesting. I wasn't aware of it before.
Bill the Lizard
both have their uses. macros from climits can be used in preprocessor tests and can be passed as template arguments. while numeric_limits offer some other fine bits of possibilities besides the range checking stuff.
Johannes Schaub - litb
+1  A: 

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);
}
Bill the Lizard
+3  A: 

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

Marc
Thanks. It's good to know a platform independent alternative.
Bill the Lizard
nobar
A: 
sizeof(int)
Robert
That tells you how many bytes a type takes up in memory. What tells you the range?
Bill the Lizard
0 to 2 ** (sizeof(int) * 8) - 1, for unsigned types anyway :-)
Ferruccio
Ferruccio. nono, let's stay portable: 0 to 2 ** (sizeof(int) * CHAR_BIT) - 1 hehe
Johannes Schaub - litb
Even then, still not portable. The only integer types in which every bit of the storage representation is required to participate in the value representation are the three char types. It's perfectly valid to have a 9 bit char, but a 32 bit int.
Steve Jessop
guys, look at my response
A: 

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

[email protected]