欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第一节 很久不见---变量

程序员文章站 2022-05-31 20:30:30
...

基本数据所占内存的大小:

#include<stdio.h>

void Interger_type()
{
	//整型
	char c = 'c';
	short st = 1;
	int i = 2;
	long l = 3;
	//无符号整型
	unsigned char uc = 'k';
	unsigned short ust = 4;
	unsigned int ui = 5;
	unsigned long ul = 6;
	
	printf("*****Interger_type*******\n");
	printf("c = %d ,char = %d\n",sizeof(c),sizeof(char)); // 1 , 1
	printf("st = %d ,short = %d\n",sizeof(st),sizeof(short)); // 2 , 2
	printf("i = %d ,int = %d\n",sizeof(i),sizeof(int));   // 4 , 4
	printf("l = %d ,long = %d\n",sizeof(l),sizeof(long));  // 8 , 8
	printf("uc = %d ,unsigned char = %d\n",sizeof(uc),sizeof(unsigned char)); // 1 , 1
	printf("ust = %d ,unsigned short = %d\n",sizeof(ust),sizeof(unsigned short));  // 2 , 2
	printf("ui = %d ,unsigned int = %d\n",sizeof(ui),sizeof(unsigned int));   // 4 , 4
	printf("ul = %d ,unsigned long = %d\n",sizeof(ul),sizeof(unsigned long));  // 8 , 8
	
	printf("\n");
}
void Float_type()
{
	// 浮点型
	float f = 3.401f;
	double d = 3.1415926;
	long double ld = 3.12434455;
	
	//unsigned float uf = 9.8765432; //error 浮点型中的unsigned、short、signed关键字都不适用
	
	printf("*****Float_type*******\n");
	printf("f = %d ,float = %d\n",sizeof(f),sizeof(float));  // 4 , 4 
	printf("d =%d ,double = %d\n",sizeof(d),sizeof(double)); // 8 , 8
	printf("ld =%d ,long double  = %d\n",sizeof(ld),sizeof(long double)); //16 , 16 不同的机器的值会不相同
	//printf("unsigned float = %d\n",sizeof(uf)); //error
	
	printf("\n");
	
}
void Struct_type()
{
	struct TagSIMPLE {
		int i;
		char c;
		short st;
	};
	struct TagSIMPLE tagS = {0,'c',2};
	struct TagSIMPLE* pTagS = &tagS;
	typedef struct {
		int i;
		float f;
		short st;
	} Simple;
	Simple Ts;
	
	printf("*****Struct_type*******\n");
	printf("%d ,%d\n",sizeof(tagS),sizeof(pTagS)); // 8 , 8 
	printf("%d, %c, %d\n",tagS.i,tagS.c,tagS.st);   // 0 , c , 2
	printf("%d ,%d\n",sizeof(Ts),sizeof(Simple*));  // 12 , 8 
	printf("%d, %c, %d\n",Ts.i,Ts.f,Ts.st); // 717254944 , ( , 2147483642 随机值
	printf("\n");
}
void Point_type()
{
	char c = 'c';
	char* pChar = &c;
	short st = 1;
	short* pShort = &st;
	int i = 2;
	int* pInt;
	pInt = &i;
	long l = 3;
	long* pLong;
	pLong = &l;
	
	printf("******Point_type*******\n");
	printf("%d, %d, %d\n",sizeof(pChar),sizeof(char*),sizeof(&c)); // 8 , 8 , 8 
	printf("%p, %p\n",pChar,&c);  // 0x7ffe2ac0712f , 0x7ffe2ac0712f
	
	printf("\n");
}
int main()
{
	Interger_type();
	Float_type();
	Point_type();
	Struct_type();
	return 0;
}

 在Centos上编译结果:

 第一节 很久不见---变量