I\'m studying the C++ language and i have some doubt about type conversion, could you explain me what happens in an expression like this :
unsigned int u =
1) Due to standard promotion rules, the signed type a is promoted to an unsigned type prior to subtraction. That promotion happens according to this rule (C++ standard 4.7/2):
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type).
Algebraically a becomes be a very large positive number and certainly larger than u.
2) u - a is an anonymous temporary and will be a unsigned type. (You can verify this by writing auto t = u - a and inspecting the type of t in your debugger.) Mathematically this will be a negative number but on implicit conversion to the unsigned type, a wraparound rule similar to above is invoked.
In short, the two conversion operations have equal and opposite effects and the result will be 52. In practice, the compiler might optimise out all these conversions.