C语言中计算变量占用内存空间
在C语言中通常用【sizeof】运算符计算变量占内存空间,如下面的例子:
#include <stdio.h> #include <stdlib.h> int main() { char ch; short a; int b; long c ; double e; float d; //赋值 ch = 'a'; a = 1; b = 2; c= 3; d = 1.5; e = 1.5; printf("%d\n",sizeof(char)); printf("%d\n",sizeof(short)); printf("%d\n",sizeof(int)); printf("%d\n",sizeof(long)); printf("%d\n",sizeof(float)); printf("%d\n",sizeof(double)); printf("%d\n",sizeof(ch)); printf("%d\n",sizeof(a)); printf("%d\n",sizeof(b)); printf("%d\n",sizeof(c)); printf("%d\n",sizeof(d)); printf("%d\n",sizeof(e)); system("pause"); return 0; }