# VERY newb homework assignment

• 04-13-2006
ajguerrero
VERY newb homework assignment
here is my hw assignment. i have absolutely no experience with C. so any help would be appreceated

write a program to calculate the highest and lowest number that can be stored in: a char, an int (signed and unsigned), a short int, a long int. once you have done this look at the include files limits.h and float.h do the values in these files match the ones you calculated?

i really have no idea where to start because this isnt in my book, itwas just assigned to us expected that we know some C.
• 04-13-2006
itsme86
Read up on loops and integer overflow.
• 04-13-2006
Quantum1024
What you need to do is first find out how many bytes each of these types uses. There are 8 bits in each byte and every bit doubles the amount of combinations of those bits.
So for an unsigned char (1 byte) it would be 2^8 or (2*2*2*2*2*2*2*2)=256 (0-255). BTW a signed value uses 1 bit as the sign bit.
• 04-13-2006
OnionKnight
Have two variables of each type, one to be incremented and one to hold the previous value. Then make a loop that increments the variable by one and stores it's value in the other variable before incrementing. Then check if the values are the same (which means it has hit a maximum/minimum) and break the loop at that point.
Do another loop for signed integers which decrements instead to get the minimum value.
• 04-13-2006
jafet
Hint: use the fact that many types are represented, stored and manipulated as powers of 2. Thus the limits must also be a power of 2.
• 04-14-2006
vinit
Quote:

Hint: use the fact that many types are represented, stored and manipulated as powers of 2. Thus the limits must also be a power of 2.
Jafet is right, just keep in mind about sign & unsigned
e.g. if int is tored in two bytes then its range is "-32768 to +32767" (signed int) i.e 2^15
if unsigned int then its "0 to 65535" i.e 2 ^ 16