对于c语言来说,数据类型是一个很重要的概念和知识点,它涉及到的是内存的空间,这在和硬件交互的时候是非常重要的。
K&R给出了7个数据类型相关的关键字,分别是:int、long、short、unsigned、char、float、double。后来C90增加了2个关键字,分别是:signed、void。再后来,C99又增加了3个关键字,分别是:_Bool、_Complex、_Imaginary。
整体来说,我们可以分为2个系列:分别是整数类型和浮点数类型。
下面就依次来说说了(环境:win7 64bit 、VS2015):
int(有符号整型)
- 占用的字节数:
printf("int:bytes %d;bit %d\n", sizeof(int), sizeof(int) * 8);
int:bytes 4;bit 32
- 取值范围:
printf("int:MIN %d;MAX %d\n", INT_MIN, INT_MAX);
int:MIN -2147483648;MAX 2147483647 即-2^31~2^31-1
- 格式说明符:%d(十进制)、%o(八进制)、%x(十六进制)
unsigned int(无符号整型)
- 占用的字节数:
printf("unsigned int:bytes %d;bit %d\n", sizeof(unsigned int), sizeof(unsigned int) * 8);
unsigned int:bytes 4;bit 32
- 取值范围:
printf("unsigned int:MIN is 0;MAX %u\n", UINT_MAX);
unsigned int:MIN is 0;MAX 4294967295 即0~2^32
- 格式说明符:%u(十进制)
char(有符号字符型)
- 占用的字节数:
printf("char:bytes %d;bit %d\n", sizeof(char), sizeof(char) * 8);
char:bytes 1;bit 8
- 取值范围:
printf("char:MIN %d;MAX %d\n", CHAR_MIN, CHAR_MAX);
char:MIN -128;MAX 127 即-2^7~2^7-1
- 格式说明符:%d(十进制)
unsigned char(无符号字符型)
- 占用的字节数:
printf("unsigned char:bytes %d;bit %d\n", sizeof(unsigned char), sizeof(unsigned char) * 8);
unsigned char:bytes 1;bit 8
- 取值范围:
printf("unsigned char:MIN is 0;MAX %d\n", UCHAR_MAX);
unsigned char:MIN is 0;MAX 255 即0~2^8-1
- 格式说明符:%d(十进制)
short(有符号短整型)
- 占用的字节数:
printf("short:bytes %d;bit %d\n", sizeof(short), sizeof(short) * 8);
short:() 2;bit 16
- 取值范围:
printf("short :MIN %hd;MAX %hd\n", SHRT_MIN, SHRT_MAX);
short :MIN -32768;MAX 32767 即-2^15~2^15-1
- 格式说明符:%hd(十进制)
unsigned short(无符号短整型)
- 占用的字节数:
printf("unsigned short:bytes %d;bit %d\n", sizeof(unsigned short), sizeof(unsigned short) * 8);
unsigned short:bytes 2;bit 16:bytes 2;bit 16
- 取值范围:
printf("unsigned short:MIN is 0;MAX %d\n", USHRT_MAX);
unsigned short:MIN is 0;MAX 65535 即0~2^16-1
- 格式说明符:%hd(十进制)
long(有符号长整型)
- 占用的字节数:
printf("long:bytes %d;bit %d\n", sizeof(long), sizeof(long) * 8);
long:bytes 4;bit 32
- 取值范围:
printf("long :MIN %ld;MAX %ld\n", LONG_MIN, LONG_MAX);
long :MIN -2147483648;MAX 2147483647 即-2^31~2^31-1
- 格式说明符:%ld
unsigned long(无符号长整型)
- 占用的字节数:
unsigned long:bytes %d;bit %d\n", sizeof(unsigned long), sizeof(unsigned long) * 8);
unsigned long:bytes 4;bit 32
- 取值范围:
printf("unsigned long:MIN is 0;MAX %lu\n", ULONG_MAX);
unsigned long:MIN is 0;MAX 4294967295 即0~2^32-1
- 格式说明符:%lu
long long(有符号长长整型)
- 占用的字节数:
printf("long long :bytes %d;bit %d\n", sizeof(long long), sizeof(long long) * 8);
long long :bytes 8;bit 64
- 取值范围:
printf("long long :MIN %lld;MAX %llu\n", LLONG_MIN, LLONG_MAX);
long long :MIN -9223372036854775808;MAX 9223372036854775807 即-2^63~2^63-1
- 格式说明符:%llu
unsigned long long(有符号的长长整型)
- 占用的字节数:
printf("unsigned long long:bytes %d;bit %d\n", sizeof(unsigned long long), sizeof(unsigned long long) * 8);
unsigned long long:bytes 8;bit 64
- 取值范围:
printf("unsigned long long:MIN is 0;MAX %llu\n", ULLONG_MAX);
unsigned long long:MIN is 0;MAX 18446744073709551615 即0~2^64-1
- 格式说明符:%llu
float (单精度浮点型)
- 占用的字节数:
printf("float :bytes %d;bit %d\n", sizeof(float), sizeof(float) * 8);
float :bytes 4;bit 32
- 取值范围:C标准规定,float类型至少能表示6位有效数字,范围至少为10^-37~10^37
诸如:22.333444这样的数。其中8bit用于表示指数及其符号,24bit表示尾数或有效数字及其符号。
double (双精度浮点型)
- 占用的字节数:
printf("float :bytes %d;bit %d\n", sizeof(float), sizeof(float) * 8);
double :bytes 8;bit 64
- 取值范围:C标准规定,double类型至少能表示10位有效数字。一些系统将多出来的32bit都来表示尾数部分,这就至少有13位有效数字了,这也超出了c规定的最小标准了。
long double
- 占用的字节数:
printf("long double :bytes %d;bit %d\n", sizeof(long double), sizeof(long double) * 8);
long double :bytes 8;bit 64
取值范围:理论上是比double有更高的精度,但是c只保证long double至少同double 一样的精度
源码:
#include <stdio.h> #include<limits.h> int main() { printf("int:bytes %d;bit %d\n", sizeof(int), sizeof(int) * 8); //int:bytes 4;bit 32 printf("int:MIN %d;MAX %d\n", INT_MIN, INT_MAX); // int:MIN -2147483648;MAX 2147483647 printf("unsigned int:bytes %d;bit %d\n", sizeof(unsigned int), sizeof(unsigned int) * 8); //unsigned int:bytes 4;bit 32 printf("unsigned int:MIN is 0;MAX %u\n", UINT_MAX); //unsigned int:MIN is 0;MAX 4294967295 printf("-------------------------------------------------------\n"); printf("char:bytes %d;bit %d\n", sizeof(char), sizeof(char) * 8); // char:bytes 1;bit 8 printf("char:MIN %d;MAX %d\n", CHAR_MIN, CHAR_MAX); // char:MIN -128;MAX 127 printf("unsigned char:bytes %d;bit %d\n", sizeof(unsigned char), sizeof(unsigned char) * 8); //unsigned char:bytes 1;bit 8 printf("unsigned char:MIN is 0;MAX %d\n", UCHAR_MAX); // unsigned char:MIN is 0;MAX 255 printf("-------------------------------------------------------\n"); printf("short:bytes %d;bit %d\n", sizeof(short), sizeof(short) * 8); // short:bytes 2;bit 16 printf("short :MIN %hd;MAX %hd\n", SHRT_MIN, SHRT_MAX); // short :MIN -32768;MAX 32767 printf("unsigned short:bytes %d;bit %d\n", sizeof(unsigned short), sizeof(unsigned short) * 8); //unsigned short:bytes 2;bit 16 printf("unsigned short:MIN is 0;MAX %d\n", USHRT_MAX); // unsigned short:MIN is 0;MAX 65535 printf("-------------------------------------------------------\n"); printf("long:bytes %d;bit %d\n", sizeof(long), sizeof(long) * 8); // long:bytes 4;bit 32 printf("long :MIN %ld;MAX %ld\n", LONG_MIN, LONG_MAX); // long :MIN -2147483648;MAX 2147483647 printf("unsigned long:bytes %d;bit %d\n", sizeof(unsigned long), sizeof(unsigned long) * 8); //unsigned long:bytes 4;bit 32 printf("unsigned long:MIN is 0;MAX %lu\n", ULONG_MAX); // unsigned long:MIN is 0;MAX 4294967295 printf("-------------------------------------------------------\n"); printf("long long :bytes %d;bit %d\n", sizeof(long long), sizeof(long long) * 8); // long long :bytes 8;bit 64 printf("long long :MIN %lld;MAX %llu\n", LLONG_MIN, LLONG_MAX); // long long :MIN -9223372036854775808;MAX 9223372036854775807 printf("unsigned long long:bytes %d;bit %d\n", sizeof(unsigned long long), sizeof(unsigned long long) * 8); //unsigned long long:bytes 8;bit 64 printf("unsigned long long:MIN is 0;MAX %llu\n", ULLONG_MAX); // unsigned long long:MIN is 0;MAX 18446744073709551615 printf("-------------------------------------------------------\n"); printf("float :bytes %d;bit %d\n", sizeof(float), sizeof(float) * 8); // float :bytes 4;bit 32 printf("double :bytes %d;bit %d\n", sizeof(double), sizeof(double) * 8); // double :bytes 8;bit 64 printf("long double :bytes %d;bit %d\n", sizeof(long double), sizeof(long double) * 8); // long double :bytes 8;bit 64 printf("-------------------------------------------------------\n"); return 0; }