Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


sprintf in FreeRTOS

Posted by pugglewuggle on November 23, 2015

I saw a couple posts asking about thread safety for sprintf online. Is it safe to use sprintf from stdio.h within a FreeRTOS task or is that a bad idea? If it is not a good idea, what is the alternative because I really need this functionality.

sprintf in FreeRTOS

Posted by rtel on November 23, 2015

The short answer is "it depends on the chip, compiler, and library you are using".

The longer answer is that in some situations in which FreeRTOS is used it is ok to call sprintf() from multiple tasks, unlike printf() which will always generate a conflict when accessing whichever console is used. However, if you are using a standard GCC sprintf(), then be aware that it will use a huge amount of stack, and might pull in all the floating point libraries which will have the effect of doubling the binary image size.

Many of the demo applications in FreeRTOS include a file called prtinf-stdarg.c which contains a small and limited but very stack light sprintf() implementation. Be aware that not all the printf-stdarg.c files implement snprintf() as they just ignore the buffer size argument. The FreeRTOS+ download contains some more featured implementations too.


sprintf in FreeRTOS

Posted by heinbali01 on November 23, 2015

Hi Pugglewuggle

The [v]s[n]printf family is very useful but it its definitions and usage are a bit controversial ( see e.g. http://linux.die.net/man/3/snprintf )

Some implementations are stack hungry, up to > 1 KB. Some implementations are not interrupt-proof. You will have to try it out to know.

I attached an implementation that is interrupt-proof and uses a modest amount of stack, about 80 bytes.

snprintf : the official versions may return an amount that it bigger that the size supplied. If that happens, the result may not nul terminated (GCC and MSC that I just tried do write a nul at the end).

The attached version will always write a terminating nul and it will return at most size - 1 bytes.

~~~~~ char buffer[ 6 ]; int rc = snprintf( buffer, sizeof buffer, "%s", "Hello world" ); ~~~~~

official snprintf returns "Hello" with value 11 attached snprintf returns "Hello" with value 5



printf-stdarg.c (14134 bytes)

sprintf in FreeRTOS

Posted by heinbali01 on November 23, 2015

After I pressed "Post" I saw that Richard had responded earlier :-)

The source that I attached indeed contains the full-featured version of [v]s[n]printf() with an extensive printf syntax and that also checks for the buffer size. You find it in the The FreeRTOS+ downloads.

Note that the usage of the user-provided function:

~~~~~ void vOutputChar( const char cChar, const TickType_t xTicksToWait ) ~~~~~

is a bit historical. The idea was to that if the buffer is NULL, the function vOutputChar() will be called for each character:

~~~~~ sprintf( NULL, "This output goes to the user functionn" ); ~~~~~


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner