# [C++] max value of numeric types ???



## maccatalan (Oct 13, 2002)

Hi.

As you know, a sizeof() function exists. It returns the number of bits that the type you put as argument needs to be stored in memory.
This number can be different from a platform to another, from a compiler to another.

but this is not what I need. 
I just would like the max value (the range of values) that the value type allows. We can compute it thanks to 2^sizeof(type)-1 when unsigned (else divide by two) but 2^sizeof(type) is greater than the max allowed value so it should not work.

Any suggestion ?

thank you very much,
Pierre


----------



## r4bid (Oct 13, 2002)

what data types are you interested in?  ints, floats etc...


I found this online, I am not quite sure if its what you want but it might be 






#include <iostream>
#include <climits>

using std::cout;
using std::endl;

volatile int char_min = CHAR_MIN;

int main(void)
{
    cout << "Size of boolean type is "
        << sizeof(bool) << " byte(s)"
        << "\n\n";

    cout << "Number of bits in a character: "
        << CHAR_BIT << '\n';
    cout << "Size of character types is "
        <<sizeof(char)
        << " byte" << '\n';
    cout << "Signed char min: "
        << SCHAR_MIN << " max: "
        <<SCHAR_MAX << '\n';
    cout << "Unsigned char min: 0 max: "
        << UCHAR_MAX << '\n';

    cout << "Default char is ";

    if (char_min < 0)
        cout << "signed";
    else if (char_min == 0)
        cout << "unsigned";
    else
        cout << "non-standard";
    cout << "\n\n";

    cout << "Size of short int types is "
        << sizeof(short) << " bytes"
        << '\n';
    cout << "Signed short min: "
        << SHRT_MIN << " max: "
        << SHRT_MAX << '\n';
    cout << "Unsigned short min: 0 max: "
        << USHRT_MAX << "\n\n";

    cout << "Size of int types is "
        << sizeof(int) << " bytes"
        << '\n';
    cout << "Signed int min: "
        << INT_MIN << " max: "
        << INT_MAX << '\n';
    cout << "Unsigned int min: 0 max: "
        << UINT_MAX << "\n\n";

    cout << "Size of long int types is "
        << sizeof(long) << " bytes"
        << '\n';
    cout << "Signed long min: " <<
        LONG_MIN << " max: "
        << LONG_MAX << '\n';
    cout << "Unsigned long min: 0 max: "
        << ULONG_MAX << endl;

    return 0;
}









Incase that didn't post right you can check out the code at http://home.att.net/~jackklein/c/inttypes.html#short

towards the very bottom


----------



## r4bid (Oct 13, 2002)

ok yeah so it didn't post right (anything in carots got erased) so just check out the last program at the bottom of the page.


----------



## maccatalan (Oct 14, 2002)

it's ok. Thank you very much !!  
wonderful!

I was looking the ULONG_MAX constant.  
Thank you very much,
Pierre.


----------



## Minion (Oct 14, 2002)

All these constants (macros here) are defined in "limits.h" files headers. Have a look at /usr/include/machine/limits.h for these machine-specific numerical constants.

Otherwise look at /usr/include/sys/syslimits.h for some system-specific values or /usr/include/limits.


----------

