0% found this document useful (0 votes)
1 views

Typecasting in C

Typecasting in C is the manual conversion of one data type to another, allowing variables to behave like different types. There are two types of typecasting: implicit (automatic by the compiler) and explicit (manual by the programmer). Typecasting is used to control conversions, prevent data loss, and handle mixed-type mathematical expressions.

Uploaded by

Shounak Mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Typecasting in C

Typecasting in C is the manual conversion of one data type to another, allowing variables to behave like different types. There are two types of typecasting: implicit (automatic by the compiler) and explicit (manual by the programmer). Typecasting is used to control conversions, prevent data loss, and handle mixed-type mathematical expressions.

Uploaded by

Shounak Mondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Typecasting in C

What is Typecasting in C?

Definition:

Typecasting in C is the process of converting one data type into another manually.

It allows you to force a variable to behave like a different data type.

Types of Typecasting in C

Types of Typecasting in C:

1. Implicit Typecasting (Automatic)

2. Explicit Typecasting (Manual)

Implicit Typecasting

1. Implicit Typecasting:

- Done automatically by the compiler.

- Happens when assigning a smaller type to a bigger type (e.g., int to float).

Example:

int a = 10;

float b = a;

printf("%f", b); // Output: 10.000000

Explicit Typecasting

2. Explicit Typecasting:

- Done manually by the programmer.

- Used to convert one data type into another using cast operator.
Typecasting in C

Syntax:

(data_type) expression

Example:

float a = 5.7;

int b = (int) a;

printf("%d", b); // Output: 5

Why Use Typecasting?

Why Use Typecasting?

- To control type conversions.

- To prevent data loss or unexpected behavior.

- Useful in mathematical expressions involving mixed types.

Example with Mixed Types

Example with Mixed Types:

int a = 5, b = 2;

float result;

result = (float)a / b;

printf("%f", result); // Output: 2.5

You might also like