Type Conversion in C: Understanding Implicit and Explicit Changes

Type conversion in C refers to the process of changing one data type to another, either automatically by the compiler (implicit conversion) or manually using type cast operators (explicit conversion). This section discusses the rules and processes involved in type conversion for effective data handling.



Type Conversion in C

In C, type conversion is the process of changing one data type to another. This can occur implicitly by the compiler or explicitly using type cast operators.

Implicit Type Conversion

Implicit type conversion happens automatically when the compiler converts a smaller data type to a larger one. This ensures compatibility and preserves data integrity.

Rules for Implicit Conversion:

  • char and short values are promoted to int.
  • If one operand is long, the entire expression is promoted to long.
  • If one operand is float, the entire expression is promoted to float.
  • If any operand is double, the result is promoted to double.

Example 1: Integer Promotion


#include <stdio.h>

int main() {
    int i = 17;
    char c = 'c';  /* ASCII value is 99 */
    int sum = i + c;
    printf("Value of sum: %d\n", sum);
    return 0;
}
    

Output: Value of sum: 116

The character c is promoted to its ASCII value (99) before being added to i.

Example 2: Usual Arithmetic Conversion


#include <stdio.h>

int main() {
    char a = 'A';
    float b = a + 5.5;
    printf("%f", b);
    return 0;
}
    

Output: 70.500000

The char variable a is promoted to float before the addition.

Explicit Type Conversion

Explicit type conversion, or type casting, allows you to convert a data type to another intentionally. This is done using the type cast operator.

Syntax:


type2 var2 = (type1) var1;

Use explicit casting when converting a larger data type to a smaller one or between unrelated types.

Example 3: Typecasting


#include <stdio.h>

int main() {
    int x = 10, y = 4;
    float z = (float)x / y;
    printf("%f", z);
    return 0;
}
    

Output: 2.500000

Typecasting one operand to float ensures the division is performed with floating-point precision.

Typecasting Functions in C

atoi() Function

The atoi() function converts a string to an integer.


#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "123";
    int num = atoi(str);
    printf("%d\n", num);
    return 0;
}
    

Output: 123

itoa() Function

The itoa() function converts an integer to a string.


#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = 123;
    char str[10];
    itoa(num, str, 10);
    printf("%s\n", str);
    return 0;
}
    

Output: 123

Other Examples

Typecasting is also used with functions like malloc() for dynamic memory allocation:


int *ptr = (int*)malloc(n * sizeof(int));
    

Or in function arguments and return values:


float divide(int a, int b) {
    return (float)a / b;
}
    

Type conversion helps in achieving type safety and code clarity, but may also lead to loss of precision or confusion in complex cases.