Integral upgrade in ANSI C
char,short int or int-bit segments (Bit-field), including their signed or unsigned variants, as well as enumeration types, can be used in an expression that requires an int or unsigned int, if the int can fully represent all the values of the source type. The value of that type is converted to int, otherwise it is converted to unsigned int.
An ordinary arithmetic conversion in ANSI C
When arithmetic operations are performed, the conversion occurs when the type of the operand is different.
The data type is generally toward a higher floating-point precision, longer length of the direction of conversion, integer number if converted to signed will not lose information, converted to signed, otherwise converted to unsigned.
This principle, called value retention (preserving), differs from the unsigned retention (unsigned preserving) principle adopted in K&r C.
Floating-point class:
If the type of an operand is a long double, then the other operand, regardless of type, is converted to a long double.
If two operands are not long, the other is converted to double when the type of one operand is double.
If two operands are not double, and one of the operands is float, the other is converted to float.
Note: Double and float are not unsigned,short decorated, and float is not available with a long modifier
Plastic Type regulation:
Otherwise, two operands are not one of three floating-point types, they must be some type of integer value. Before determining the common target elevation type, the compiler will impose a process called integer value elevation (integral promotion) on all integer value types less than int. The type (char, signed char, unsigned char and short, int) is promoted to type int when the integer value is promoted. If the int on the machine is sufficient to represent all the unsigned, shoft values (this usually happens in short with half a word and an int in the case of a word), the unsigned short int is also converted to int, otherwise it is promoted to unsigned Int.
wchar_t and enumeration types are promoted to the smallest integer types that can represent all values of their underlying type (underlying type).
Once the integer value is promoted, the type comparison begins again. If an operand is unsigned long, the second is also converted to a unsigned long type. If the type of the two operands is not unsigned long, and one of the operands is a long, the other is also converted to a long shape. A general conversion of the long type has an exception, if one operand is long and the other is unsigned int, then only the long on the machine is long enough to hold all the values of the unsigned int (typically, in a 32-bit operating system, The long and int types are expressed in a single word, so they do not satisfy the assumptions here, unsigned int is converted to a long type, otherwise two operands are promoted to the unsigned long type. If two operands are not long and one of them is unsigned int, the other is also converted to the unsigned int type. Otherwise, the two operands must all be int type.
int d =-1;
if (d <= sizeof (arr)/sizeof (arr[0))
...
There is a problem with this comparison statement, and the sizeof operator returns an unsigned number.
If statements test for equality between signed int and unsigned int, as described above, you can explain this:
First, the signed int and the unsigned int are of the same length and do not convert to a longer direction.
Second, the signed int does not fully represent all the values of the unsigned int.
Therefore, signed int d is converted to the unsigned int type.
In this way,-1 becomes a very large positive integer, resulting in the comparison being inconsistent with the expected result.
The workaround is to use the cast, (int) (sizeof (ARR)/sizeof (arr[0)).
I tried the above code in VC + + 6.0 and dev-c++ 4.9.9.0, and that's true.
Do not use it to denote numbers (such as age, national debt, etc.) because there is no negative value for unsigned numbers, so that you do not have to worry about boundary conditions (such as-1 being translated to very large positive numbers) in a mixed operation, using a signed number such as Int.
Unsigned numbers are used only when using bit segments and binary masks.
You should use coercion type conversions in an expression so that all operands are signed or unsigned, so you do not have to select the type of the result by the compiler.