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
相关推荐
小明说Java2 分钟前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20191 小时前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826522 小时前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme3 小时前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5123 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8243 小时前
计数if|
算法
a伊雪4 小时前
c++ 引用参数
c++·算法
程序员Jared4 小时前
深入浅出C语言——程序环境和预处理
c语言
应茶茶4 小时前
从 C 到 C++:详解不定参数的两种实现方式(va_args 与参数包)
c语言·开发语言·c++
Data_agent4 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法