Originally Posted by
Tool
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.
What is direct computation? Does it means using INT_MAX?
How can i determine ranges of floating types on my machine? Using C?
What they want you to do is to use the standard header files to print out the ranges of these data types. Yes, INT_MAX is one of them.
You'll want to include headers like limits.h
Then the second part of your assignment is to find these values yourself, on your system, using direct computation.
pseudo code for find char limit would be something like this:
Code:
#include <stdio.h>
int main() {
char limit = 0;
while(++limit > 0);
now limit has passed it's limit, and gone negative,
so back it up by 1
just for the heck of it, print the negative value of limit, before you
back it up by 1
--limit;
limit should now be at it's maximum value.
You can use similar logic to find the max value of any data type. If the type is
unsigned, the specifics change a bit, but not much.
return 0;
}
For larger data types, you can speed it up a great deal by having the variable increment by say, 10,000 each time, then use a while loop to back it down, instead of just subtracting one. Big time saver.
The sizeof method mentioned above is good, and much faster. Since there would still be some multiplication involved, I think that would also be a "direct computation".
It's great to know how your data type is laid out in bits and bytes, and learning about MSB, and such.