Typecasting in ‘C’ contd
In this article, let’s continue our discussion about Typecasting.
Here, I would like to include some other data in this code, some different data. This time I use 6 byte data and 2 byte data. Let me write the 6 byte data, so I’ll write it in hex, 0x1FFFFFFFA0B0. Now let me add 2 byte data to this. Let me write 0x1245, as shown below.
#include<stdio.h> int main(void) { unsigned char data = 0x1FFFFFFFA0B0 + 0x1245; float result = 80 / (float)3 ; printf("Data : %u result : %f \n",data , result); }
6 byte data cannot be represented by int data type. The compiler will consider 0x1FFFFFFFA0B0 as a long long int, not as an int. And the compiler treats 0x1245 also as a long long int data. That means there is an implicit casting on this number to upgrade its data type from int to long long int. This is the addition of a long long int with another long long int.
Let’s see what happens when you try to compile this code. There is a warning saying you are trying to store a long long int data to a variable whose type is char, and it is warning you about the data loss.
So, with that note, I would like to end the discussion on typecasting, and we are going to encounter typecasting again when we cover pointers. And it is very important when dealing with pointers.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1