#include #define RANGE(type, name) \ { \ type minval, maxval, bit ; \ unsigned bits ; \ char *format ; \ \ /* Measure size of data type */ \ bits = 1 ; bit = (type) 1 ; \ while (bit <<= 1) bits++ ; \ \ /* Signed or unsigned? (2's complement!) */ \ if ((type) (1L << (bits - 1)) > 0) \ { \ format = "%12lu" ; /* unsigned */ \ minval = (type) 0 ; \ } \ else \ { \ format = "%12ld" ; /* signed */ \ minval = (type) 1 << (bits - 1) ; \ } \ \ /* Assume two's complement */ \ maxval = minval - 1 ; \ \ printf("%20s (%2u bits): ", name, bits) ; \ printf(format, (long) minval) ; \ printf(" to ") ; \ printf(format, (long) maxval) ; \ printf("\n") ; \ } int main() { printf("\nTesting range of UNSIGNED ints ...\n\n") ; RANGE(unsigned char, "unsigned char" ) ; RANGE(unsigned short int, "unsigned short int" ) ; RANGE(unsigned int, "unsigned int" ) ; RANGE(unsigned long int, "unsigned long int" ) ; printf("\nTesting range of SIGNED ints ...\n\n") ; RANGE(signed char, "signed char" ) ; RANGE(signed short int, "signed short int" ) ; RANGE(signed int, "signed int" ) ; RANGE(signed long int, "signed long int" ) ; return 0 ; }