Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# ANSI C 'printf' Format

Nyquist/XLISP: The *float-format* and *integer-format* variables define format strings for the underlying 'sprintf' C function. In C, the same format string specification is used for 'fprint' [writes to a file], 'printf' [writes to standard output] and 'sprintf' [writes to another string]. These three functions are meant in the following text with 'the printf functions'.

ANSI C: The printf functions write output under control of a format string that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored. The printf functions return when the end of the format string is encountered.

Back to top

## Format String

The format string is composed of zero or more directives, which are ordinary characters [except "%"], which are copied unchanged to the output stream, and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character "%":

    %[flags][field-with][precision][data-type]conversion-type

After the "%", the following appear in sequence:

- Flags - Zero or more flags that modify the meaning of the conversion specification.

- Field Width - An optional decimal integer specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left to the field width. The field is padded on the right if the − flag has been given, or padded with zeros if the 0 flag had been given. A negative integer, given as 'field width' argument, is interpreted as a − flag followed by a positive field width.

- Precision - An optional decimal integer that gives the minimum number of digits to appear for integer conversions, the number of digits to appear after the decimal-point character for floating-point e and f conversions, or the maximum number of significant digits for the floating-point g conversion.

  The precision takes the form of a period character followed by an optional decimal integer:

      .[integer]

  If the integer is omitted, it is treated as zero, a negative precision argument is taken as if it were missing.

  Note: In C, the precision also specifies the maximum number of characters to be written from a string in 's' conversion, but "%s" should not be used in the XLISP *float-format* or *integer-format* variables, otherwise XLISP will crash.

- Data Type - An optional character specifying a data type to be used for the conversion.

  XLISP: Nyquist/XLISP uses C 'long' signed integers and C 'double' floats only, so with floating-point numbers no special data type needs to be given, while integer conversions should always be prefixed by a 'l' [lowercase L], otherwise the printed representation of integer numbers may be differently than the behaviour of the Nyquist/XLISP functions.

- Conversion Type - A character that specifies the conversion type to be applied.

Not with Nyquist/XLISP: In C, a 'field width' or 'precision', or both, may be indicated by an asterisk * instead of a digit string. In this case, an int argument supplies the field width or precision. The arguments specifying field width or precision, or both, shall appear [in that order] before the argument [if any] to be converted.

Back to top

## Flags

The flag characters and their meanings are:

− [minus sign character]

  The result of the conversion will be left-justified within the field.

+ [plus sign character]

  The result of a signed conversion will always begin with a plus or minus sign.

space [space character]

  If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space will be prepended to the result. If the 'space' and + flags both appear, the 'space' flag will be ignored.

# [hash character]

  The result is to be converted to an 'alternate form':

  Octal Numbers - For o conversion, it increases the precision to force the first digit of the result to be a zero.

  Hexadecimal Numbers - For x or X conversion, a nonzero result will have 0x or 0X prepended to it.

  Floating-point Numbers - For e, E, f, g, and G conversions, the result will always contain a decimal-point character, even if no digits follow it (normally, a decimal-point character appears in the result of these conversions only if a digit follows it). For g and G conversions, trailing zeros will not be removed from the result. For other conversions, the behavior is undefined.

0 [number zero character]

  For integer d, i, o, u, x, X, and floating-point e, E, f, g, G conversions, leading zeros [following any indication of sign or base] are used to pad to the field width. No space padding is performed. If the '0' and − flags both appear, the '0' flag will be ignored. For integer d, i, o, u, x, X conversions, if a precision is specified, the '0' flag will be ignored. For other conversions, the behavior is undefined.

Back to top

## Data Type

h [lowercase H character]

  A following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument [the argument will have been promoted according to the integral promotions, and its value shall be converted to short int or unsigned short int before printing].

  A following n conversion specifier applies to a pointer to a short int argument.

l [lowercase L character]

  A following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument.

  A following n conversion specifier applies to a pointer to a long int argument.

L [uppercase L character]

  A following e, E, f, g, or G conversion specifier applies to a long double argument.

If an h, l, or L appears with any other conversion specifier, the behavior is undefined.

Back to top

## Conversion Type

Integer conversion:

d, i, o, u, x, X

  The integer argument is converted to:

  `` | d | → | signed decimal
  `` | i | → | signed decimal
  `` | o | → | unsigned octal
  `` | u | → | unsigned decimal
  `` | x | → | unsigned hexadecimal, using 'abcdef'
  `` | X | → | unsigned hexadecimal, using 'ABCDEF'

  The precision specifies the minimum number of digits to appear. If the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting a zero value with an explicit precision of zero results in no characters.

  XLISP: Nyquist/XLISP uses C 'long' signed integers, so the integer conversions should always be prefixed by a 'l' [lowercase L] indicating a 'long int' C data type, otherwise the printed representation of integer numbers may be differently than the behaviour of the Nyquist/XLISP functions.

Floating-point conversion:

f

  The floating-point argument of C data type 'double' is converted to decimal notation in the style:

      [-]ddd.ddd

  where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6. If the precision is explicitly zero, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

e, E

  The floating-point argument of C data type 'double' is converted in the style:

      [-]d.ddde+-dd

  where there is one digit before the decimal-point character [which is nonzero if the argument is nonzero] and the number of digits after it is equal to the precision. If the precision is missing, it is taken as 6. If the precision is zero, no decimal-point character appears. The value is rounded to the appropriate number of digits. The exponent always contains at least two digits. If the value is zero, the exponent is zero. The "E" conversion specifier will produce a number with 'E' instead of 'e' introducing the exponent.

g, G

  The floating-point argument of C data type 'double' is converted in style f or e or in style E in the case of a "G" conversion specifier, with the precision specifying the number of significant digits. If an explicit precision is zero, it is taken as 1. The style used depends on the value converted. Style e will be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result. A decimal-point character appears only if it is followed by a digit.

Other conversion types defined in ANSI C, but not to be used with the XLISP *float-format* and *integer-format* variables, because XLISP will produce nonsense or just simply will crash:

c

  The integer argument is converted to an 'unsigned char', and the resulting character is written.

  XLISP: If the *float-format* or *integer-format* variable is set to "%c", then with integers or floating-point numbers, the lowest 8 bit of the internal binary representation will be interpreted as an ASCII character.

s

  The argument shall be a pointer to an array of character type. Characters from the array are written up to [but not including] a terminating null character. If the precision is specified, no more than that many characters are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.

  XLISP: If

p

  The argument shall be a pointer to 'void'. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.

n

  The argument shall be a pointer to an integer into which is written the number of characters written to the output stream so far by this call to the C 'fprintf' function. No argument is converted.

%

  A "%" is written. No argument is converted. The complete conversion specification shall be "%%".

If a conversion specification is invalid, the behavior is undefined. In no case does a nonexistent or small field width cause truncation of a field. If the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

The minimum value for the maximum number of characters produced by any single conversion shall be 509.

Back to top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference