Formatting Data in eScript

The print family of functions and scan family of functions both use format strings to format the data written and read, respectively.

Formatting Output in eScript

Table 3 lists the format strings for use with the print family of functions: fprintf() (read Clib.fprintf() Method), rsprintf(), and sprintf() (read Clib.rsprintf() Method). In these functions, characters are printed as read to standard output until a percent character (%) is reached. The percent symbol (%) indicates that a value is to be printed from the parameters following the format string. The form of the format string is as follows:

%[flags][width][.precision]type

To include the % character as a character in the format string, use two % characters together (%%).

Table 3. Format Strings for the Print Family of Functions

Formatting Character

Effect

Flag Values

-

Left justification in the field with space padding, or right justification with zero or space padding

+

Force numbers to begin with a plus (+) or minus (-)

space

Negative values begin with a minus (-); positive values begin with a space

#

Append one of the following symbols to the # character to display the output in the indicated form:

o to prepend a zero to nonzero output

x or X to prepend 0x or 0X to the output, signifying hexadecimal

f to include a decimal point even if no digits follow the decimal point

e or E to include a decimal point even if no digits follow the decimal point, display the output in scientific notation, and remove trailing zeros

g or G to include a decimal point even if no digits follow the decimal point, display the output in scientific notation, and leave trailing zeros in place

Width Values

n

At least n characters are output; if the value is fewer than n characters, the output is padded with spaces

0n

At least n characters are output, padded on the left with zeros

*

The next value in the parameter list is an integer specifying the output width

Precision Values

If precision is specified, then it must begin with a period (.) and must take one of the following forms:

.0

For floating-point type, no decimal point is output

.n

Output is n characters, or n decimal places if the value is a floating-point number

.*

The next value in the parameter list is an integer specifying the precision width

Type Values

d,i

Signed integer

u

Unsigned integer

o

Octal integer

x

Hexadecimal integer using 0 through 9 and a, b, c, d, e, f

X

Hexadecimal integer using 0 through 9 and A, B, C, D, E, F

f

Floating-point of the form [-]dddd.dddd

e

Floating-point of the form [-]d.ddde+dd or [-]d.ddde-dd

E

Floating-point of the form [-]d.dddE+dd or [-]d.dddE-dd

g

Floating-point number of f or e type, depending on precision

G

Floating-point number of F or E type, depending on precision

c

Character; for example, 'a', 'b', '8'

s

String

Formatting Input in eScript

Format strings are also used with the scan family of functions: fscanf() (read Clib.fscanf() Method), sscanf() (read Clib.sscanf() Method), and vfscanf(). The format string contains character combinations that specify the type of data expected. The format string specifies the admissible input sequences and how the input is to be converted to be assigned to the variable number of parameters passed to the function. Characters are matched against the input as read and as it matches a portion of the format string until a percent character (%) is reached. The percent character indicates that a value is to be read and stored to subsequent parameters following the format string.

Each subsequent parameter after the format string gets the next parsed value taken from the next parameter in the list following the format string. A parameter specification takes this form:

%[*][width]type

The *, width, and type values may be one of the following:

Parameter

Description

*

Suppresses assigning this value to any parameter

width

Sets the maximum number of characters to read; fewer are read if a white-space or nonconvertible character is encountered

If width is specified, the input is an array of characters of the specified length.