Thread: computing max/min values of numeric variables: ints, chars, etc

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    computing max/min values of numeric variables: ints, chars, etc

    What are the best methods?

    Overflowing is by the standard, undefined - incrementing the variable until it becomes negative; and then seting it one position back. So we don't want that.

    So that leaves us with only one option - using sizeof() operator and then calculating pow(2, sizeof() - 1) - 1?

    Any other alternatives?
    Last edited by Tool; 06-10-2010 at 10:11 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your title is a bit misleading. A lot of things are variables, including pointers. I guess you are trying to determine the size of your primitive numeric types?

    If so, these are determined by the architecture you are compiling on, so I don't see any reason why they should be "determined".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    It's a part of an exercise in K&R;

    Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
    Thats why i was wandering if there are any other methods.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    #include <limits.h>

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Since the exercise is expecting computation then the increment method seems fine. It may take too long for larger integers though. Perhaps start with 1 and shift it left until the value is less than its previous value. That will determine the number of bits. Of course you could do sizeof(n)*8 assuming a byte is 8 bites on your architecture.

    If it can not be assumed a machine has 8 bits per byte, and it can not be assumed the machine uses 2's complement representation, then incrementing until the result is not one greater than it was previous is one reliable way.

    Some fancier iterative way that uses larger step sizes which are reduced logarithmically would be faster.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can use CHAR_BITS * sizeof(whatever).

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    To be clear, BYTE_BITS would be more correct, but Bayint Naung has a good idea.

    Is there an architecture still in use that uses not 8 bit bytes? I know the byte used to vary quite a bit, but I have no knowledge of a current machine using non-8 bit bytes.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What a mystery!

    We went through this same question just six months ago. Why are you bringing up the same questions, again?

    As discussed here:
    direct computation

    there are three ways to get the max and minimum values for data types:

    1) Use sizeof(datatype) and remember to subtract one for positive values, if it's signed.

    2) Have your compiler tell you what the values are, using macro's like INT_MAX or MAX_INT or whatever names your compiler uses for this. Your compiler's help files will pinpoint the exact name for these macros.

    3) Direct computation, until you overflow. It's undefined in the C standard, but not necessarily on your computer and compiler. Remember that it relies on 2's complement, which the vast majority of modern PC's use, and on your compiler using 2's complement in a very popular manner.

  9. #9
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    EDIT: Adak beat me to it .
    Or you could overflow the variable.
    Code:
        int max;
        int test = 1;
        for(max = 0; test > max; test++)
            max = test;
        printf("Max Value: %i", max);
        printf("Min Value: %i", test);
    "test" will overflow to the lowest possible, and "max" will equal the highest.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by User Name: View Post
    To be clear, BYTE_BITS would be more correct, but Bayint Naung has a good idea.
    To be clear, Bayint had it right. CHAR_BITS is defined in the C standard. BYTE_BITS is not.

    The results of overflowing or underflowing a signed integral type are undefined i.e. not guaranteed to work. Unsigned types wrap (or, more accurately, use modulo arithmetic) signed types don't.
    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, Buttercup, and read this, this, and this before posting again.

  11. #11
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by grumpy View Post
    To be clear, Bayint had it right. CHAR_BITS is defined in the C standard. BYTE_BITS is not.

    The results of overflowing or underflowing a signed integral type are undefined i.e. not guaranteed to work. Unsigned types wrap (or, more accurately, use modulo arithmetic) signed types don't.
    An unsigned would overflow to 0, an unsigned's minimum value. You'd have to printf with "%u" instead "%i" though.
    Code:
        unsigned int max;
        unsigned int test = 1;
        for(max = 0; test > max; test++)
            max = test;
        printf("Max Value: %u\n", max);
        printf("Min Value %u",test);
    I get 4294967295 and 0 on 64 bit Linux.

    As for CHAR_BIT and BYTE_BIT, that was ignorance on my part. I didn't know CHAR_BIT was already defined, I though Bayint was meaning OP had to fill in the value every time, in which case BYTE would make more sense. Bayint was right. sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 03:58 AM
  2. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  3. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  4. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM
  5. finding max values in an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 02:47 PM