sizeof关键字使用方法及实例演示
程序员文章站
2024-03-23 15:32:58
...
sizeof关键字使用方法及实例演示
首先强调,sizeof 不是函数, 是关键字。主要用来求一个变量、类型的大小。 返回一个 无符号整数。 使用 %u 接收返回值。
方法1: sizeof(类型名)
sizeof(int)
方法2: sizeof (变量名)
int a = 20;
sizeof(a);
【了解】: sizeof 变量名/类型名
类型名 举例1: sizeof int
变量名 举例2: sizeof a
用 sizeof 关键字求取各个数据类型的大小(单位默认为字节)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 1024 // 定义常量
int main(void)
{
int a = 3;
short b = 4;
long c = 5; // 5L 5l
long long d = 6; // 5LL 5ll
printf("sizeof(int) = %d\n", sizeof(int));
printf("sizeof(short) = %d\n", sizeof(short));
printf("sizeof(long) = %d\n", sizeof(long));
printf("sizeof(long long) = %d\n", sizeof(long long));
printf("--------------------------------------\n");
unsigned int aun = 3; // 3u
unsigned short bun = 4; // 3u
unsigned long cun = 5; // 3lu
unsigned long long dun = 6; // 3llu
printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int)); // aun
printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
printf("sizeof(unsigned long long) = %d\n", sizeof(unsigned long long));
}
运行结果