sprintf Function

Purpose

Returns a string with formatted data.

Syntax

$ = sprintf[$](format$ [, argument] ... )

format:sexp
argument, …:aexp

Description

The sprintf function formats and stores a series of characters and values in string. Each argument (if any) is converted and output according to the corresponding format specification in format.

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called “escape sequences.” To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant. Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark (").

Escape sequence for sprintf are:

"\a" Chr(7) Bell alert
"\b" Chr(8) Backspace
"\e" Chr(27) Escape
"\t" Chr(9) Vertical tab
"\n" Chr(10) New line
"\r" Chr(13) Carriage return
"\f" Chr(12) Formfeed
"\v" Chr(11) Vertical tab.
"\\" Backslash
"\%" % an expansion
"\#nnn" ASCII character in decimal notation (\#27 is similar to Chr(27)).
"\ooo" ASCII character in octal notation (\033 is similar to Chr(0o033); each o represents only one octal digit (0..7)).
"\xhhh" ASCII character in hexadecimal notation (\x1b is similar to Chr(0x1b), each h represents one hexadecimal digit (0..9a..fA..F)).
"\ " \Space: this is no sequence like the others before, its purpose is to end \character codes: "\10\ 33" "\b33", but "\1033" results in "C3".

Format specifications

Format specifications always begin with a percent sign (%) and are read left to right. When sprintf encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the format specifications.

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [. precision] [{h | l | L}]type

h | l | L Optional prefixes to type-that specify the size of argument

h - short

I - long int

L - Large, int64

Print sprintf("%Li", Large 120986754678// 120986754678

Print sprintf("%Ii", Large 120986754678// 727670390


Type Meaning
%d Signed decimal integer (Int)
%i Signed decimal integer (Int)
%x Unsigned hexadecimal integer, using "abcdef". (Int)
%X Unsigned hexadecimal integer, using "ABCDEF". (Int)
%o Unsigned octal integer (Int)
%f Signed value having the form [ - ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. (Double)
%e Signed value having the form [ - ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or -. (Double)
%E Identical to the e format except that E rather than e introduces the exponent. (Double)
%g Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than -4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. (Double)
%G Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). (Double)
%s String. Characters are printed up to the first null character or until the precision value is reached.
%c Character

Flags

The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.

Specification Meaning
- Left align the result within the given field width. Right align.
+ Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-).
0 If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored. No padding.
blank (' ') Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears.
# When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively. No blank appears.
When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Decimal point appears only if digits follow it.
When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.
Ignored when used with c, d, i, u, or s.
Decimal point appears only if digits follow it. Trailing zeros are truncated.

Print sprintf("%+i", -255) // Prints -255

Width

The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values - depending on whether the - flag (for left alignment) is specified - until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).

The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).

Debug.Show

Debug.Print sprintf("%6i", 0)   // "     0"

Debug.Print sprintf("%6s", "xx")// "    xx"

Precision

The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits. Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.

Type Meaning
c, C The precision has no effect. Character is printed.
d, i, o, x, X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. Default precision is 1.
e, E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
f The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
g, G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated.
s, S The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered.

Print sprintf("%+.8i", -255) // "-00000255"

Print sprintf("%+.4e", -255) // "2.5500e+002"

Example

Print sprintf("%d is in octal %o", 255, 255)

Print sprintf("%d is in int %i", 255, 255)

Print sprintf("%d is in hexadecimal %x", 255, 255)

Print sprintf("%d is in hexadecimal %X", 255, 255)

Print sprintf("%d is in double %f", 255, 255)

Print sprintf("%d is in double %e", 255, 255)

Print sprintf("%d is in double %E", 255, 255)

Print sprintf("%d is in compact double %g", 255, 255)

Prints:

255 is in octal 377
255 is in int 255
255 is in hexadecimal ff
255 is in hexadecimal FF
255 is in double 255.000000
255 is in double 2.550000e+002
255 is in double 2.550000E+002
255 is in compact double 255

Remarks

If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

Print sprintf("%.0i", 0) // no output

sprintf is C-compatible function. The GFA-BASIC 32 functions Format(), Dec() are easier to use.

See Also

Format, Hex$(), Oct$(), Dec$(), Using

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}