Thread: Range of an integer.

  1. #1
    Unregistered
    Guest

    Question Range of an integer.

    As far as I know, the range for an integer is from -32768 to 32768, right? But take a look at this code:

    #include <stdio.h>

    main()
    {
    int n, lar = -32768, count;

    for( count = 0 ; count < 10 ; count++ )
    {
    printf("Please enter your %d integer: ",count+1);
    scanf("%d", &n);

    if( n >= lar)
    lar = n;

    }

    printf("Largest is %d", lar);

    return 0;
    }

    Eventhough when I entered an integer number out of the range of integer, the compiler still work properly. Why?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the range for an integer is from -32768 to 32768, right
    The standard dictates that an integer is guaranteed to have at least 16 bits of precision. Different compilers are free to increase that precision as long as they maintain the 16 bit minimum.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If you declare a variable of type integer and give it a value larger than it's range (depends on the system you are working), then some compilers will make the value smaller so it fits in the maximum integer. So if you are working on a 32 bit system, and your value requires 45 bits, then the compiler will throw away the last 13 bits.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    #include <limits.h>
    #include <stdio.h>
    
    int main(void)
    {
     printf("The highest integer possible is %d",
            INT_MAX);
     return 0;
    }

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Hmmm...I never knew of that constant. Thanks!
    1978 Silver Anniversary Corvette

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    most x86 computers range about +/- 2.1 billion
    although a long long can range farther

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Or you can just create your own datatypes when you want to use huge numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM