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
相关推荐
是小Y啦6 分钟前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode
liuyang-neu16 分钟前
力扣 42.接雨水
java·算法·leetcode
一名路过的小码农21 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
y_dd23 分钟前
【machine learning-12-多元线性回归】
算法·机器学习·线性回归
m0_6312704023 分钟前
标准c语言(一)
c语言·开发语言·算法
万河归海42824 分钟前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
小周的C语言学习笔记28 分钟前
鹏哥C语言36-37---循环/分支语句练习(折半查找算法)
c语言·算法·visual studio
y_dd29 分钟前
【machine learning-七-线性回归之成本函数】
算法·回归·线性回归
小魏冬琅1 小时前
K-means 算法的介绍与应用
算法·机器学习·kmeans
凌肖战1 小时前
力扣上刷题之C语言实现(数组)
c语言·算法·leetcode