Compute upper and lower limit of data types.

hello all,
am stuck with ?. how to compute limits of char, int, float. i have done it simply displaying values from <limits.h> but i wan to calculate it.. its absolutley not a homework.
thank you.

For an unsigned type: min is 0, max is all bits set to 1
For a signed type: min is MSB set to 1, the rest to 0, max is the inverse.

Using that information, sizeof(), and bit-shifts you should be able to compute that on any platform.

got it, will do it.
thanx a lot for reply.

Hi.

For arithmetic properties, see also Enquire: Everything you wanted to know about your C Compiler and Machine, but didn't know who to ask ... cheers, drl

float and double are different from what pludi described for integers. IEEE-754 float is common but there is no single format for fp arithmetic operations across all platforms.

For IEEE-754 See:
IEEE Standard 754 Floating-Point

for ex. i want to compute MAX limit for a char why cant i do like this.

char x = NULL       // all bits set to zero
printf("%d",~x)       //~ gives all bits 1 ie biggest value.

Because C data types are signed by default, with the MSB being the sign indicator. So if you subtract 1 from 0 you get -1, or in binary

  0000 0000 (0)
- 0000 0001 (1)
-----------
  1111 1111 (-1)

yes i got some thing.

unsigned char x = NULL; 
x = (~x) >> 1;
printf("%d",x);      // prints 255  and prints 2147483647 for unsigned int x = NULL

now will try to work out for signed and float.. thank you