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
相关推荐
哈哈哼嘿3 分钟前
C语言:函数指针,数组,结构体
c语言
CoovallyAIHub9 分钟前
YOLOv8-SMOT:基于切片辅助训练与自适应运动关联的无人机视角小目标实时追踪框架
深度学习·算法·计算机视觉
CoovallyAIHub20 分钟前
全景式综述|多模态目标跟踪全面解析:方法、数据、挑战与未来
深度学习·算法·计算机视觉
像风一样自由202024 分钟前
五种算法详解(SVM / Logistic Regression / kNN / Random Forest / HistGradientBoosting)
算法·随机森林·支持向量机
7hhhhhhh34 分钟前
自学嵌入式第二十四天:数据结构(4)-栈
数据结构
一起努力啊~41 分钟前
算法题打卡力扣第34题:在排序数组中查找元素的第一个和最后一个位置(mid)
数据结构·算法·leetcode
晨曦5432101 小时前
图(Graph):关系网络的数学抽象
开发语言·算法·php
1白天的黑夜12 小时前
链表-143.重排链表-力扣(LeetCode)
数据结构·leetcode·链表
Ustinian_3102 小时前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
cwplh3 小时前
Manacher(马拉车算法)详解
算法