C 语言实例 - 计算 int, float, double 和 char 字节大小

使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。

sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++、--等,它并不是函数。

sizeof 操作符以字节形式给出了其操作数的存储大小。

c 复制代码
#include <stdio.h>
 
int main()
{
    int integerType;
    float floatType;
    double doubleType;
    char charType;
 
    // sizeof 操作符用于计算变量的字节大小
    printf("Size of int: %ld bytes\n",sizeof(integerType));
    printf("Size of float: %ld bytes\n",sizeof(floatType));
    printf("Size of double: %ld bytes\n",sizeof(doubleType));
    printf("Size of char: %ld byte\n",sizeof(charType));
 
    return 0;
}

运行结果:

c 复制代码
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

计算 long long, long double 字节大小

c 复制代码
#include <stdio.h>
int main()
{
    int a;
    long b;
    long long c;
 
    double e;
    long double f;
 
 
    printf("Size of int = %ld bytes \n", sizeof(a));
    printf("Size of long = %ld bytes\n", sizeof(b));
    printf("Size of long long = %ld bytes\n", sizeof(c));
 
    printf("Size of double = %ld bytes\n", sizeof(e));
    printf("Size of long double = %ld bytes\n", sizeof(f));
 
    return 0;
}

运行结果:

c 复制代码
Size of int = 4 bytes 
Size of long = 8 bytes
Size of long long = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
相关推荐
leaves falling13 分钟前
冒泡排序(基础版+通用版)
数据结构·算法·排序算法
l1t34 分钟前
在arm64 Linux系统上编译tdoku-lib的问题和解决
linux·运维·服务器·c语言·cmake
C雨后彩虹36 分钟前
无向图染色
java·数据结构·算法·华为·面试
secondyoung37 分钟前
Git使用:rebase用法
c语言·经验分享·git·vscode
坚持就完事了43 分钟前
扫描线算法
算法
鱼跃鹰飞1 小时前
Leetcode尊享面试100题:252. 会议室
算法·leetcode·面试
程序员-King.1 小时前
二分查找——算法总结与教学指南
数据结构·算法
Zevalin爱灰灰1 小时前
现代控制理论——第三章 线性控制系统的能控性和能观性
线性代数·算法·现代控制
kklovecode1 小时前
C语言之头文件,宏和条件编译
c语言·开发语言·算法
Xの哲學1 小时前
Linux自旋锁深度解析: 从设计思想到实战应用
linux·服务器·网络·数据结构·算法