Computer Presentation
Computer Presentation
Introduction
• Type casting and conversion are essential concepts in
programming that allow for the manipulation of data
types. These concepts are used to convert one data type
to another, and to ensure that the correct data type is
being used in a program.
• Type casting and conversion are important concepts in
programming that allow you to change the data type of a
variable from one type to another. This can be useful in
a variety of situations, such as when you need to
perform calculations or comparisons on variables of
different types.
Type casting
Type casting involves changing the data type of a variable from one type to another. In
some cases, this can be done implicitly by the programming language, while in other
cases it must be done explicitly by the programmer.
Implicit Type Casting
• Implicit type casting, also known as type coercion, occurs when the programming
language automatically converts one data type to another. For example, if you add an
integer and a floating-point number, the programming language may automatically
convert the integer to a floating-point number before performing the addition.
Explicit Type Casting
• Explicit type casting, also known as type conversion, occurs when the programmer
manually converts one data type to another. This is often necessary when you need to
perform operations on variables of different types. For example, if you have a string
that represents a number, you may need to convert it to an actual number before you
Type Conversion
Type conversion involves changing the data type of a
variable from one type to another. This is typically done
explicitly by the programmer using built-in functions or
methods.
String Conversion: String conversion involves
converting a variable to a string data type. This is often
necessary when you need to output the value of a
variable as text or store it in a text file.
Numeric Conversion: Numeric conversion involves
converting a variable to a numeric data type, such as an
integer or a floating-point number. This is often
necessary when you need to perform mathematical
operations on the variable or compare it to other numeric
values
Boolean Conversions: This involves converting a value
to a boolean data type. For example, converting an
integer to a boolean using the bool() function.
Uses of typecasting and
conversion
• Type casting and conversion are essential
concepts in programming that allow
developers to change the data type of a
variable or convert it to a different
format. This feature is available in many
programming languages and is used
extensively in real-world applications. In
this presentation, we will explore some
examples of how type casting and
conversion are used in different
programming languages.
Examples of typecasting in C
This example converts string to Integer and Integer to String
char str_num[] = "42";
int int_num = atoi(str_num); // Conversion of string to integer
int int_val = 42; char str_val[10];
sprintf(str_val, "%d", int_val); // Conversion of integer to string
int int_num = 2;
char char_num = int_num + '0'; // Conversion of integer to character
Examples in Other Programming language
Python Java
Python is a popular programming language that Java is a strongly typed language that requires
supports dynamic typing, which means that variables to be declared with their data type
variables can be assigned different data types before they can be used. Type casting is used to
during runtime. Type casting is used to convert convert a variable from one data type to another.
one data type to another. For example, if we have For example, if we have a double variable and we
a string variable containing a number, we can use want to convert it to an integer, we can use type
type casting to convert it to an integer or float. casting.
Converting a string to an integer: int("10") Converting a double to an integer: (int) 3.14
Converting a string to a float: float("3.14")
Impact on Performance and Efficiency