C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C 计算 int、float、double 和 char 大小的程序

C 程序查找 int、float、double 和 char 的大小

在本例中,您将学习使用 sizeof 运算符评估每个变量的大小。
要理解此示例,您应该了解以下C 编程 主题:
C 数据类型 C 变量、常量和文字 C 输入输出(I/O)
sizeof(variable) 运算符计算变量的大小。并且,为了打印 sizeof 返回的结果,我们使用 %lu%zu 格式说明符。

查找变量大小的程序

#include<stdio.h>
int main() {
    int intType;
    float floatType;
    double doubleType;
    char charType;
    // sizeof evaluates the size of a variable
    printf("Size of int: %zu bytes\n", sizeof(intType));
    printf("Size of float: %zu bytes\n", sizeof(floatType));
    printf("Size of double: %zu bytes\n", sizeof(doubleType));
    printf("Size of char: %zu byte\n", sizeof(charType));
    
    return 0;
}
输出
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
在这个程序中,声明了 4 个变量 intTypefloatTypedoubleTypecharType
然后,使用 sizeof 运算符计算每个变量的大小。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4