• Home
  • 4.5 Explicit Type Conversion

4.5 Explicit Type Conversion

View Categories

4.5 Explicit Type Conversion

Explicit type conversion, also called type casting, allows a programmer to manually convert a value from one data type to another. In C, a cast is written by placing the destination type in parentheses before the expression.

Syntax #

(destination_type) expression

Example Program #

#include <stdio.h>

int main(void)
{
    double price = 149.75;
    int whole_price = (int)price;

    int a = 7;
    int b = 2;
    double result = (double)a / b;

    printf("Original price = %.2f\n", price);
    printf("Whole price    = %d\n", whole_price);
    printf("Division result = %.2f\n", result);

    return 0;
}

Example Output #

Original price = 149.75
Whole price    = 149
Division result = 3.50

Explanation #

1. Converting double to int

double price = 149.75;
int whole_price = (int)price;
  • price stores the floating-point value 149.75.
  • (int)price converts the value to int.
  • The fractional part is discarded, so whole_price becomes 149.

For floating-point to integer conversion, the fractional part is discarded toward zero. For example, (int)-4.8 produces -4.

2. Converting an integer before division

int a = 7;
int b = 2;
double result = (double)a / b;
  • (double)a converts a to 7.0.
  • Since one operand of / is a double, the other operand is converted to double for the operation.
  • The division produces 3.5, which is stored in result.

Compare this with:

double result = a / b;

Here, both operands are integers, so integer division produces 3 before the result is converted to double. The stored result is therefore 3.0, not 3.5.

Common Type-Casting Examples #

Expression Result Explanation
(int)9.8 9 Fractional part discarded
(int)-9.8 -9 Truncated toward zero
(double)5 5.0 Integer converted to floating point
(float)10 / 4 2.5 Floating-point division
(int)7.0 / 2 3 Both operands are integers after the cast
(double)(7 / 2) 3.0 Integer division occurs before the cast

Important Rules #

  • A cast applies to the expression immediately following it, according to C operator precedence. Use parentheses to make the intended order clear.
  • A cast does not change the declared type of the original variable; it converts the value for that expression.
  • Casting a floating-point value to an integer type is only defined when its integral part is representable in the destination type. Otherwise, the behavior is undefined.
  • A cast does not guarantee that the converted value is unchanged. Conversions may lose precision or change the value.
  • Explicit casts can make conversions clearer, but they should not be used to hide potential data loss or incorrect calculations.

Key takeaway: Use explicit type conversion when you need to control how a value is converted or how an expression is evaluated. Always consider the destination type and the conversion rules before casting.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *