程序代码清单3.3.2_toobig.c程序_《C Primer Plus》P40

// toobig.cpp : 定义控制台应用程序的入口点。
//
/* toobig.c -- 超出您系统上的最大 int 值 */
/*
    时间:2018年06月04日 22:37:21
    代码:程序代码清单3.3.2_toobig.c程序_《C Primer Plus》P40
    目的:int 与 unsigned int 整数溢出的例子

*/
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    int i = 2147483647;                // int 范围值:-2147483648 ~ 2147483647;
    unsigned int j = 4294967295;    // unsigned 范围值 :0 ~ 4294967295;

    printf("%d %d %d\n", i, i+1, i+2);
    printf("%u %u %u\n", j, j+1, j+2);    // %u 输出控制符 显示 unsigned int 类型的值;

    getchar();

    return 0;
}

/*
    在VS2010中运行结果:
-----------------------------------------------------
2147483647 -2147483648 -2147483647
4294967295 0 1
------------------------------------------------------
    总结:
    unsigned 达到最大值,它将溢出到起始点:0;
    int 同理 达到最大值时,溢出到起始点:-2147483648;
------------------------------------------------------
*/


猜你喜欢

转载自blog.51cto.com/13555061/2124824