Bill the Lizard
Bill the Lizard

Reputation: 405765

How do you find the range of values that integer types can represent in C++?

The size and range of the integer value types in C++ are platform specific. Values found on most 32-bit systems can be found at Variables. Data Types. - C++ Documentation. How do you determine what the actual size and range are for your specific system?

Upvotes: 11

Views: 23404

Answers (9)

Shaohong Li
Shaohong Li

Reputation: 58

Bitwise operations can be used to find the number of bits and range of int in a platform. Here is a sample I wrote to test the range of 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

Upvotes: 0

Th.Srinivas
Th.Srinivas

Reputation: 11

#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.  
}

Upvotes: 1

Prathap
Prathap

Reputation:

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.

Upvotes: 2

Adam
Adam

Reputation: 26497

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;

Upvotes: 38

Bill the Lizard
Bill the Lizard

Reputation: 405765

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);
}

Upvotes: 3

Robert Deml
Robert Deml

Reputation: 12532

sizeof(int)

Upvotes: -5

Marc
Marc

Reputation: 1356

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

Upvotes: 3

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

Take a look at std::numeric_limits

Upvotes: 9

Doug T.
Doug T.

Reputation: 65609

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

ie:

boost::uint32_t
boost::int32_t

etc

Upvotes: 6

Related Questions