×

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that can be converted. A compiler is responsible for all the type conversions in the C.

The final data type cannot be less than the original data type after the type conversion. Type conversion, also known as "widening conversion," occurs only during the compilation period.

Type conversion comes in two varieties. They are:

1. Explicit Type Conversion

2. Implicit Type Conversion

1. Explicit Type Conversion: We manually transform values from one data type to another in the explicit type conversion. Explicit Type Conversion is also known as the type casting method, and it is also user-defined. Users can typecast the output here to generate it of a specific data type. 

In the C programming language, the syntax for explicit type conversion is

(data type) expression

Example

#include<stdio.h>
int main() 
{
	 /* Here create a float variable */
  	float num_1 = 96.125626;
  	printf("The given decimal value is: %.6f\n", num_1);
  	/* Explicit data type conversion is performed here */
  	double num_2 = (double) num_1;
  	printf("The value after type conversion is: %.2lf", num_2);
  	return 0;
}

Output:

Type Conversion in the C

In the preceding program, we declared a float variable named "num_1" with the value 96.125626. Take note of the following code snippet:

/* Explicit data type conversion is performed here */
double num_2 = (double) num_1;

Here,

(double) indicates the data type that "num_1" should be converted to.

The value num_1 will be converted to double data type.

Example

#include<stdio.h>
int main()  
{
	 /* Here create a float variable */
  	int num_1 = 106;
  	printf("The given integer value is:%d\n", num_1);
 	/* (char) is a function that converts a number to a character. */
  	char alpha_1 = (char) num_1; 
  	printf("The value after type conversion is: %c", alpha_1);
  	return 0;
}

Output:

Type Conversion in the C

In the preceding program, we declared an int variable named "num_1" with the value 106. Take note of the following code snippet:

/* (char) is a function that converts a number to a character. */
char alpha_1 = (char) num_1;

Here,

(char) converts "num_1" (an integer data type) to a character data type specifically.

The value num_1 will be converted to char data type.

2. Implicit Type Conversion: In implicit type conversion, the value of one data type is automatically transformed to another data type. Automatic type conversion is another name for implicit type conversion.

This is entirely handled by the compiler, with no external help. This usually happens when more than one data type is contained in an expression. In such cases, type conversion occurs to prevent any data loss.

All variable data types are updated to the data type of their variables with the greatest data type.

bool data type < char data type < short int data type < int data type < unsigned int data type < long data type < unsigned data type < long long data type < float data type < double data type < long double data type

Implicit conversions may lose information, signs may be lost (when a signed value is implicitly converted to an unsigned value), and overflow is possible. (The long data type is implicitly converted to a float data type.)

Implicit Type Conversion Occurs in the C

Some of its occurrences are listed below:

1. "Rank of Conversion" is an example of implicit type conversion.

2. “Assignment expression conversions”

3. Implicit data type conversion can also be seen in promotions and demotions.

Example

#include<stdio.h>
int main() 
{
	/* Here create a float variable */
  	double num_1 = 5185.89;
  	printf("The given double value is: %.2lf\n", num_1);
 	/* Converting a double value num_1 to an integer */
  	int num_2 = num_1;
  	printf("The value after type conversion is: %d", num_2);
  	return 0;
}

 Output:

Type Conversion in the C

In the preceding program, we declared a double variable named "num_1" with the value 5185.89. Take note of the following code snippet:

/* Converting a double value num_1 to an integer */
int num_2 = num_1;

The C language compiler automatically transforms the double number 5185.89 to the integer value 5185 in this case.

Because the conversion occurs automatically, this form of conversion is known as implicit type conversion.

Example

#include<stdio.h>
int main() 
{
	/* Here create a float variable */
  	char alpha_1 = 'k';
  	printf("The given char value is:%c\n", alpha_1);
	/* Converting a char value alpha_1 to an integer */
  	int num_1 = alpha_1;
  	printf("The value after type conversion is: %d", num_1);
 	return 0;
}

Output:

Type Conversion in the C

In the preceding program, we declared a char variable named "alpha_1" with the value ‘k’. Take note of the following code snippet:

/* Converting a char value alpha_1 to an integer */
int num_1 = alpha_1;

The C language compiler automatically transforms the char value ‘k’ to the integer value 107 in this case.


Related Topics

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.