1.
main()
{
if(-1<(unsigned char)1)
printf("-1 is less than (unsigned char)1:ANSI semantics");
else
printf("-1 NOT less than (unsigned char)1:K&R semantics");
}
2.
int array[] = {23,41,12,24,52,11};
#define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))
main()
{
int d = -1,x;
if(d<=TOTAL_ELEMENTS -2)
x = array[d+1];
}
第一轉換無符號字符1到的標誌ed變量,而第二個程序將d轉換爲一個無符號整型,這使得在ANSI C中 條件表達式返回false。 爲什麼它們的行爲有所不同?
在你的第二個代碼示例中,你的意思是「數組」而不是「arrary」嗎? – mweiss 2009-02-01 09:56:53