总结:
1.C规定float为4个字节, 小数点后精度为6位
2.C规定double占用8个字节, 至少精确到小数点后13位
3.long double在32位系统和64位系统中均占用16个字节
4.占4字节或者8字节, 不同系统实现不同
5.bool类型在系统中其实是int类型
6.字符型数据其实在预编译前被翻译为整形数字
7.浮点类型默认为double
8.浮点常量后面加L表示long double
9.常量后面加ULF ulf的目的是:让编译器以unsigned int的方式存储常量,下同
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 泛型表达式(需要gcc 4.7以上版本, 编译时加上-std=c11)
#define GENERIC(x) _Generic((x), \
long double:"long double",\
bool:"bool",\
char:"char", \
int:"int", \
float:"float", \
double:"double", \
long int:"long int", \
long long int:"long long int", \
unsigned char:"unsigned char", \
unsigned int:"unsigned int", \
unsigned long int:"unsigned long int", \
unsigned long long int:"unsigned long long int",\
char *: "string",\
default:"undefined")
#define PRINT_GENERIC(x) printf("%s\n", GENERIC(x))
int main(int argc, char *argv[])
{
printf("%d\n", sizeof(float)); // 4 C规定float为4个字节, 小数点后精度为6位
printf("%d\n", sizeof(double)); // 8 C规定double占用8个字节, 至少精确到小数点后13位
printf("%d\n", sizeof(long double)); // 16 long double在32位系统和64位系统中均占用16个字节
printf("%d\n", sizeof(long int)); // 4 占4字节或者8字节, 不同系统实现不同。
printf("%d\n", sizeof(long long int)); // 8
printf("=========================================\n");
printf("%s\n", GENERIC(true)); // int bool
printf("%s\n", GENERIC('A')); // int, 预编译前被翻译为整形数字
printf("%s\n", GENERIC("ABC")); // string
printf("%s\n", GENERIC(1)); // int
printf("%s\n", GENERIC(3.14)); // double 浮点类型默认为double
printf("%s\n", GENERIC(1.11e+2)); // double
printf("=========================================\n");
printf("%s\n", GENERIC(1U)); // unsigned int 让编译器以unsigned int的方式存储常量,下同
printf("%s\n", GENERIC(1L)); // long int
printf("%s\n", GENERIC(1UL)); // unsigned long int
printf("%s\n", GENERIC(1ULL)); // unsigned long long int
printf("=========================================\n");
printf("%s\n", GENERIC(3.14f)); // float
printf("%s\n", GENERIC(1.11e+2f)); // float
printf("%s\n", GENERIC(3.14L)); // long double 浮点常量后面加L表示long double
printf("%s\n", GENERIC(1.11e-2L)); // long double
printf("=========================================\n");
float a=32000.0;
double b = 2.14e5;
long double c = 5.32e-5;
float d = 0x0.aafP3; // 16进制浮点数(c11)
long double e = 0x0.a1fP3;
printf("%f, %e\n", a,a); // 32000.000000 3.200000e+004
printf("%f, %e\n", b,b); // 214000.000000 2.140000e+5
printf("%Lf, %Le\n", c,c); // 0.000053 5.320000e-05
printf("%A, %LA\n", d,e); // 0X1.55EP+2, 0XA.1FP-1
return 0;
}