C 语言实例 – 计算 int, float, double 和 char 字节大小

C语言教程评论阅读模式

C 语言实例 - 计算 int, float, double 和 char 字节大小

使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。

sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++、--等,它并不是函数。

sizeof 操作符以字节形式给出了其操作数的存储大小。

#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;
}

运行结果:

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

计算 long long, long double 字节大小

#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;
}

运行结果:

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

继续阅读
weinxin
我的微信
运营不易,
感谢支持!
公式库网
  • 本文由 发表于 2021年12月4日 22:10:37
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-yuyanshili-jisuan-int-float-double-he-char-zijiedaxiao.html
C语言教程

C 标准库 –

C 标准库 - <time.h> 简介 time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量 下面是头文件 time.h 中定义的变量类型: ...
C语言教程

C 标准库 –

C 标准库 - <string.h> 简介 string .h 头文件定义了一个变量类型、一个宏和各种操作字符数组的函数。 库变量 下面是头文件 string.h 中定义的变量类型...
C语言教程

C 标准库 –

C 标准库 - <stdlib.h> 简介 stdlib .h 头文件定义了四个变量类型、一些宏和各种通用工具函数。 库变量 下面是头文件 stdlib.h 中定义的变量类型: ...
C语言教程

C 标准库 –

C 标准库 - <stdio.h> 简介 stdio .h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出。 库变量 下面是头文件 stdio.h 中定义的变量类型: ...

发表评论