xuyejin 发表于 2017-6-30 09:35:23

stm32f203 使用vsprintf为什么不能显示浮点数?%f没用?

各位大侠您们好

         有个新问题请教,我要是用 vsprintf显示字符,为什么%d,%s都可以,而%f一直都是0.000000?
这个问题可是奇怪了,还有大侠指教下啊?谢谢

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>


void Uart_Printf(char *pTransBuf,char *fmt,...)
{
        u8 i;
       
        va_list ap;

      va_start(ap,fmt);
      vsprintf(pTransBuf,fmt,ap);
      va_end(ap);

}

数据填入pTransBuf缓冲区,使用%f,固定就填0.000000,晕死

xuyejin 发表于 2017-6-30 09:36:16

使用的是是stm32f207,内部存储器

xuyejin 发表于 2017-6-30 13:36:34

唉,没有人指教?怎么我尽碰到这种稀奇古怪的问题

xuyejin 发表于 2017-7-3 11:22:48

顶一下,怎么没有人指教啊?

xuyejin 发表于 2017-7-5 15:11:22

怎么没用回复?都没有碰到过么?

发表于 2017-7-5 16:21:32

楼主,没有碰到类似的问题,我一般使用sprintf,而且是直接调用。还是你编程的问题要多一些。ap这个值是什么类型?

xuyejin 发表于 2017-7-28 11:42:44

我发现和stemwin 的 GUI_TOUCH_Exec(); 有关,不使用GUI_TOUCH_Exec(); 就没有问题,一循环调用
GUI_TOUCH_Exec(); 就出问题了,这个是怎么回事情?

xuyejin 发表于 2017-7-28 11:48:20

就是使用sprintf也是一样,有指教的么?

kylongmu 发表于 2017-7-28 15:10:57

extern _ARMABI int fprintf(FILE * __restrict /*stream*/,
                  const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1,2)));
   /*
    * writes output to the stream pointed to by stream, under control of the
    * string pointed to by format that specifies how subsequent arguments are
    * converted for output. If there are insufficient arguments for the format,
    * the behaviour is undefined. If the format is exhausted while arguments
    * remain, the excess arguments are evaluated but otherwise ignored. The
    * fprintf function returns when the end of the format string is reached.
    * The format shall be a multibyte character sequence, beginning and ending
    * in its initial shift state. The format is composed of zero or more
    * directives: ordinary multibyte characters (not %), which are copied
    * unchanged to the output stream; and conversion specifiers, each of which
    * results in fetching zero or more subsequent arguments. Each conversion
    * specification is introduced by the character %. For a description of the
    * available conversion specifiers refer to section 4.9.6.1 in the ANSI
    * draft mentioned at the start of this file or to any modern textbook on C.
    * The minimum value for the maximum number of characters producable by any
    * single conversion is at least 509.
    * Returns: the number of characters transmitted, or a negative value if an
    *          output error occurred.
    */
#pragma __printf_args
extern _ARMABI int _fprintf(FILE * __restrict /*stream*/,
                     const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1,2)));
   /*
    * is equivalent to fprintf, but does not support floating-point formats.
    * You can use instead of fprintf to improve code size.
    * Returns: as fprintf.
    */
#pragma __printf_args
extern _ARMABI int printf(const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1)));
   /*
    * is equivalent to fprintf with the argument stdout interposed before the
    * arguments to printf.
    * Returns: the number of characters transmitted, or a negative value if an
    *          output error occurred.
    */
#pragma __printf_args
extern _ARMABI int _printf(const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1)));
   /*
    * is equivalent to printf, but does not support floating-point formats.
    * You can use instead of printf to improve code size.
    * Returns: as printf.
    */
#pragma __printf_args
extern _ARMABI int sprintf(char * __restrict /*s*/, const char * __restrict /*format*/, ...) __attribute__((__nonnull__(1,2)));
   /*
    * is equivalent to fprintf, except that the argument s specifies an array
    * into which the generated output is to be written, rather than to a
    * stream. A null character is written at the end of the characters written;
    * it is not counted as part of the returned sum.
    * Returns: the number of characters written to the array, not counting the
    *          terminating null character.
    */
extern _ARMABI int vsprintf(char * __restrict /*s*/,
                     const char * __restrict /*format*/, __va_list /*arg*/) __attribute__((__nonnull__(1,2)));
   /*
    * is equivalent to sprintf, with the variable argument list replaced by
    * arg, which has been initialised by the va_start macro (and possibly
    * subsequent va_arg calls). The vsprintf function does not invoke the
    * va_end function.
    * Returns: the number of characters written in the array, not counting the
    *          terminating null character.
    */---------------------------------------------

看stdio.h头文件里面,我颜色标明的部分,已经说明不支持float了。

kylongmu 发表于 2017-7-28 15:32:13

我的767用printf,还是sprintf都可以打印浮点的。
头文件里面有个编译开关:
#pragma __printf_args
控制着具体的函数实现方式,你用CUBEMX生成工程看看,可能CUBE默认的编译开关支持浮点。
sprintf用起来真不省心,很有可能不是你想象的结果。
页: [1]
查看完整版本: stm32f203 使用vsprintf为什么不能显示浮点数?%f没用?