Microcontroller Embedded C Programming Lecture 47| Typecasting in ‘C’ contd

  • Post author:
  • Post category:Blog

 

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.

Typecasting in 'C'
Figure 1. Warning

 

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

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply