首页 > 代码库 > int类型有符号与无符号内存中 -- C

int类型有符号与无符号内存中 -- C


/*
int 有符号
			0xffffffff == -1
			0xfffffffe == -2
	最小		0x80000000 == -21 4748 3648
	最大		0x7fffffff == 21 4748 3647
			0x00000001 == 1
			0x00000000 == 0
			
int 无符号
	最大		0xffffffff == 42 9496 7295
	 		0x80000000 == 21 4748 3648
			0x7fffffff == 21 4748 3647
	最小		0x00000000 == 0
*/

int
main()
{
	/*	首先参数5为int型,32位平台为4字节,栈中分配4字节的内存,
	用于存放参数5。
		%f符号会认为这个参数是double型,printf函数中会将float自动
	转换成double,因此栈中读取8个字节。
		所以内存访问越界。
		*/
	printf("5 = %f\n",5);

	/*	参数5.01为double型,存储时是8个字节。
		%d只读取其中4个字节。
		*/
	printf("5.01 = %d\n",5.01);

}
/*
[root@localhost test_class]# ./a.out 
5 = 0.000000
5.01 = 1889785610
[root@localhost test_class]# ./a.out 
5 = 0.000000
5.01 = 1889785610
[root@localhost test_class]# ./a.out 
5 = 0.000000
5.01 = 1889785610
*/