# Thread: Maximum and Minimum Values

1. ## 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

Code:
`unsigned char maxchar = ~0;`

3. limits.h should make things easy.

4. 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. There is no portable way to find out by direct computation.
Variable underflow for unsigned types isn't portable? -1u

6. 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. 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.

8. Originally Posted by dwks
```int x;