C语言基本数据类型
类型 | C语言规范 | gcc-7.4.0 x86_64 (Bytes) |
char | 一个字节 | 1 |
short | 字节数小于或等于int | 2 |
int |
| 4 |
long | 字节数大于或者等于int | 8 |
long long (C99) | 字节数大于或者等于long | 8 |
float | 至少6位有效数字,且取值范围至少10-37 ~ 1037 | 4 |
double | 至少10位有效数字,取值范围同float | 8 |
long double (C99) | 有效数字大于或等于double | 16 |
说明:
1、对于整型,如果数据类型前没有unsigned修饰编译器按有符 号数据处理,如果前面有unsigned修饰则按无符号处理。
2、浮点型的数据类型不能用unsigned修饰。
3、浮点数和浮点数或者浮点数和整数都不能用“==”判断相等,应该看两数的差值是否在一个可接受的范围。比如:fabs(a - b) < 0.001
#include <stdio.h>
int main(void)
{
printf("%-11s %-6s %s\n", "Type", "Length", "Unit");
printf("%-11s %-6ld %s\n", "char", sizeof(char), "Bytes");
printf("%-11s %-6ld %s\n", "short", sizeof(short), "Bytes");
printf("%-11s %-6ld %s\n", "int", sizeof(int), "Bytes");
printf("%-11s %-6ld %s\n", "long", sizeof(long), "Bytes");
printf("%-11s %-6ld %s\n", "long long", sizeof(long long), "Bytes");
printf("%-11s %-6ld %s\n", "float", sizeof(float), "Bytes");
printf("%-11s %-6ld %s\n", "double", sizeof(double), "Bytes");
printf("%-11s %-6ld %s\n", "long double", sizeof(long double), "Bytes");
return 0;
}
输出(gcc-7.4.0 x86_64):
Type Length Unit
char 1 Bytes
short 2 Bytes
int 4 Bytes
long 8 Bytes
long long 8 Bytes
float 4 Bytes
double 8 Bytes
long double 16 Bytes