变量在内存中所占空间大小

测试程序:

 1 #include 
 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 }

 

打印结果:

变量在内存中所占空间大小_第1张图片

转载于:https://www.cnblogs.com/n-a-n-c/archive/2013/04/15/3022653.html

你可能感兴趣的:(变量在内存中所占空间大小)