0% found this document useful (0 votes)
9 views4 pages

CP Practical-2

Uploaded by

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

CP Practical-2

Uploaded by

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

Practical - 2

Aim :- Implement data type and data casting.

DataType :-
Each variable in C has an associated data type. It specifies the type of data that the variable
can store like integer, character, floating, double, etc. Each data type requires different
amounts of memory and has some specific operations which can be performed over it. The
data type is a collection of data with values having fixed values.

The types in C can be classified as follows −

Types Description

Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.

User Defined
The user-defined data types are defined by the user himself.
Data Types

The data types that are derived from the primitive or built-in datatypes
Derived Types
are referred to as Derived Data Types.
Data Casting in C

Data casting is the process of converting one data type to another. There are two types of
casting in C:

 Implicit Casting (Automatic Type Conversion):

This happens automatically when you assign a value of one type to a variable of another type.

Example:

int i = 10;
double d = i; // Implicitly casts 'int' to 'double'

 Explicit Casting (Type Casting):

This requires explicit syntax to convert one data type to another.

Example:

double d = 10.5;
int i = (int) d; // Explicitly casts 'double' to 'int'
Examples of Type Casting
Example 1: Implicit Casting
#include <stdio.h>
#include<conio.h>

void main()
{
int i = 42;
double d = i; // Implicit cast from int to double

clrscr();

printf("Value of d: %f\n", d);


getch();
}
Example 2: Explicit Casting
#include <stdio.h>
#include <conio.h>
void main()
{
double d = 42.56;
int i = (int) d; // Explicit cast from double to int
clrscr();
printf("Value of i: %d\n", i);
getch();
}

C program to illustrate the use of typecasting

#include <stdio.h>
#include<conio.h>

void main()
{
// Given a & b
int a = 15, b = 2;
float div;

// Division of a and b
div = a / b;

printf("The result is %f\n", div);

return 0;
}

You might also like