My understanding is that 10-u16c
gets implicit type promotion to unsigned int
.
This depends upon the representation of the type of 10
(int
, as it were). Your understanding is correct for some systems, and we'll cover that first, but as you'll see later on in this answer, you're missing a big part of the picture.
Section 5.2.4, Environmental limits specifies that values of type int
can range from -32767 to 32767; this range may be extended at the discretion of implementations, but int
values must be able to represent this range.
uint16_t
, however, if it exists (it's not required to) has a range from 0 to 65535. Implementations can't extend that; it's a requirement that the range be precisely [0..65535]
(hence the reason this type isn't required to exist).
Section 6.3.1.3, Signed and unsigned integers tells us about the conversions to and fro. I couldn't paraphrase it better, so here's a direct quote:
1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.60)
3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
This all supports your theory that the int
value 10 would get converted to a uint16_t
if and only if int
is a sixteen bit type. However, section 6.3.1.8, usual arithmetic conversion rules should be applied first to decide which of the three above conversions takes place, as these rules change the way you'll look at the conversion when int
is greater than sixteen bits:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
So, as you can see from this, the type of the expression 10-u16c
might vary from system to system. On systems where int
is sixteen bits, that expression will be a uint16_t
.
Mathematically 10-u16c equals to -90. But how is it possible to represent a negative number as an unsigned int. When -90 gets promoted to unsigned int, does it mean that the sign of a number is ignored?
According to Annex H.2.2:
C's unsigned integer types are ''modulo'' in the LIA-1 sense in that overflows or out-of-bounds results silently wrap.
In other words, if 10
gets converted to a uint16_t
and the subtraction is performed, the result will be a large number, in this case you can see that number by explicitly converting both operands (i.e. casting them) to a uint16_t
. You could see a similar effect by using unsigned integer constants such as -90U
. This is largely supported by rule #2 from the quote from 6.3.1.3 earlier.
When this gets assigned to s32 which is 32 bit wide signed integer variable, how does the transformation takes place?
The expression 10-u16c
is converted according to rule #1 in 6.3.1.3 (quoted above) to an int32_t
value and stored as that value.
To fix this situation in 16bit integer machine, there needs a typecast of (int16_t)
for u16c
. How does this remedy this problem?
The typecast adds no useful information to this discussion. Perhaps you're using a non-compliant (buggy) compiler. I suspect the manual might shed some light on this, but since I don't know which compiler you're using I can't read it...