我正在使用Kem版本5程序Stm32f4xx

我想使用带有“ A:float1 float2”格式的printf函数。

I want to float2 have to start 11.column.

    When float1=225.4 and float2=245.1:
    A:225.4V  45.1A
    When float1=0 and float2=0
    A:0.0V    0.0A


我的代码1:

char EA1[10]
char EA2[10]
CHAR line[20]

sprintf(EA1,"          ");
sprintf(EA1,"A:%3.1fV",float1);
sprintf(EA2,"%3.1fA    ",float2);
LCD_Set_Cursor(1,1)
printf(%s %s,EA1,EA2);


我的代码2:

LCD_Set_Cursor(1,1);
printf("A:%3.1fV  ",float1);
LCD_Set_Cursor(11,1);
printf("%3.1fA    ",float2);


当float1 = 0和float2 = 0时,mycodes-1 float2的输出从8开始

mycodes-2 float2的输出无法正常工作。 float2有时9.column有时11.column

最佳答案

您可以使用固定位置组成字符串,例如:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

void printLine(char *s, size_t size, uint16_t position, float float1, float float2)
{
    if ((position > 0) && (position<size))
    {
        int index = 0;
        index += sprintf(s, "A:%3.1fV", float1);
        memset(&s[index], 0x20, size - index);
        sprintf(&s[position - 1], "%3.1fA", float2);

        printf("%s\n", s);
    }
}

int main(void)
{
    char LINE[128] = { 0 };

    printf("         1         2\n");
    printf("12345678901234567890\n");
    printLine(LINE, sizeof(LINE), 11, 225.4f, 41.5f);
    printLine(LINE, sizeof(LINE), 11, 0, 0);

    return 0;
}


输出值

         1         2
12345678901234567890
A:225.4V  41.5A
A:0.0V    0.0A

关于c - 打印移位错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42021027/

10-11 21:01