#include ""
int main(int argc, char *argv[])
{
//unsigned types min (为数值0)
unsigned char u_char_min = 0;
unsigned short u_short_min = 0;
unsigned int u_int_min = 0;
unsigned long u_long_min = 0;
//unsigned types max (最小值取反为最大值)
unsigned char u_char_max = ~u_char_min;
unsigned short u_short_max= ~u_short_min;
unsigned int u_int_max = ~u_int_min;
unsigned long u_long_max = ~u_long_min;
//signed types max (无符号最大值除以2为有符号数最大值)
char char_max = (char)(u_char_max >> 1);
short short_max = (short)(u_short_max >> 1);
int int_max = (int)(u_int_max >> 1);
long long_max = (long)(u_long_max >> 1);
//signed types min (符号数最大值再加1, 为符号数最小值)
char char_min = char_max + 1;
short short_min = short_max + 1;
int int_min = int_max + 1;
long long_min = long_max +1;
//输出结果:
//unsigned types min
printf("unsigned char min = %u 0x%x\n", u_char_min,u_char_min);
printf("unsigned short min = %u 0x%x\n", u_short_min,u_short_min);
printf("unsigned int min = %u 0x%x\n", u_int_min,u_int_min);
printf("unsigned long min = %lu 0x%x\n", u_long_min ,u_long_min);
//unsigned types max
printf("unsigned char max = %u 0x%x\n", u_char_max,u_char_max);
printf("unsigned short max = %u 0x%x\n", u_short_max,u_short_max);
printf("unsigned int max = %u 0x%x\n", u_int_max,u_int_max);
printf("unsigned long max = %lu 0x%x\n", u_long_max ,u_long_max);
//signed types max
printf("signed char max = %d 0x%x\n", char_max,char_max);
printf("signed short max = %d 0x%x\n", short_max ,short_max);
printf("signed int max = %d 0x%x\n", int_max ,int_max);
printf("signed long max = %ld 0x%x\n", long_max,long_max);
//signed types min
printf("signed char min = %d 0x%x\n", char_min,char_min);
printf("signed short min = %d 0x%x\n", short_min,short_min);
printf("signed int min = %d 0x%x\n", int_min ,int_min);
printf("signed long min = %ld 0x%x\n", long_min ,long_min);
_getch();
return 0;
}
输出结果:
unsigned char min = 0 0x0
unsigned short min = 0 0x0
unsigned int min = 0 0x0
unsigned long min = 0 0x0
unsigned char max = 255 0xff
unsigned short max = 65535 0xffff
unsigned int max = 4294967295 0xffffffff
unsigned long max = 4294967295 0xffffffff
signed char max = 127 0x7f
signed short max = 32767 0x7fff
signed int max = 2147483647 0x7fffffff
signed long max = 2147483647 0x7fffffff
signed char min = -128 0xffffff80
signed short min = -32768 0xffff8000
signed int min = -2147483648 0x80000000
signed long min = -2147483648 0x80000000
小结:
语言是强类型语言,先声明变量类型,再赋值。不同的类型,将决定用多大内存存储数据。
2.由此看出各种类型数的表示方法及最大值,最小值是多少。
#include ""
int main(int argc, char *argv[])
{
printf("%d, %x\n",1,1);
printf("%d, %x\n",2,2);
printf("%d, %x\n",-1,-1);
printf("%d, %x\n",-2,-2);
_getch();
return 0;
}
输出结果:
1, 1
2, 2
-1, ffffffff
-2, fffffffe
由此可见,负数是按补码方式表示的。
负数的补码是其正数按位取反再加1.
用补码表示的好处是。 一个正数加上其对应的负数,结果为0.