整形溢出问题,宏定义预算的范围?

#define START 0x8000
#define END 0xFFFF
#define LEN 0x8000
#if ( (START + LEN ) > END )
#error Buffer Overrun /* OK because START and LEN are unsigned long /
#endif
#if ( ( ( END – START ) – LEN ) < 0 )
#error Buffer Overrun
/
Not OK: subtraction result wraps around to 0xFFFFFFFF /
#endif
/
contrast the above START + LEN with the following /
if ( ( START + LEN ) > END ) *
//此处为什么会溢出,上面预编译不会溢出**
{
error ( “Buffer overrun “ );
/* Not OK: START + LEN wraps around to 0x0000 due to
unsigned
int arithmetic */
}

预编译和if 语句的 范围怎么不一样了

不知道你是在什么环境下测试的,但是,宏定义的常量是没有类型的,而且你定义的三个宏,都是16位整数就可以存下的,用int(如果是32位的)
可以正常使用,在VS下运行如下代码,三个条件都为真:

 #include<stdio.h>

#define START   0x8000
#define END     0xFFFF
#define LEN     0x8000



#if ((START + LEN )>END)
    SUM = 1;
#endif

#if (((END - START) - LEN)<0)
    VEL = 0;
#endif


void main()
{
    int sum = SUM, vel = VEL;
    printf("sum = %d\nvel=%d\n",sum,vel);

    if ((START + LEN) > END)
    {
        int i = (START + LEN);
        printf("START + LEN = %d\n", i);
    }

    system("pause");
}

结果:
sum = 1
vel=0
START + LEN = 65536