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
相关推荐
cellurw19 分钟前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http
..过云雨43 分钟前
03.【Linux系统编程】基础开发工具1(yum软件安装、vim编辑器、编辑器gcc/g++)
linux·c语言·笔记·学习
青草地溪水旁1 小时前
Linux 高性能 I/O 事件通知机制的核心系统调用—— `epoll_ctl`
linux·c语言·c++
靠近彗星1 小时前
2.1线性表
数据结构
island13142 小时前
【Redis#9】其他数据结构
数据结构·数据库·redis
行走的bug...2 小时前
用图论来解决问题
算法·图论
nsjqj3 小时前
数据结构:优先级队列(堆)
数据结构
岁忧3 小时前
(LeetCode 每日一题) 3541. 找到频率最高的元音和辅音 (哈希表)
java·c++·算法·leetcode·go·散列表
pusue_the_sun3 小时前
每日算法题推送
算法·双指针
JasmineX-13 小时前
数据结构——顺序表(c语言笔记)
c语言·开发语言·数据结构·笔记