C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Type Conversion

C Programming

In this tutorial we will learn about type conversion in C programming language.

In C we can have expressions consisting of constants and variables of different data types.

Types of conversions

There are two type of conversions in C.

  • Implicit type conversion
  • Explicit type conversion

Implicit type conversion

C performs automatic conversions of type in order to evaluate the expression. This is called implicit type conversion.

For example, if we have an integer data type value and a double data type value in an expression then C will automatically convert integer type value to double in order to evaluate the expression.

Rules for implicit type conversion

Following are the rules for the implicit type conversion in C.

First, all char and short are converted to int data type.

Then,

  • If any of the operand in the expression is long double then others will be converted to long double and we will get the result in long double.
  • Else, if any of the operand is double then other will be converted into double and the result will be in double.
  • Else, if any of the operand is float then other will be converted into float and the result will be in float.
  • Else, if any of the operand is unsigned long int then others will be converted into unsigned long int and we will get the result in unsigned long int.
  • Else, if any of the operand is long int and another is in unsigned int then,
    • If unsigned int can be converted to long int then it will be converted into long int and the result will be in long int.
    • Else, both will be converted into unsigned long int and the result will be in unsigned long int.
  • Else, if any of the operand is long int then other will be converted to long int and we will get the result in long int.
  • Else, if any of the operand is unsigned int then other will be converted into unsigned int and the result will be in unsigned int.

Remember the following hierarchy ladder of implicit type conversion.

If we downgrade from a higher data type to a lower data type then it causes lose of bits.

For example: Moving from double to float causes rounding of digits.

Downgrading from float to int causes truncation of the fractional part.

Explicit type conversion

In explicit type conversion we decide what type we want to convert the expression.

Syntax of explicit type conversion is:

(type) expression

Where, type is any of the type we want to convert the expression into.

In the following example we are converting floating point numbers into integer.

#include <stdio.h>
int main(void)
{
  //variables
  float
    x = 24.5,
    y = 7.2;

  //converting float to int
  int result = (int) x / (int) y;
  
  //output
  printf("Result = %d\n", result);
  
  printf("End of code\n");
  return 0;
}

Output

Result = 3
End of code

In the above code (int) x converts the value 24.5 into 24 and (int) y converts the value 7.2 into 7 so, we get 24/7 i.e., 3 as result because result is of type int and hence the decimal part is truncated.