Maximum and Minimum Values

This is a discussion on Maximum and Minimum Values within the C Programming forums, part of the General Programming Boards category; Hi Guys, Let us do a small program. Find out the maximum and minimum values of different types in C ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    Maximum and Minimum Values

    Hi Guys,
    Let us do a small program. Find out the maximum and minimum values of different types in C (int, unsigned int, float, double etc). The challenging part here is that the program needs to be portable. It should give the correct ranges when run either on a 32 bit or a 64 bit machine

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you can start with
    Code:
    unsigned char maxchar = ~0;
    and post your effort
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    limits.h should make things easy.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by swaugh
    Hi Guys,
    Let us do a small program. Find out the maximum and minimum values of different types in C (int, unsigned int, float, double etc). The challenging part here is that the program needs to be portable. It should give the correct ranges when run either on a 32 bit or a 64 bit machine
    For integral types, see the macros defined in <limits.h>
    e.g. 'INT_MAX', 'INT_MIN', etc.

    For real number types, see the macros in <float.h>,
    e.g. 'FLT_MIN', 'FLT_MAX', etc.

    That's the ONLY way your program can be fully portable. There is no portable way to find out by direct computation.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There is no portable way to find out by direct computation.
    Variable underflow for unsigned types isn't portable? -1u
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by dwks
    Variable underflow for unsigned types isn't portable? -1u
    It is, but I meant that there isn't any portable way for ALL the types, including the signed ones, as the OP wanted.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There is no portable way to find out by direct computation.
    Variable underflow for unsigned types isn't portable? -1u
    It is, but I meant that there isn't any portable way for ALL the types, including the signed ones, as the OP wanted.
    Sorry, I took your statement to mean all types. Whew, because I'm very fond of using (size_t)-1 rather than the C99 SIZE_MAX.

    What about this for integral types?
    Code:
    int x;
    for(x = 0; (int)(x+1) > x; x ++);
    <limits.h> is definitely a better idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    What about this for integral types?
    Code:
    int x;
    for(x = 0; (int)(x+1) > x; x ++);
    Undefined behavior for signed types.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 02:54 PM
  2. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM
  5. Replies: 2
    Last Post: 10-31-2002, 06:27 PM

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