C 数据类型
原生数据类型
整数类型
大小范围 8bit 到 32bit, 某些平台支持64bit, C99扩展支持64bit.
- signed char : 1byte大小 ,有符号,范围 [-128,127].
- unsigned char : 1byte大小, 无符号, 范围 [0 ,256].
- char : 1byte, 可能是signed char 或 unsigned char, 依懒系统实现。习惯上用char 表示字符,比如 ‘a’, ‘\n’.
- short int: 2byte大小, 有符号,范围 [-32768,32767].
- unsigned short int: 2bye 大小,无符号,大小 [0 , 65535].
- int : 4byte 大小, 有符号,范围 [-231 , 231 -1].
- unsigned int: 4byte 大小,无符号,范围【0,232】.
- long int : 32byte (32位系统 ) or 64byte(64位系统), 有符号。
- unsigned long int : 32byte (32位系统 ) or 64byte(64位系统), 无符号。
实数类型
实数在系统存在一定的精度,即不是无限精度的。考虑比较两个实数用 “==” 时是不合理的,最好考虑在一定精度范围内比较,比如 差值为0.00001时 即为相等。
- float : 大小范围 【FLT_MIN,FLT_MAX】,依赖系统实现。
- double: 大小范围 【DBL_MIN,DBL_MAX】,依赖系统实现.
- long double: 大小范围 【LDBL_MIN,LDBL_MAX】,依赖系统实现。
float real_interger = 0.0001f;
double double_interger = 123.1234;
复数类型
- C99 支持复数类型, float _Complex,double _Complex,long double _Complex。complex.h 头文件中增加complex 关键字,可以使复数类型更简单,即 float complex ,double complex.
float _Complex stander_complex = 1.0 + 3.0I
#include <complex.h>
float complext comlex = 1.0 + 3.0I
- GNU扩展的复数类型: 关键字__complex__ ,可以与原生类型组合使用。
key:
__complex__ float
__complex__ double
__complex__ long double
__complex__ int
example :
__complex__ float gnu_complex = 3.0 + 4.0i;
float gnu_complex_real = __real__ gnu_complex; /* 3.0 */
float gnu_complex_imag = __imag__ gnu_complex; /* 4.0 */
__complex__ int gnu_complex_interger = 3 + 4i;
int gnu_complex_real = __real__ gnu_complex_interger; /* 3 */
int gnu_complex_imag = __imag__ gnu_complex_interger; /* 4 */
枚举 (enumerations)
枚举用于存储整型常量值,并且可以用名字引用常量值,默认为signed int 型,GCC 编译器支持 -fshort-enums 将类型修改为shot 型(不建议使用)。
- 定义枚举数据:
枚举在编译时值时确定取值,不能修改。
/*
* 简单的枚举定义,A = 0,B=1,c=2,大小写任意
*/
enum