×

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

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute 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.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes 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.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.