Data Type Conversion in C

In an assignment statement if the type of the right side is different from the type of the left side the right side is converted into the type of that of the left. This process may be problematic when the type of the left side is smaller than the type of the right side.

#include <stdio.h>

int main(void){
 
  char ch;
  int i;
 
  i = 1984;
  ch = i;
 
 
  printf("%d\n", i);
  printf("%d\n", ch);
 
  return 0;
 
}

Also, when converting from a floating point number to an integer number the fractional part is lost.

#include <stdio.h>

int main(void){

 int i;
 double f;
 
 f = 394.4075;
 i = f;
 
 printf("%f \t %d\n", f, i);
 
 return 0;
 
}

If we perform a binary operation on two different data types, the compiler will convert the more restrictive data type to the less restrictive data type. The simplest conversion is the integer promotion. For instance, in a mathematical operation involving a char and an int, the char is automatically converted to the larger, less restrictive int data type. Likewise, if one operand is of type int and the other is of type unsigned int, the first operand is promoted to the unsigned type.

#include <stdio.h>

int main(void){
 
 int a = 6809;
 char b = 73;
 int sum;

 unsigned c = 26007800;
 
 unsigned usum = c + a;
 
 sum = a + b;
 
 printf("%d + %d = %d\n", a, b, sum);
 printf("%d + %u = %u\n", a, c, usum);
 
 return 0;
 
 
}

Often it is necessary to temporarily change the type of a variable so that it can be used differently. This is known as performing a type cast. A type cast in no way permanently changes the variable’s type or the value it stores; a type cast causes only a temporary type change.

 The cast operator is what is known as a unary prefix operator, it consists of a type name inside parentheses placed in front of the variable name.

#include <stdio.h>


int main(void){

  int d = 1999;
  int e = -1492;
 
  printf("d = %d\n", d);
  printf("d = %d (whoops)\n", (char)d);
  printf("d = %f\n", (float)d);
  printf("e = %d\n", e);
  printf("e = %f\n", (float)e);
  printf("e = %u (whoops)\n", (unsigned)e);
 
 
 return 0;
 
}

The value of any variable can be cast to any type, but the results may not always be desirable.

Type casts are very useful when working with functions that require arguments of a different data type than that of the variable we wish to use. For instance, the sqrt() library function requires a single argument of type double, and returns a double value. We must convert any argument we send to the sqrt() function to the double type.

#include <stdio.h>
#include <math.h>

int main(void){
    
  int i = 7800;
 
  printf("square root of %d = %f\n", i, sqrt((double)i));
 
  i = 121;
 
  printf("square root of %d = %f\n", i, sqrt((double)i));
 
  return 0;
 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s