C语言中,int, char和short int三种类型变量所占用的内存大小是

如题所述

C语言中,int, char和short int三种类型变量所占用的内存大小因机器的不同有所不同。

一般在32位及以上机器上,int占四字节,char占一字节,short占2字节。

可以通过sizeof()()命令获取本机器上的各类型的占内存的大小,参考代码如下:

#include <stdio.h>
void main()
{
    printf("sizeof int=%d\n", sizeof(int) );
    printf("sizeof short=%d\n", sizeof(short) );
    printf("sizeof char=%d\n", sizeof(char) );
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-31
int 四个字节,char一个字节,short int 两个字节

vc++编译器下,可用以下代码测试:
#include<iostream.h>
int main()
{
int x=0;short int y=0;long int z=0;
char c='\0';
bool b;
float f=0.0;double d=0.0;long double l=0.0;
cout<<"bool :"<<sizeof(bool)<<endl;
cout<<"char :"<<sizeof(char)<<endl;
cout<<"short int :"<<sizeof(short int )<<endl;
cout<<"int :"<<sizeof(int)<<endl;
cout<<"long int :"<<sizeof(long int)<<endl;
cout<<"float :"<<sizeof(float)<<endl;
cout<<"double :"<<sizeof(double)<<endl;
cout<<"long double :"<<sizeof(long double)<<endl;
return 0;
}
第2个回答  2012-05-30
似乎是跟编译器有关,一般int 4个字节,short int 2字节,char 1个字节
第3个回答  2012-06-01
分别占4,1,2,int的大小与编译器有关,有时可能也占2字节
第4个回答  2012-05-30
在vc中是4,1,2
不同的编译器可能占不同内存本回答被提问者采纳