Linux
Linux/Unix SSH, Ping, FTP, Telnet Communication Commands
While working on a Linux operating system, you may need to communicate with other devices . For...
Typecasting is converting one data type into another one. It is also called as data conversion or type conversion. It is one of the important concepts introduced in 'C' programming.
'C' programming provides two types of type casting operations:
Implicit type casting means conversion of data types without losing its original meaning. This type of typecasting is essential when you want to change data types without changing the significance of the values stored inside the variable.
Implicit type conversion happens automatically when a value is copied to its compatible data type. During conversion, strict rules for type conversion are applied. If the operands are of two different data types, then an operand having lower data type is automatically converted into a higher data type. This type of type conversion can be seen in the following example.
#include<stdio.h>
int main(){
short a=10; //initializing variable of short data type
int b; //declaring int variable
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
} Output
10 10
Consider the example of adding a character decoded in ASCII with an integer:
#include <stdio.h>
main() {
int number = 1;
char character = 'k'; /*ASCII value is 107 */
int sum;
sum = number + character;
printf("Value of sum : %d\n", sum );
} Output:
Value of sum : 108
Here, compiler has done an integer promotion by converting the value of 'k' to ASCII before performing the actual addition operation.
The compiler first proceeds with promoting a character to an integer. If the operands still have different data types, then they are converted to the highest data type that appears in the following hierarchy chart:
Consider the following example to understand the concept:
#include <stdio.h>
main() {
int num = 13;
char c = 'k'; /* ASCII value is 107 */
float sum;
sum = num + c;
printf("sum = %f\n", sum );} Output:
sum = 120.000000
First of all, the c variable gets converted to integer, but the compiler converts num and c into "float" and adds them to produce a 'float' result.
We cannot perform implicit type casting on the data types which are not compatible with each other such as:
In all the above cases, when we convert the data types, the value will lose its meaning. Generally, the loss of meaning of the value is warned by the compiler.
'C' programming provides another way of typecasting which is explicit type casting.
In implicit type conversion, the data type is converted automatically. There are some scenarios in which we may have to force type conversion. Suppose we have a variable div that stores the division of two operands which are declared as an int data type.
int result, var1=10, var2=3; result=var1/var2;
In this case, after the division performed on variables var1 and var2 the result stored in the variable "result" will be in an integer format. Whenever this happens, the value stored in the variable "result" loses its meaning because it does not consider the fraction part which is normally obtained in the division of two numbers.
To force the type conversion in such situations, we use explicit type casting.
It requires a type casting operator. The general syntax for type casting operations is as follows:
(type-name) expression
Here,
Let us write a program to demonstrate implementation of explicit type-casting in 'C'.
#include<stdio.h>
int main()
{
float a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
return 0;
}Output:
Value of a is 1.200000 Value of b is 2
In this way, we can implement explicit type casting in 'C' programming.
Keep in mind the following rules for programming practice when dealing with different data type to prevent from data loss :
While working on a Linux operating system, you may need to communicate with other devices . For...
What is a Variable? Variable is a name assign to a storage area that the program can manipulate. A variable...
Here are SCCM interview questions for fresher as well as experienced candidates to get their dream...
In this tutorial, we will learn- What is a Pipe in Linux? 'pg' and 'more' commands The 'grep'...
A Podcast Hosting Platform is a special service that stores media files and delivers them to...
A Virtual Machine (VM) is a software environment that emulates a computer system. It facilitates a...