c语言 - sprintf 中的 %f 似乎在 TI 84 CE 在线 IDE 中不起作用



当我在计算器上运行这个程序时:

void main(void) {
    char *quot = malloc(10 * sizeof(char));
    char *rest = malloc(10 * sizeof(char));
    sprintf(quot, "%d", 5);
    printText(quot, 0, 0);
    sprintf(rest, "%f", 2.03);
    printText(rest, 0, 1);
}
我的TI 84 CE计算器的printText函数:
void printText(const char *text, uint8_t xpos, uint8_t ypos) {
    os_SetCursorPos(ypos, xpos);
    os_PutStrFull(text);
}

这是我的计算器LCD上的输出:

5
%

有一个百分比令牌而不是2.03,这背后的原因是什么?

我包含了这些库:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tice.h> // this is for my TI84

%f由于计算器的内存限制而被禁用。浮点转换是昂贵的。使用的C语言是C89,这是最好的C语言。无论如何,您可以通过在makefile中添加以下行来为您的程序启用%f:

USE_FLASH_FUNCTIONS := NO

这将大大增加二进制文件的大小,因此建议实现%f的自定义限制版本。或者,这段代码将做与%f相同的事情,但将输出复制到字符数组中。

void float2str(float value, char *str) {
    real_t tmp_real = os_FloatToReal(value);
    os_RealToStr(str, &tmp_real, 8, 1, 2);
}

最新更新