Type Casting in Programming
- Converting a single data type value—such as an integer int, float, or double—into another data type is known as typecasting. You have the option of doing this conversion manually or automatically. The conversion is done in two ways, automatically by the compiler and manually by a programmer.
- Type casting is sometimes known as type conversion. For example, a programmer can type cast a long variable value into an int if they wish to store it in the program as a simple integer. Thus, type casting is a technique that allows users to utilize the cast operator to change values from one data type to another.
- Type casting is used when imagine you have an age value, let’s say 30, stored in a program. You want to display a message on a website or application that says “Your age is: 30 years.” To display it as part of the message (a string), you would need to convert the age (an integer) to a string.
- Simple explanation of type casting can be done by this example:
- Imagine you have two types of containers: one for numbers and one for words. Now, let’s say you have a number written on a piece of paper, like “42,” and you want to put it in the container meant for words. Type casting is like taking that number, converting it into words, and then putting it in the container for words. Similarly, in programming, you might have a number (like 42) stored as one type, and you want to use it as if it were another type (like a word or text). Type casting helps you make that conversion.
Types of Type Casting:
The process of type casting can be performed in two major types in a C program. These are:
- Implicit – done internally by compiler.
- Explicit – done by programmer manually.
Syntax for Type Casting:
<datatype> variableName = (<datatype>) value;
Example of Type Casting:
1. Converting int into double
#include <iostream>
using namespace std;
int main() {
int intValue = 54;
double doubleValue;
doubleValue = intValue;
cout << "int value: " << intValue << endl;
cout << "double value: " << doubleValue << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int intValue = 54; // Declaring and initializing an integer variable
double doubleValue; // Declaring a double variable
doubleValue = intValue; // Implicit conversion from int to double
System.out.println("int value: " + intValue); // Printing the integer value
System.out.println("double value: " + doubleValue); // Printing the double value
}
}
# code
print "GFG"
using System;
class MainClass {
public static void Main (string[] args) {
int intValue = 54; // Declaring and initializing an integer variable
double doubleValue; // Declaring a double variable
doubleValue = intValue; // Implicit conversion from int to double
Console.WriteLine ("int value: " + intValue); // Printing the integer value
Console.WriteLine ("double value: " + doubleValue); // Printing the double value
}
}
// JavaScript program to demonstrate implicit type conversion
// Declaring and initializing an integer variable
let intValue = 54;
// Declaring a double variable
let doubleValue;
// Implicit conversion from int to double
doubleValue = intValue;
// Printing the integer value
console.log("int value: " + intValue);
// Printing the double value
console.log("double value: " + doubleValue);
Output
int value: 54 double value: 54
2. Automatic Conversion of double to int:
#include <iostream>
using namespace std;
int main() {
double doubleValue = 54.7;
int intValue;
intValue = doubleValue;
cout << "double value: " << doubleValue << endl;
cout << "int value: " << intValue << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
// Declaring a double variable
double doubleValue = 54.7;
// Converting the double to an integer
int intValue = (int) doubleValue;
// Printing the double value
System.out.println("double value: " + doubleValue);
// Printing the integer value
System.out.println("int value: " + intValue);
}
}
using System;
class MainClass {
public static void Main (string[] args) {
double doubleValue = 54.7; // Declaring and initializing a double variable
int intValue; // Declaring an integer variable
intValue = (int)doubleValue; // Explicitly casting the double value to an int
Console.WriteLine ("double value: " + doubleValue); // Printing the double value
Console.WriteLine ("int value: " + intValue); // Printing the integer value
}
}
// Declaring a double variable
const doubleValue = 54.7;
// Converting the double to an integer
const intValue = Math.floor(doubleValue);
// Printing the double value
console.log("double value: " + doubleValue);
// Printing the integer value
console.log("int value: " + intValue);
# The code declares a double variable, assigns a value to it,
# converts it to an integer, and prints both the double and integer values.
# Declaring a double variable
double_value = 54.7
# Converting the double to an integer
int_value = int(double_value)
# Printing the double value
print("double value:", double_value)
# Printing the integer value
print("int value:", int_value)
Output
double value: 54.7 int value: 54
Data Types in Programming
In Programming, data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and that the value of each property is as expected.
Table of Content
- What are Data Types in Programming?
- Common Data Types in Programming
- Common Primitive Data Types in Programming
- Common Composite Data Types
- Common User-Defined Data Types
- Dynamic vs Static Typing in Programming
- Type Casting in Programming
- Variables and Data Types in Programming
- Type Safety in Programming