The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+3 votes
asked in Programming by Junior (909 points) | 14.3k views

1 Answer

+3 votes

Type conversion

  • Type conversion occurs when the expression has data of mixed data types. 
  • example of such expression include converting an integer value in to a float value, or assigning the value of the expression to a variable with different data type.
  • In type conversion, the data type is promoted from lower to higher because converting higher to lower involves loss of precision and value.
  • For type conversion, C following some General rules explained below 
    • Integer types are lower than floating point types
    • Signed types are lower than unsigned types
    • Short whole number types are lower than longer types
    • double>float>long>int>short>char

In other word :

Type Conversion refers to translating of values (roughly speaking, contents of the object), so that they may be interpreted as belonging to a new type.

Example : 

double  d;
long    l;
int     i;

if (d > i)   d = i;
if (i > l)   l = i;
if (d == l)  d *= 2;

Type Casting (or) Explicit Type conversion:

  • Explicit type conversions can be forced in any expression , with a unary operator called a cast.
  • Syntax is 

             (type-name) expression;

  • Example 

int n;
float x;

  • The above statement will convert the value of n to a float value before assigning to x.but n is not altered
  • Type casting does not change the actual value of the variable but the resultant value may be put in temporary storage.
  • The cast operator has the same high precedence as other unary operators.
  • The Typecasting should not be used in some places.such as
  • Type cast should not be used to override a const or volatile declaration.overriding these type modifiers can cause the program to fail to run correctly.
  • Type cast should not be used to turn a pointer to one type of structrure or data type in to another.
  • Example of type casting using pointers

    void *temp; //void pointer
    char c='a',*ch="hello";
    int i=10;
    printf("char=%c\n",*(char *)temp);
    printf("string=%s\n",(char *)temp);
    printf("i=%d\n",*(int *)temp);
    return 0;
i=10       //here temp is a void pointer.temp is used to typecast to anyother pointer


answered by Junior (807 points)
What is the difference between implicit and explicit type casting and what is the problem due to this and it's solution

I think you should read the answer first and I am pretty sure that you won't get question like "What is the difference between implicit and explicit type casting and what is the problem due to this".In some language like Java Type conversion refere to Implicit and TypeCasting refer to Explicit.I haven't heard implicit in C but in java many time.

Now in Java implicit done by compiler if the type is in the range else compiler throw an exception like ClassCastException and in explicit you have to request to compiler that "Hey compiler trust me, the given source object or type is same as destination object or type ".Then compiler cast your source type to destination type and move forward.

For more info just google ,you will get plenty of answers.

In c also implicit type conversion

Int k;


You will get k=0

When we need explicit type casting
why should type casting not be used to turn a pointer to one type of structure or data type in to another? any specific disadvantage of it ?

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,718 questions
40,263 answers
38,900 users