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

相关推荐

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

    2024-04-26 14:40:03       34 阅读
  2. C++|char*大小端转换

    2024-04-26 14:40:03       43 阅读
  3. c语言大小字母转换程序

    2024-04-26 14:40:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 14:40:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 14:40:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 14:40:03       82 阅读
  4. Python语言-面向对象

    2024-04-26 14:40:03       91 阅读

热门阅读

  1. Redis系列:HyperLogLog实现海量数据基数统计

    2024-04-26 14:40:03       42 阅读
  2. 大文件的分片上传和断点上传

    2024-04-26 14:40:03       40 阅读
  3. js将后端返回的blob请求下载

    2024-04-26 14:40:03       36 阅读
  4. Haystack

    Haystack

    2024-04-26 14:40:03      37 阅读
  5. SpringBoot集成rabbitMq

    2024-04-26 14:40:03       40 阅读
  6. python的练习

    2024-04-26 14:40:03       26 阅读
  7. 多线程(74)分段锁

    2024-04-26 14:40:03       35 阅读
  8. Ubuntu20.04下 Django框架搭建、部署、上线项目

    2024-04-26 14:40:03       34 阅读
  9. 一文掌握python面向对象魔术方法(二)

    2024-04-26 14:40:03       32 阅读
  10. C++ 中容器的种类

    2024-04-26 14:40:03       37 阅读
  11. 静态网卡配置centos、kali、ubantu

    2024-04-26 14:40:03       28 阅读