C语言整数类型的存储空间和取值范围
- 四种整数类型 char, short, int, long 默认有符号,再加上无符号限制,共8种情况
char // 字符型,单字节,取值范围:CHAR_MIN - CHAR_MAX
unsigned char // 无符号字符型, 取值范围:0 - UCHAR_MAX
short int // 短整型,双字节,取值范围:SHRT_MIN - SHRT_MAX
unsigned short int // 无符号短整型, 取值范围:0 - USHRT_MAX
int // 整型,四字节, 取值范围:INT_MIN - INT_MAX
unsigned int // 无符号整型, 取值范围:0 - UINT_MAX
long int // 长整型,八字节,取值范围:LONG_MIN - LONG_MAX
unsigned long int // 无符号长整型, 取值范围: 0 - ULONG_MAX
在Linux下测试发现long long,即长长整型与long长整型所表达的效果是一样的!!!
- 写代码输出整数类型的存储空间和取值范围相关信息
注意:相关的宏定义在limits.h头文件中,要包含进来!
/* filename: limits.c */
#include <stdio.h>
#include <limits.h>
/*
compile : gcc limits.c -o limits
run : ./limits
*/
/* output datatype char, short, int, long
sizeof, min, max, unsigned max information */
void
out_smmu_info (void)
{
printf ("Sizeof char is %lu\n", sizeof (char));
printf ("Min char is %d\n", CHAR_MIN);
printf ("Max char is %d\n", CHAR_MAX);
printf ("Max unsigned char is %u\n", UCHAR_MAX);
printf ("--------------------------------\n");
printf ("Sizeof short is %lu\n", sizeof (short));
printf ("Min short is %d\n", SHRT_MIN);
printf ("Max short is %d\n", SHRT_MAX);
printf ("Max unsigned short is %u\n", USHRT_MAX);
printf ("--------------------------------\n");
printf ("Sizeof int is %lu\n", sizeof (int));
printf ("Min int is %d\n", INT_MIN);
printf ("Max int is %d\n", INT_MAX);
printf ("Max unsigned int is %u\n", UINT_MAX);
printf ("--------------------------------\n");
printf ("Sizeof long is %lu\n", sizeof (long));
printf ("Min long is %ld\n", LONG_MIN);
printf ("Max long is %ld\n", LONG_MAX);
printf ("Max unsigned long is %lu\n", ULONG_MAX);
printf ("--------------------------------\n");
}
/* output char, short, int, long
min, max, unsigned max value, define myself use hex */
void
out_selfdef_info (void)
{
printf ("Self def: CHAR_MIN ==> 0X80, %d\n", (char)(0X80));
printf ("Self def: CHAR_MAX ==> 0X7F, %d\n", (char)(0X7F));
printf ("Self def: UCHAR_MAX ==> 0XFF, %u\n", (unsigned char)(0XFF));
printf ("-------------------------------------------------------\n");
printf ("Self def: SHORT_MIN ==> 0X8000, %d\n", (short)(0X8000));
printf ("Self def: SHORT_MAX ==> 0X7FFF, %d\n", (short)(0X7FFF));
printf ("Self def: USHORT_MAX ==> 0XFFFF, %u\n", (unsigned short)(0XFFFF));
printf ("-------------------------------------------------------\n");
printf ("Self def: INT_MIN ==> 0X80000000, %d\n", (int)(0X80000000));
printf ("Self def: INT_MAX ==> 0X7FFFFFFF, %d\n", (int)(0X7FFFFFFF));
printf ("Self def: UINT_MAX ==> 0XFFFF, %u\n", (unsigned int)(0XFFFFFFFF));
printf ("-------------------------------------------------------\n");
printf ("Self def: LONG_MIN ==> 0X8000000000000000, %ld\n",
(long)(0X8000000000000000));
printf ("Self def: LONG_MAX ==> 0X7FFFFFFFFFFFFFFF, %ld\n",
(long)(0X7FFFFFFFFFFFFFFF));
printf ("Self def: ULONG_MAX ==> 0XFFFFFFFFFFFFFFFF, %lu\n",
(unsigned long)(0XFFFFFFFFFFFFFFFF));
printf ("-------------------------------------------------------\n");
}
/**/
int
main (int argc, char *argv[])
{
out_smmu_info ();
out_selfdef_info ();
return 0;
}
/* --(:-:)-- */
- 编译运行,输出结果如下:
gwsong@ubuntu:~/works/zoo/tmp$ gcc limits.c -o limits
gwsong@ubuntu:~/works/zoo/tmp$ ./limits
Sizeof char is 1
Min char is -128
Max char is 127
Max unsigned char is 255
--------------------------------
Sizeof short is 2
Min short is -32768
Max short is 32767
Max unsigned short is 65535
--------------------------------
Sizeof int is 4
Min int is -2147483648
Max int is 2147483647
Max unsigned int is 4294967295
--------------------------------
Sizeof long is 8
Min long is -9223372036854775808
Max long is 9223372036854775807
Max unsigned long is 18446744073709551615
--------------------------------
Self def: CHAR_MIN ==> 0X80, -128
Self def: CHAR_MAX ==> 0X7F, 127
Self def: UCHAR_MAX ==> 0XFF, 255
-------------------------------------------------------
Self def: SHORT_MIN ==> 0X8000, -32768
Self def: SHORT_MAX ==> 0X7FFF, 32767
Self def: USHORT_MAX ==> 0XFFFF, 65535
-------------------------------------------------------
Self def: INT_MIN ==> 0X80000000, -2147483648
Self def: INT_MAX ==> 0X7FFFFFFF, 2147483647
Self def: UINT_MAX ==> 0XFFFF, 4294967295
-------------------------------------------------------
Self def: LONG_MIN ==> 0X8000000000000000, -9223372036854775808
Self def: LONG_MAX ==> 0X7FFFFFFFFFFFFFFF, 9223372036854775807
Self def: ULONG_MAX ==> 0XFFFFFFFFFFFFFFFF, 18446744073709551615
-------------------------------------------------------
- 用十六进制自定义取值范围清晰明了,不同情况下可以选择不同长度的整数数据类型
下一步研究一下其他数据类型的存储长度和相关信息。