Can large can an int hold?

This is a discussion on Can large can an int hold? within the C Programming forums, part of the General Programming Boards category; Just a quick question, how large can an int hold? Is it between -999999999 and 999999999? does this work? int ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by winggx View Post
    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>.)
    Last edited by tabstop; 04-01-2010 at 10:55 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    36

    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. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by vivekraj View Post
    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...
    Last edited by vart; 04-02-2010 at 01:42 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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 02:50 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Ah right, thanks! (editted)

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,624
    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 03:42 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote 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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21