变量所占字节

以下程序输出各种c语言数据类型在内存中所占字节

#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>

int main()
{
	/*c语言内置类型*/
	printf("the size of char is: %d\n",     sizeof(char));     //1
	printf("the size of short is: %d\n",    sizeof(short));    //2
	printf("the size of short int is: %d\n",sizeof(short int));//2
	printf("the size of int is: %d\n",     sizeof(int));      //vc:4  tc:2
	printf("the size of long int: is %d\n",sizeof(long int)); //4

//	printf("the size of long long int is: %lld\n",sizeof(long long int)); //4
//	cout<<sizeof(long long)<<endl;  //vc不支持long long

	printf("the size of float is: %d\n",   sizeof(float));    //4
	printf("the size of double is: %d\n",  sizeof(double));   //8

	/*字符串以及字符串数组*/
	char a[]={'a','b','c','d','e'};
	printf("size of a[] is:%d\n",      sizeof(a));//5

	char b[] = "zbcd";
	printf("size od b[] is %d:\n",      sizeof(b));//5

	char c[][3] = {{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
	printf("size of c[][] is %d:\n",    sizeof(c));//12
	printf("size of c[0] is %d:\n",     sizeof(c[0]));//3
	printf("size of c[0][0] is %d:\n",  sizeof(c[0][0]));//1

	/*指针所占字节数*/
	char *p = NULL;
	printf("size of char *p is %d:\n",  sizeof(p));//4
	printf("size of char * is %d:\n",   sizeof(char *));//4
	printf("size of int * is %d:\n",    sizeof(int *));//4
	printf("size of float * is %d:\n",  sizeof(float *));//4
	printf("size of double * is %d:\n", sizeof(double *));//4


	/*字符数组所占字节*/
	char d[][5] = {"abcc","deff","ghii","jkll"};
	printf("size of d[0] is %d:\n",      sizeof(d[0]));      //输出某一行所占字节
	printf("size od d[0][0] is %d:\n",   sizeof(d[0][0]));//输出某一元素所占字节

	system("pause");

	return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值