element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog efficient sprintf() string format lib for lower range microcontrollers
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 4 Oct 2025 7:17 PM Date Created
  • Views 24 views
  • Likes 1 like
  • Comments 0 comments
  • MSPM0L1105
  • MSPM0
  • embedded
  • easyL1105
Related
Recommended

efficient sprintf() string format lib for lower range microcontrollers

Jan Cumps
Jan Cumps
4 Oct 2025

I'm learning the ins and outs of an MSPM0 controller. I'm using an MSPM01105, that's low on power use. But also low on available memory.
I tried to use the standard C library's sprintf() function in a project, to format a string with a few integers. That's not possible. The function exhausts this Arm M0's resources.

So I went looking for alternatives. One of those is the printf library. It does a great job at delivering a lot of the stdlib functionality, cheaper. Of course, it's all based on compromises. But definitely usable.

Using printf

Your project will need 2 files: printf.c and printf.h. Once you add those to your project, you can start using the sprintf() function. It has the same formatting commands as the stdlib one. Just less options. That's what I use. I don't use this library to immediately printf() to UART. I use sprintf() to format memory buffer strings, that can later be sent by native buffered UART.

If you also want to use the printf() to print formatted text directly to UART,  you'll have to write a putchar_() function that knows how to output data to your UART. Else a printf() will do nothing.

Example:

#include "printf.h"
#define buf_len 16
#define fifo_len 4

char txPacket[buf_len];

void stream_data() {
    sprintf(txPacket, "[%+5i %+5i]\n", myIntA, myIntB);
    for(uint32_t i = 0; i < buf_len / fifo_len; i++) {

        /* Fills TX FIFO with data and transmits the data */
        DL_UART_Main_fillTXFIFO(UART_0_INST, &txPacket[0 + i * fifo_len], fifo_len);

        /* Wait until all bytes have been transmitted and the TX FIFO is empty */
        while (DL_UART_Main_isBusy(UART_0_INST)) ;
    }    
}

Lowering the footprint further:

If you don't need to format floats and exponential, you can set two symbols to 0:

PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS=0
PRINTF_SUPPORT_DECIMAL_SPECIFIERS=0

image

This takes care that the most expensive chunks of code don't get added to your project, if you don't need them.

Example

Here is example output, created with sprintf(txPacket, "[%+5i %+5i]\n", myIntA, myIntB):

image

thank you for reading.

Related posts

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube