c/language/cast

Performs explicit type conversion

Syntax
where

Explanation
If is, then  is evaluated for its side-effects and its returned value is discarded, same as when  is used on its own, as an.

Otherwise, if is exactly the type of, nothing is done (except that if  has floating type and is represented with greater range and precision than its type indicates -- see below)

Otherwise, the value of is converted to the type named by, as follows:

Every is allowed.

In addition to the implicit conversions, the following conversions are allowed:


 * Any integer can be cast to any pointer type. Except for the null pointer constants such as null (which ), the result is implementation-defined, may not be correctly aligned, may not point to an object of the referenced type, and may be a.
 * Any pointer type can be cast to any integer type. The result is implementation-defined, even for null pointer values (they do not necessarily result in the value zero). If the result cannot be represented in the target type, the behavior is undefined (unsigned integers do not implement modulo arithmetic on a cast from pointer)
 * Any pointer to object can be cast to any other pointer to object. If the value is not correctly aligned for the target type, the behavior is undefined. Otherwise, if the value is converted back to the original type, it compares equal to the original value. If a pointer to object is cast to pointer to any character type, the result points at the lowest byte of the object and may be incremented up to sizeof the target type (in other words, can be used to examine or to make a copy via memcpy or memmove).
 * Any pointer to function can be cast to a pointer to any other function type. If the resulting pointer is converted back to the original type, it compares equal to the original value. If the converted pointer is used to make a function call, the behavior is undefined (unless the function types are )
 * When casting between pointers (either object or function), if the original value is a null pointer value of its type, the result is the correct null pointer value for the target type.

In any case (both when executing an implicit conversion and in the same-type cast), if and  are floating types and  is represented with greater range and precision than its type indicates (see flt_eval_method), the range and precision are stripped off to match the target type.

The of the cast expression is always non-lvalue.