Typecasting in C

Typecasting is simply a mechanism by which we can change the data type of a variable, no matter how it was originally defined. When a variable is typecasted into a different type, the compiler basically treats the variable as of the new data type.

Example

In the above example, first we divide 'a' by 'b' without typecasting. While in the second attempt we typecast both 'a' and 'b' to float and then divide them.

So, what happens is, in the first attempt, the compiler knows both 'a' and 'b' are integers so it ignores the floating part of the result and stores the result of a/b in a temporary integer variable and then assign the value of this temporary integer variable to 'c'. So despite of 'c' being a float, the final value that 'c' gets is an integer ie '0' in this case.

While in the second attempt, as both 'a' and 'b' are individually typecasted as 'float', so this typecasting makes compiler think as if both 'a' and 'b' are floats, so the compiler retains the floating part of the result and assigns the float value to 'c'.

The output of the above program :

[0.000000]

[0.625000]

Typecasting with pointers

The above example was good enough to know typecasting but its the pointer arithmetic where typecasting really makes huge difference.

Firstly, we made the character pointer to point character array and integer pointer to point integer array. In the loop we tried to print the character array elements by incrementing character pointer. Since each character in character array occupies one byte and a character pointer also increments one byte when '1' is added to it so every thing went as desired. Same is the case with integer array and integer pointer, an integer occupies 4 bytes and an integer pointer increments by four bytes when '1' is added to it.

Here what we did was, we pointed charcter pointer to integer array and integer pointer to character array. So, lets say when we incremented charcter pointer by '1', it incremented by only one byte. Now since an integer occupies 4 bytes in memory, so this character pointer instead of pointing the next integer (which is 4 bytes away), it points to the second byte of the 1st integer and hence we get whatever value was lying there. Same is the case with integer pointer which jumped 4 bytes when incremented by '1' and hence printed the 4th character in array instead of second.

So, as a solution to the problem we faced in 'B' above, this time whenever we incremented the pointer, we typecasted it according to the values it is pointing to, i.e, if character pointer is pointing to integer, then it is typecasted to integer pointer and then incremented by 1. So now after the typecasting is done, the compiler sees a charcter pointer as an integer pointer and increments it by 4. Same is whith integer pointer, which when typecasted to charcter pointer is treated as character pointer and is incremented only once.

Conclusion

So we saw that through typecasting the programer can give a completely different picture of the memory (being typecasted) to the compiler and the compiler works on the memory thinking of it as of typecasted data type for that particular instruction.