Warning : C2705: Possible loss of data

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Warning : C2705: Possible loss of data

1,142 Views
AndersJ
Contributor IV

CodeWarrior 6.3 for HCS08.

Why do I get warning C2705 in the division statement below?

static void Do_SM_PostStateTasks(void)
{
  int8_t A;
  int8_t B;
  int8_t C;
  
  A = 10;
  B = 20;
  C = (A / B);  // <---- WARNING HERE - WHY
  
  SM.StateTimer = C;
}
Labels (1)
2 Replies

710 Views
BlackNight
NXP Employee
NXP Employee

Hi Anders,

the division requires integral promotion in C language. So both A and B will be expanded to int, with an int division. The result is again int (16bit on the HCS08) and then gets assigned to an 8bit variable C. At that point you will loose the upper 8 bit, and that's what the warning is about.

I hope this helps,

Erich

710 Views
AndersJ
Contributor IV

Thank you Erich,

for ending my head scratching session.

Much appreciated,

Anders

0 Kudos