C Programming โ€ข C Basics, Operators, Loops
C Programming / Type conversion

Type conversion

Notes 2 C Basics, Operators, Loops

Type conversion is the process of converting a value from one data type to another data type. It is useful when different data types are used together in an expression.

Notes

Type Conversion

Type conversion is the process of converting a value from one data type to another data type. It is useful when different data types are used together in an expression.

๐Ÿ’ก Example

An integer value can be converted into a floating-point value: int a = 10; โ†’ 10.0

Types of Type Conversion

Type conversion in C is commonly understood in two ways: Implicit Type Conversion and Explicit Type Conversion.

1. Implicit Type Conversion

Implicit type conversion happens automatically by the compiler when values of different data types are used together. The programmer does not explicitly specify the conversion.

๐Ÿ’ก Example
int a = 10; float b = 2.5; float result = a + b;

Here, a is automatically converted to a floating-point value before the addition, so the result can be stored in float.

2. Explicit Type Conversion (Type Casting)

Explicit type conversion is performed when the programmer deliberately converts a value into another data type using a type cast.

๐Ÿ’ก Example
int a = 5; int b = 2; float result = (float)a / b;

Here, (float)a explicitly converts a into a floating-point value, so the division produces 2.5.

Type Casting Syntax

The basic syntax for explicit type casting is:

(data_type) expression
๐Ÿ’ก Example

float x = (float)10;

The integer value 10 is explicitly converted to float.

Why is Type Conversion Needed?

Type conversion is needed when different types of values must be used together or when the result is required in a particular data type.

๐Ÿ’ก Example
int total = 5; int count = 2; float average = (float)total / count;

Type casting is used here so that the average is calculated as 2.5 instead of integer division giving 2.

Implicit vs Explicit Conversion

Feature Implicit Explicit
Performed by Compiler Programmer
Specified by programmer? No Yes
Syntax No special syntax required (data_type)
Example float x = 10; float x = (float)10;

Important Example: Integer Division

When two integer operands are divided using /, the result is an integer. The fractional part is not retained.

๐Ÿ’ก Example
int a = 5; int b = 2; int result = a / b;

Output: 2

Using Type Casting in Division

To obtain a decimal result, one operand can be explicitly converted to float.

๐Ÿ’ก Example
int a = 5; int b = 2; float result = (float)a / b;

Output: 2.5

Possible Loss of Data

When a value is converted from a data type that can represent more information to one that can represent less information, some data may be lost.

๐Ÿ’ก Example
float x = 10.75; int y = (int)x;

The fractional part is not retained, so y becomes 10.

Quick Revision

Concept Remember
Type Conversion Changing a value from one data type to another.
Implicit Conversion performed automatically.
Explicit Conversion performed by the programmer.
Type Casting Explicit conversion using (data_type).
Integer Division Integer รท Integer gives an integer result.
Data Loss Can occur when converting to a less precise type.

Important Exam Questions

Short Answer Questions

  1. What is type conversion in C?
  2. What is implicit type conversion?
  3. What is explicit type conversion?
  4. What is type casting?
  5. Write the syntax of type casting in C.
  6. What is the difference between implicit and explicit conversion?
  7. Why is type casting useful in integer division?

Long Answer Questions

  1. Explain implicit and explicit type conversion with suitable examples.
  2. Explain type casting in C with suitable examples.
  3. Explain integer division and show how type casting can be used to obtain a decimal result.
๐ŸŽฅ Recommended Learning

Watch a beginner-friendly explanation of implicit and explicit type conversion in C.

โ–ถ Watch: Type Conversion in C โ€” Hindi

๐Ÿ“ Handwritten Notes

A short handwritten-style revision sheet for type conversion will be provided here.

๐Ÿง  Mind Map

Use the mind map for quick revision of implicit and explicit type conversion.