Type casting in C programming is the process of converting a value from one data type to another.
It allows us to explicitly change the data type of a variable or an expression.
(type_name) expression
"type_name" is the target data type we want to cast to.
"expression" is the value or variable we want to cast.
Implicit Conversion: Some conversions are performed implicitly by the compiler, such as promoting smaller data types to larger ones (e.g., int to float), or converting between compatible types.
Explicit Conversion: You can explicitly cast a value to a different data type using the cast operator (type_name).
Loss of Data: When performing type casting, you may lose data if the target type cannot represent the full range or precision of the original value.
Integer Division: Be cautious when casting the result of integer division to another data type, as the fractional part may be truncated.
Pointer Type Casting: Type casting is also used for converting pointer types, but it requires careful consideration to avoid undefined behavior.
Here are some examples of type casting
#include <stdio.h> int main() { int integerNum = 10; float floatNum; // Implicit type conversion from int to float floatNum = integerNum; printf("Integer number: %d\n", integerNum); printf("Float number: %f\n", floatNum); return 0; }
Integer number: 10 Float number: 10.000000
Note: the integer value 10 is implicitly converted to a floating-point value 10.000000 when assigned to "floatNum" variable.
#include <stdio.h> int main() { float floatNum = 3.14; int integerNum; // Explicit type casting from float to int integerNum = (int)floatNum; printf("Float number: %f\n", floatNum); printf("Integer number: %d\n", integerNum); return 0; }
Float number: 3.140000 Integer number: 3
Here, the floating-point value 3.14 is explicitly cast to an integer, resulting in the integer value 3. The fractional part is truncated during the conversion.
#include <stdio.h> int main() { int numerator = 10; int denominator = 3; float result; // Type casting in arithmetic operation result = (float)numerator / denominator; printf("Result: %f\n", result); return 0; }
Result: 3.333333
Note: type casting is used to ensure that the division operation is performed in floating-point arithmetic, resulting in a more accurate result (3.333333) compared to integer division (3).
#include <stdio.h> int main() { int num = 10; int *ptr = # char *charPtr; // Pointer type casting charPtr = (char *)ptr; printf("Integer value: %d\n", num); printf("Character value: %c\n", *charPtr); return 0; }
Integer value: 10 Character value:
Note: an integer pointer "ptr" is cast to a character pointer "charPtr". The character value obtained through "charPtr" may not represent the original integer value in a meaningful way due to differences in memory interpretation.
Type casting is a powerful feature in C programming that allows us to manipulate data types as needed, but it requires careful consideration to ensure correct behaviour and avoid unintended consequences.