Write a security code:

read C99 standard a few days ago and wrote two blog posts on integer promotion, http://blog.chinaunix.net/space.php?uid23629988&amp ; doblog& Id2938697 in this comment, pragma friend asked a question. After Rereading C99, discussing with others and thinking, I got the answer. This has deepened my understanding of integer lifting again, so I need to further summarize this problem
let’s take a look at the questions raised by pragma


#include#include# define PRINT_ COMPARE_ RESULT(a,b)if(a>b){printf(#a” > “# b”n”);} elseif(a

the output result is
[ [email protected] test]#./ a.outa > bc

why does the result change from int to short
in C99 standard, there are two conversions in arithmetic operation, one is integer promotion and the other is universal arithmetic conversions. In the previous blog post, I studied the second one and understood it as integer promotion, which is actually inaccurate
the following is the description of the first integer promotion in C99:
If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

— in the context of C99, this integer promotion occurs during arithmetic operation

and C99 about universal arithmetic conversions, join my blog: http://blog.chinaunix.net/space.php?uid23629988&amp ; doblog& Id2938697

in other words, two integer conversions will be involved in arithmetic operations. The results of the above test code are also derived from this For the following rules: > and , it is only used for div analysis, so it is equivalent to
for C and D, first, integer promotion will work, and both C and D will turn into int type, that is, it is equivalent to int c – 1 and int d 2; Then carry out the usual arithmetic conversion. Since both are int type at this time, the natural result is C & lt; D
now the answer is clear, but it makes me feel uneasy, because such integer conversion rules are too easy to make mistakes. For us, although the two rules have been relatively clear, we should try to avoid the comparison of signed and unsigned numbers in the code. If it is unavoidable, it is best to use cast in order to clearly indicate your purpose