- 一、简介
- 二、字节数对比
- 三、输出不同类型格式
- 四、常用类型简写总结
本文主要介绍C语言常见数据类型字节数、不同类型输出格式和c程序中不同类型的简写总结。c语言中,常见数据类型的字节数和机器字长及编译器有关系,所以,int、long int、short int的宽度都可能随编译器而异。但有几条铁定的原则(ANSI/ISO制订的):
sizeof(short int)<=sizeof(int) ; sizeof(int)<=sizeof(long int); short int至少应为16位(2字节) ; long int至少应为32位。二、字节数对比
| 数据类型 编译器位数 | 16位 | 32位 | 64位 |
|---|---|---|---|
| char | 1个字节 | 1个字节 | 1个字节 |
| char* (即指针,一个地址所占位数) | 2个字节 | 4个字节 | 8个字节 |
| short int | 2个字节 | 2个字节 | 2个字节 |
| int | 2个字节 | 4个字节 | 4个字节 |
| unsigned int | 2个字节 | 4个字节 | 4个字节 |
| float | 4个字节 | 4个字节 | 4个字节 |
| double | 8个字节 | 8个字节 | 8个字节 |
| long | 4个字节 | 4个字节 | 8个字节 |
| long long | 8个字节 | 8个字节 | 8个字节 |
| unsigned long | 4个字节 | 4个字节 | 8个字节 |
%p 可以匹配对应类型的指针地址,输出以16进制表示,会自带前缀0x %x 可以对应int类型的指针,输出以16进制输出,不会自带前缀0x %u 可以打印unsigned int类型 %d 可以打印int类型 %o 以八进制格式打印数据类型 %ho 以八进制格式输出short类型
注意:
如果系统的int和long类型具有同样的长度,使用%d就可以打印long数值,但是这会给程序移植到其他系统(这两种数据类型的长度不一样的系统)带来麻烦,所以建议使用%ld打印long数值。在x和o符号前也可以使用l前缀,因此%lx表示以十六进制格式打印长整数,%lo表示以八进制格式打印长整数。请注意,尽管在C中常量后缀可以使用大写和小写,但格式说明符只能使用小写字母。
C语言中还有其他几种printf()格式。首先,可以对short类型使用h前缀,因此%hd 表示以十进制显示short整数,%ho表示以八进制显示short 整数。h和 l 前缀都可以同u结合使用表示无符号整数。例如,%lu 表示打印unsigned long类型。
typedef unsigned long uint64_t; typedef unsigned int uint32_t; typedef unsigned short uint16_t; typedef unsigned char uint8_t; typedef long long int64_t; typedef int int32_t; typedef short int16_t; typedef char int8_t; typedef signed char s8; typedef unsigned char u8; typedef signed short s16; typedef unsigned short u16; typedef signed int s32; typedef unsigned int u32; typedef signed long s64; typedef unsigned long u64;



