Thread: Can large can an int hold?

1. Can large can an int hold?

Just a quick question, how large can an int hold?

Is it between -999999999 and 999999999?

does this work?

int num = 999999999 + 1;

thx

2. Originally Posted by winggx
Just a quick question, how large can an int hold?
INT_MAX. (What value that has on your system, only your system knows.) (And you will need to #include <limits.h>.)

3. Integer max value:

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.

4. Originally Posted by vivekraj
int_mix to get the minimum value.
Code:
`int_min`
PS I do not know why it changes constnt to lower case every time I edit this post...

5. 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.

6. Originally Posted by cyberfish
Usually 2^31 to -2^31-1 on PCs, and 2^15 to -2^15-1 on 8/16-bit embedded systems.
I think the ranges are more likely to be [-2^31, 2^31-1] and [-2^15, 2^15-1] respectively.

7. Ah right, thanks! (editted)

8. 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.

9. 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
value = 0;
for( ;; )
{
break;
}
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;
}```

10. Originally Posted by Sebastiani
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.
The MinGW port of gcc 3.4.5 reports a syntax error on line 1

11. Originally Posted by laserlight
The MinGW port of gcc 3.4.5 reports a syntax error on line 1
Bah! Well, the basic principle is the same, anyway. Way more fun in C++, though, IMO...