测试程序:
1 #include <stdio.h> 2 3 #define RANGE(type) \ 4 if ( (type)((type)0 - 1) > 0 ) \ 5 { \ 6 type m = 0; \ 7 m = m - 1; \ 8 printf("%s:\t %d Bytes [%.0f µ½ %.0f] \n", #type, sizeof(type), (double)0, (double)m ); \ 9 } \ 10 else \ 11 { \ 12 double c = (1 << ( sizeof(type)*8-1 ) ); \ 13 printf( "%s:\t\t %d Bytes [%.0f µ½ %.0f]\n", #type, sizeof(type), (double)c*-1, (double)c-1 ); \ 14 } 15 16 int main(void) 17 { 18 RANGE(char); 19 RANGE(unsigned char); 20 printf("\n"); 21 RANGE(short); 22 RANGE(unsigned short); 23 printf("\n"); 24 RANGE(int); 25 RANGE(unsigned int); 26 printf("\n"); 27 RANGE(long); 28 RANGE(unsigned long); 29 30 return 0; 31 }
打印结果: