C Type Casting with examples - float to int / int to char / int to long

    2 Votes

The value of a variable or expression can be converted to another data type if desired. It is called type casting or type conversion. If you have a variable of data type long and you want to assign it to an int type variable, type casting can be used for that purpose. To type cast a variable or expression it must be preceded by the desired datatype as follows.

(data type) expression;

Why do we need type casting?

Type casting would be pretty handy when your inputs to an operation is of a particular type and you need more precision in the output. Let's see the use of type casting through a simple example

#include <stdio.h>

int main()
   int value1 = 10, value2 = 3;
   float result;
   result = value1 / value2;
   printf("Result without type casting: %f", result);
   return 0;

Result without type casting: 3.000000

In the above example we assign the result of division operation to float type variable result. The result is supposed to be 3.333333. But we get the result as 3.000000. Can you think why? Because the two operands of the expression, value1 and value2 are of int type. This causes compiler to treat the result also as integer; that means, it will ignore the decimal part of the result and store the integer result in float variable.

So how do you get the result with correct precision? Just cast the type of expression to the type you need! You can see the solution below.

#include <stdio.h>

int main()
   int value1 = 10, value2 = 3;
   float result;

   result = (float)value1 / value2;
   printf("Result with type casting: %f", result);
   return 0;

Result with type casting: 3.333333

One important thing to notice here is that, type casting has more precedence than the divide operation (/). At first, value1 will be converted to float type and then division operation is performed on the float variable.

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Implicit and Explicit Type Casting

Type casting can either be performed by the compiler automatically or user can specify them using the syntax described above. Former type is called implicit type casting and the latter is called explicit type casting.

What is implicit type casting/ implicit type conversion?

We've explained the explicit type conversion in the above paragraphs. Now lets's take a look at how implicit type conversion works. In implicit type conversion compiler takes care of the casting without requiring the programmer to specify them explicitly. For example in the program given below, compiler converts the sum of two float numbers to an int.

##include <stdio.h>

int main()
   float value1 = 2.2;
   float value2 = 3.3;
   int result;

   result = value1 + value2;
   printf("Result : %d", result);
   return 0;

Result : 5

Note: Though compiler performs implicit type casting, it's always recommended to specify the conversion explicitly because this improves the code readability and makes it easier to port to other platform.

Rules for Implicit Type Casting

When different operands of an expression are of different type, complier performs implicit type conversion to match them. Below are the rules followed by compiler to perform the implicit conversion.

  • Integer Promotion: All types lower than int (char, short etc) are first promoted to int.
  • If the types of operands differ even after integer promotion, then following actions are taken
    • If one of the operand is long double, convert all others to long double
    • If one of the operand is double, convert all others to double
    • If one of the operand is float, convert all others to float
    • If one of the operand is float, convert all others to float
... This operation proceeds from the highest type to lowest.

Order of the data types from highest to lowest is given below

long double > double > float > unsigned long long > long long > unsigned long > long > unsigned int > int

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Types of Type Casting in C: Upcasting and Downcasting

There are two type of casting available in C language, known as upcasting and downcasting. Upcast means converting lower (like int) to higher (float, long int) data type. The reverse is called downcast.
Upcast results in no information loss. But downcast results in information loss as lower data type have lesser bits and can hold the lesser amount of information/data. One data type considers higher if maximum value allowed to store in it is greater than the other data type. For example, float is lower compared to double because double can store more precisions

Second program in this chapter is example of upcasting(int value is converted to float) and the third program is example of down casting(float is converted to int)

C Type Casting with examples for Float to Int/Int to Char

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern