FUNCTION SPRINTF ("C format string", ParamArray ...) AS STRING

built-in FUNCTION that calls the existing SPRINTF routine in the firmware for versions 8.11 or later (version 7.51 or later on ARM7 parts)

SPRINTF is a way to format strings. It can be as simple a short string or quite complex specifying field sizes and pad characters for a number. This routine is written in C and is part of the firmware. As of 8.11 firmware BASIC programs can access this powerful C subroutine.

Rather than duplicate the documentation of SPRINTF, there is a great deal of information for printf in documentation for C such as

C reference for printf

SPRINTF in the firmware supports c (BYTE), s (STRING), d (decimal INTEGER, x or X for (hex INTEGER), e, E, f, g, or G (for SINGLE).

The G format differs a little from the G format of C. It copies 0.0 or the range 0.1000000 through 9999999. and outside that it uses D.DDDDDDE+DD and the negative values as well.

Pad characters of 0 or space can be added to fill out LENGTH or WIDTH and PRECISION.  PRECISION is limited to 0 to 8 characters.

Special characters can be added using %c and filling that value with something like 13 for carriage return, 9 for TAB or 7 to wring the BELL


DIM str(100) AS STRING

i = 1234

x = 12.34

str = SPRINTF("%c%c a string a single %1.5f an integer %d a hex number %x and a carriage return %c",13,13,x,i,i,13)

will print --

a string a single 12.34000 an integer 1234 a hex number 4D2 and a carriage return

Differences from other BASICs

See also