×
C 语言教程C 简介C 环境设置C 程序结构C 基本语法C 数据类型C 变量C 常量C 存储类C 运算符C 判断C 循环C 函数C 作用域规则C 数组C 指针C 函数指针与回调函数C 字符串C 结构体C 共用体C 位域C typedefC 输入 & 输出C 文件读写C 预处理器C 头文件C 强制类型转换C 错误处理C 递归C 可变参数C 内存管理C 命令行参数C 语言实例C 经典100例

C 标准库

C 标准库 - 参考手册C 标准库 - <assert.h>C 标准库 - <ctype.h>C 标准库 - <errno.h>C 标准库 - <float.h>C 标准库 - <limits.h>C 标准库 - <locale.h>C 标准库 - <math.h>C 标准库 - <setjmp.h>C 标准库 - <signal.h>C 标准库 - <stdarg.h>C 标准库 - <stddef.h>C 标准库 - <stdio.h>C 标准库 - <stdlib.h>C 标准库 - <string.h>C 标准库 - <time.h>

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


C 语言实例C 语言实例


使用 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

C 语言实例C 语言实例


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)