122. Data Conversion and Encoding-ii

Question.6

A developer needs to display 23.7 on an LCD using UART. The MCU has only 1 KB RAM and no printf/sprintf. Which approach is best?

 

Option A:

sprintf(buf, "%.1f", temp);  // Pull in printf library

Option B:

int int_part = (int)temp;        // 23
int frac_part = (int)((temp - int_part) * 10);  // 7
buf[0] = (int_part / 10) + '0';  // '2'
buf[1] = (int_part % 10) + '0';  // '3'
buf[2] = '.';
buf[3] = frac_part + '0';        // '7'
buf[4] = '\0';

Option C:

ftoa(temp, buf, 1);  // Non-standard function

Which is the most reliable for this constraint?

Need Help? Refer to the Quick Guide below

Select Answer