Just a quick question, how large can an int hold?
Is it between -999999999 and 999999999?
does this work?
int num = 999999999 + 1;
thx
Just a quick question, how large can an int hold?
Is it between -999999999 and 999999999?
does this work?
int num = 999999999 + 1;
thx
As tabstop mentioned,you include the limits.h header file in your program.You can simply the print the values by using the macros such as INT_MAX to get the maximum value and INT_MIX to get the minimum value.
Last edited by vart; 04-02-2010 at 12:42 AM.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Usually 2^31-1 to -2^31 on PCs, and 2^15-1 to -2^15 on 8/16-bit embedded systems.
But the only way to know for sure is INT_MAX.
Last edited by cyberfish; 04-02-2010 at 01:50 AM.
I think the ranges are more likely to be [-2^31, 2^31-1] and [-2^15, 2^15-1] respectively.Originally Posted by cyberfish
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah right, thanks! (editted)
The standards actually specifies that the range of an int is INT_MIN to INT_MAX.
INT_MIN and INT_MAX are implementation defined (i.e. they depend on the compiler, operating system, etc).
The 1999 C standard specifies (in effect, the wording is slightly different) that INT_MIN will not be greater than -32767 and INT_MAX shall not be less than +32767. This translates, practically, to at least a 16 bit integer, including the sign bit.
I don't have a copy of the 1989 C standard handy. But my recollection is that the 1999 and 1989 C standards have the same requirements.
The results on signed integer overflow (eg adding 1 to an int containing the value INT_MAX) are undefined: anything is allowed to happen.
Last edited by grumpy; 04-02-2010 at 02:42 AM.
Well, considering that 99.9% of the world uses two's-complement, I'd just as soon calculate it programmatically. I do have an odd fascination with precalculating constants, though. Go figure.
Code:template < typename Integer > Integer precalculate_maximum( void ) { Integer mask = 1, value = 0; for( ;; ) { value |= mask; if( Integer( mask << 1 ) < mask ) break; mask <<= 1; } return value; } template < typename Integer > Integer maximum( void ) { static Integer const value = precalculate_maximum< Integer >( ); return value; } #include <iostream> int main( void ) { using namespace std; cout << int( maximum< signed char >( ) ) << endl; cout << int( maximum< unsigned char >( ) ) << endl; cout << maximum< int >( ) << endl; cout << maximum< unsigned int >( ) << endl; cout << maximum< short >( ) << endl; cout << maximum< unsigned short >( ) << endl; cout << maximum< long >( ) << endl; cout << maximum< unsigned long >( ) << endl; }
The MinGW port of gcc 3.4.5 reports a syntax error on line 1Originally Posted by Sebastiani
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)