内存空间运算符sizeof
每⼀种数据类型都有⾃⼰的⻓度,使⽤不同的数据类型,能够创建出⻓度不同的变量,变量⻓度的不
同,存储的数据范围就有所差异,存储单位是字节,一个字节为8个bit。
size_t sizeof( a variable or a type (including aggregate types) )
sizeof是⼀个关键字,也是操作符,专门是⽤来计算sizeof操作符数的类型⻓度的,单位是字节。
sizeof操作符的操作数可以是数据类型,也可是变量、字面值。
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
sizeof(类型);
sizeof 变量或字面值; //变量或字面值可以省略括号
如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
double a = 0;
printf("%zd\n", sizeof(int));
printf("%zd\n", sizeof(char*));
printf("%zd\n", sizeof a); //变量或字面值可以省略括号
return 0;
}
运行结果:
4
4
8
基本数据类型和指针类型
int :4个字节
char :1个字节
float :4个字节
double :8个字节
…
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
printf("sizeof(int):%zd\n", sizeof(int));
printf("sizeof(char):%zd\n", sizeof