Conversion Specifiers for sprintf


Conversion Specifiers for sprintf

Parts of a specifier

A conversion specifier consists of the following parts, in the given order:

  • A percent sign %.

  • One or more flags (optional).

  • A numeric minimum field width (optional).

  • A numeric precision (optional). The precision, if present, begins with a period.

  • A conversion character, which determines the type of conversion that will take place, as well as the interpretation of the other parts of the conversion specifier.

The - (minus) flag specifies left-justified output. By default, converted values are right-justified.

If a numeric field (width or precision) is replaced with an * (asterisk), the value of the field is taken from the next argument in the argument list.

Integer specifiers

Specifier

Description

Notes

d , i

Signed decimal

+ (plus) flag adds leading + to positive values.

' ' (space) flag adds a leading space if the first character of the converted value is not a + or - .

u

Unsigned decimal

 

o

Unsigned octal

# flag forces leading .

x , X

Unsigned hexadecimal

x specifier outputs a-f ; X outputs A-F .

# flag forces leading 0x .

For all integer specifiers, numbers are padded with leading s to the length specified by the precision. The (zero) flag pads to the field width with s.

The flag l before an integer specifier indicates that the value is to be converted as a C long or unsigned long type. The flag h before an integer specifier indicates that the value is to be converted as a C short or unsigned short type. The flag V indicates that the value is to be converted as a Perl integer type.

String specifiers

Specifier

Description

Notes

s

String

Precision specifies maximum number of characters output; assumed infinite if omitted.

c

Character

Value is converted to unsigned decimal and then output as a character.

Floating-point specifiers

Specifier

Description

Notes

f

Fixed-point

For all floating-point specifiers, the precision specifies the number of digits to the right of the decimal point (6 by default). The # flag forces a decimal point.

e , E

Exponential notation

Value is converted to exponential notation, e.g. 1.234e-03 . E specifier gives uppercase E .

g , G

Fixed-point or exponential

Value is converted as per f or e ( E in case of G ). e is used if the exponent resulting from the conversion is less than -4 or greater than or equal to the precision. Trailing zeros after the decimal point are discarded.

The rule for the g (or G ) specifier may seem complicated, but basically all it means is to use whichever of f or e (or E ) looks better.

Other specifiers

Specifier

Description

Notes

%

Percent sign

Outputs a % .

p

Pointer

Address of Perl value is output in hexadecimal.

n

Count

Outputs a count of the number of characters generated in the output so far into the next variable in the argument list.

The -w command line option can be helpful in debugging sprintf or printf formats that don't work.



Effective Perl Programming. Writing Better Programs with Perl
Effective Perl Programming: Writing Better Programs with Perl
ISBN: 0201419750
EAN: 2147483647
Year: 1996
Pages: 116

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net