# Maximum and Minimum Values

• 12-13-2006
swaugh
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
• 12-13-2006
vart
Code:

`unsigned char maxchar = ~0;`
and post your effort
• 12-13-2006
OnionKnight
limits.h should make things easy.
• 12-13-2006
noodles
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.
• 12-13-2006
dwks
Quote:

There is no portable way to find out by direct computation.
Variable underflow for unsigned types isn't portable? -1u
• 12-13-2006
noodles
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.
• 12-16-2006
dwks
Quote:

Quote:

Quote:

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. :)

Code:

```int x; for(x = 0; (int)(x+1) > x; x ++);```
<limits.h> is definitely a better idea. :)
• 12-16-2006
quzah
Quote:

Originally Posted by dwks
```int x; for(x = 0; (int)(x+1) > x; x ++);```