# An exercise from K&R

This is a discussion on An exercise from K&R within the C Programming forums, part of the General Programming Boards category; Hello all. Here's the subject: Exercise 2-1. Write a program to determine the ranges of char, short, int, and long ...

1. ## An exercise from K&R

Hello all. Here's the subject:
Exercise 2-1. 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.
Okay, 'direct computation' for (unsigned) integer numbers means 2^n where n is a number of bits for type. But what would it look like for floating-point numbers? Also I would very appreciate any good pointers to where I can find out about machine floating-point representation in great detail, since I'm very short in knowledge about it.

2. The size of the data type, depends a lot on the OS and compiler you're using. Check your compiler's help pages for the details.

But can you actually code something that calculates the largest possible range of these data types? I don't mean 2^n or 2^n-1, but actually code up a solution using basic arithmetic?

3. >The size of the data type, depends a lot on the OS and compiler you're using.
Computing the range of unsigned int:
Code:
`unsigned range = pow(2.0, sizeof(int) * 8) - 1;`
So what are you trying to say? That it wouldn't work in any envieronment?

>But can you actually code something that calculates the largest possible range of these data types? I don't mean 2^n or 2^n-1, but actually code up a solution using basic arithmetic?
It's just that I urge to know about how float is represented internally.

4. I thought you were going to pull out the MAX_INT type macro's. That equation is a good one.

I was working on this with a beginner a few months back, and we found out that you can go beyond the range that the MAX_(DATA TYPE), give you, for the larger unsigned one's and for floats and doubles.

We were both surprised.

Popular pages Recent additions