[置顶] sizeof()和c++中变量们
sizeof()就像一个管家,知道家里的所有姑娘的身材一样,会为它们量身订做衣服,好让他们变得更加美丽动人,
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{ // 右侧为结果
cout<<"char"<<sizeof(char)<<endl; // 1
cout<<"unsigned char"<<sizeof(unsigned char)<<endl; //1
cout<<"signed char"<<sizeof(signed char)<<endl; //1
cout<<"int "<<sizeof(int)<<endl; //4
cout<<"unsigned int "<<sizeof(unsigned int )<<endl;//4
cout<<"signed int "<<sizeof(signed int)<<endl; //4
cout<<"short int "<<sizeof(short int)<<endl; //2
cout<<"long int"<<sizeof(long int )<<endl; //4
cout<<"double"<<sizeof(double)<<endl; //8
cout<<"float"<<sizeof(float)<<endl; //4
cout<<"string"<<sizeof(string)<<endl; //28
//_sleep(20);
cout<<endl;
int w;
cin>>w;
return 0;
}
从结果当中我们可以知道:
char 类型占 1个字节
unsigned char 类型占1个字节
signed char 类型占一个1字节
int 类型占4个字节
signed int 占 4个字节
unsigned int 类型占4个字节
short int 类型占2个字节
long int 类型占4个字节
double 类型占8个字节
float 类型占4个字节
string 类型张28个字节