Type Conversions

A type conversion yields the value of an expression in a new type. Conversion can be performed only on scalar types, i. e., arithmetic types and pointers.

A type conversion always conserves the original value, if the new type is capable of representing it. Floating-point numbers may be rounded on conversion from double to float, for example.

Type conversions can be implicit —i. e., performed by the compiler automatically—or explicit , through the use of the cast operator. It is considered good programming style to use the cast operator whenever type conversions are necessary. This makes the type conversion explicit, and avoids compiler warnings.

Get C Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.