Standard Type Conversions

Many C operators cause implicit type conversions, which change the type of a value. When you add values of operands having different data types, both values are first converted to the same type. For example, when a short int value and an int value are added together, the short int value is converted to the int type.

Implicit type conversions can occur when:

You can perform explicit type conversions using the cast operator or the function style cast.

Signed-Integer Conversions
The compiler converts a signed integer to a shorter integer by truncating the high-order bits and converting the variable to a longer signed integer by sign-extension.

Conversion of signed integers to floating-point values takes place without loss of information, except when an int or long int value is converted to a float, in which case some precision may be lost. When a signed integer is converted to an unsigned integer, the signed integer is converted to the size of the unsigned integer, and the result is interpreted as an unsigned value.

Unsigned-Integer Conversions
An unsigned integer is converted to a shorter unsigned or signed integer by truncating the high-order bits. An unsigned integer is converted to a longer unsigned or signed integer by zero-extending. Zero-extending pads the leftmost bits of the longer integer with binary zeros.

When an unsigned integer is converted to a signed integer of the same size, no change in the bit pattern occurs. However, the value changes if the sign bit is set.

Floating-Point Conversions
A float value converted to a double undergoes no change in value. A double converted to a float is represented exactly, if possible. If the compiler cannot exactly represent the double value as a float, the value loses precision. If the value is too large to fit into a float, the result is undefined.

When a floating-point value is converted to an integer value, the decimal fraction portion of the floating-point value is discarded in the conversion. If the result is too large for the given integer type, the result of the conversion is undefined.

Pointer Conversions
Pointer conversions are performed when pointers are used, including pointer assignment, initialization, and comparison.

A constant expression that evaluates to zero can be converted to a pointer. This pointer will be a null pointer (pointer with a zero value), and is guaranteed not to point to any object.

Any pointer to an object that is not a const or volatile object can be converted to a void*. You can also convert any pointer to a function to a void*, provided that a void* has sufficient bits to hold it.

You can convert an expression with type array of some type to a pointer to the initial element of the array, except when the expression is used as the operand of the & (address) operator or the sizeof operator.

You can convert an expression with a type of function returning T to a pointer to a function returning T, except when the expression is used as the operand of the & (address) operator, the () (function call) operator, or the sizeof operator.

You can convert an integer value to an address offset.

You can convert a pointer to a class A to a pointer to an accessible base class B of that class, as long as the conversion is not ambiguous. The conversion is ambiguous if the expression for the accessible base class can refer to more than one distinct class. The resulting value points to the base class subobject of the derived class object. A null pointer (pointer with a zero value) is converted into itself.

You cannot convert a pointer to a class into a pointer to its base class if the base class is a virtual base class of the derived class.

Reference Conversions (C++)

A reference conversion can be performed wherever a reference initialization occurs , including reference initialization done in argument passing and function return values. A reference to a class can be converted to a reference to an accessible base class of that class as long as the conversion is not ambiguous. The result of the conversion is a reference to the base class subobject of the derived class object.

Reference conversion is allowed if the corresponding pointer conversion is allowed.

Pointer-to-Member Conversions (C++)

Pointer-to-member conversion can occur when pointers to members are initialized, assigned, or compared.

A constant expression that evaluates to zero is converted to a distinct pointer to a member.

Note: A pointer to a member is not the same as a pointer to an object or a pointer to a function.

A pointer to a member of a base class can be converted to a pointer to a member of a derived class if the following conditions are true:

Function Argument Conversions
If no function prototype declaration is visible when a function is called, the compiler can perform default argument promotions, which consist of the following:

Other Conversions
By definition, the void type has no value. Therefore, it cannot be converted to any other type, and no other value can be converted to void by assignment. However, a value can be explicitly cast to void.

No conversions between class, structure or union types are allowed. You can convert from an enum to any integral type but not from an integral type to an enum.



Operands
lvalues
Integral Promotions
Arithmetic Conversions


Arithmetic Conversions
Arithmetic Conversions for extended Level C
Pointers to Members
Pointer to Member Operators .* ->* .