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
相关推荐
ChiaWei Lee7 分钟前
【C语言】深入理解指针(三):C语言中的高级指针应用
c语言·开发语言
最后一个bug8 分钟前
教你快速理解linux中的NUMA节点探测是干什么用的?
linux·c语言·开发语言·arm开发·嵌入式硬件
trust Tomorrow14 分钟前
每日一题-力扣-2278. 字母在字符串中的百分比 0331
算法·leetcode
Lecea_L28 分钟前
你能在K步内赚最多的钱吗?用Java解锁最大路径收益算法(含AI场景分析)
java·人工智能·算法
Tony8830 分钟前
热题100 - 394. 字符串解码
java·算法
Lecea_L36 分钟前
🔍 找到数组里的“节奏感”:最长等差子序列
java·算法
是Dream呀38 分钟前
ResNeXt: 通过聚合残差变换增强深度神经网络
人工智能·算法
姜行运1 小时前
数据结构【链表】
c语言·开发语言·数据结构·链表
学习2年半1 小时前
53. 最大子数组和
算法
君义_noip2 小时前
信息学奥赛一本通 1524:旅游航道
c++·算法·图论·信息学奥赛