The ASNI C standard rule takes the value preserving principle, that is, when several integer operands are mixed, the result type may be either a conforming number or an unsigned number, depending on the relative size of the operand's type.
#include <stdio.h>
#define Size_of_array (arr) (sizeof (ARR)/sizeof (arr[0))
int main () {
int d =-1;
int a[] = {1, 2, 3, 4, 5, 6, 7};
if (D<size_of_array (a)) {
printf ("1\n");
} else {
printf (" -1\n");
}
return 0;
}
The execution results are as follows: 1
The type of the value defined by Size_of_array is the unsigned int type (the type that sizeof returns is an unsigned type), and the IF statement tests equality between signed int and unsigned int, so d is promoted to unsigned int Type. 1 is a large value when converted to unsigned int type, so the expression is false.
If you cast the return result of Size_of_array to int, the result will be as follows because of the same type:
if (d< (int) (Size_of_array (a)))
The execution results are as follows: 1
Value retention principle