HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02

Chapter 4 Type Conversions

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The use of different types of data within C programs creates a need for data type conversions. For example, some circumstances that may require a type conversion are when a program assigns one variable to another, when it passes arguments to functions, or when it tries to evaluate an expression containing operands of different types. C performs data conversions in these situations.

  • Assignment — Assignment operations cause some implicit type conversions. This makes arithmetic operations easier to write. Assigning an integer type variable to a floating type variable causes an automatic conversion from the integer type to the floating type.

  • Function call — Arguments to functions are implicitly converted following a number of 'widening' conversions. For example, characters are automatically converted to integers when passed as function arguments in the absence of a prototype.

  • Normal conversions — In preparation for arithmetic or logical operations, the compiler automatically converts from one type to another. Also, if two operands are not of the same type, one or both may be converted to a common type before the operation is performed.

  • Casting — You can explicitly force a conversion from one type to another using a cast operation.

  • Returned values — Values returned from a function are automatically converted to the function's type. For example, if a function was declared to return a double and the return statement has an integer expression, the integer value is automatically converted to a double.

Conversions from one type to another do not always cause an actual physical change in representation. Converting a 16-bit short int into a 64-bit double causes a representational change. Converting a 16-bit signed short int to a 16-bit unsigned short int does not cause a representational change.

© Hewlett-Packard Development Company, L.P.