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 ",sizeof(char)); printf("%d ",sizeof(short)); printf("%d ",sizeof(int)); printf("%d ",sizeof(long)); printf("%d ",sizeof(float)); printf("%d ",sizeof(double)); printf("%d ",sizeof(ch)); printf("%d ",sizeof(a)); printf("%d ",sizeof(b)); printf("%d ",sizeof(c)); printf("%d ",sizeof(d)); printf("%d ",sizeof(e)); system("pause"); return 0; }