AT&T Home | AT&T Labs | Research
AT&T Labs, Inc. - Research

The Yoix® Scripting Language

Home | What's New | Grammar | Documentation | Download | License | YChart | YDAT | YWAIT | Byzgraf | FAQs
fprintf (Stream stream, String format [, Object obj1, ..., Object objN]) yoix.stdio
 
Converts objects to strings under the control of a format specification, writes the results to stream, and returns the number of characters written, or -1 if there was an error. The format string consists of zero or more of the following components:
  • ordinary characters that are copied directly to the output stream.
  • conversion specifications that determine how arguments are converted to strings.
There should be general agreement between the type of a conversion specification and the corresponding argument. Each format conversion has the general form,
%[ flag ][ width ][. precision ]specification
where the square brackets indicate optional elements and:
  • flag is one or more of:
    - indicating left-justification.
    + indicating right-justification.
    (sp) indicating right-justify and pad with spaces.
    0 indicating right-justify and pad with zeros.
    # indicating lead octals with a "0" and hexadecimals with a "0x".
    ' indicating locale specific grouping and decimal point presentation for the default Locale.
  • width indicates the minimum field width.
  • precision indicates the maximum field width for strings or, for numbers, the decimal precision.
  • specification is one of:
    c takes the integer portion of a number and interprets it as a Unicode character.
    d takes only the integer portion of a number.
    e or E expresses a number using base-10 exponential notation (with a "e" or "E" being used to indicate where the exponent begins).
    f expresses the real portion of a number using decimal notation.
    g or G provides generic formatting for numbers: for real number, formatting is like "f" or "e" formatting, as appropriate ("f" or "E" if "G" is specified) and, for integers, formatting is like d.
    o expresses the integer portion of a number in octal format.
    O formats any object in the manner of the toString built-in and uses the precision, if supplied, to control the depth of the dump.
    s or S for handling strings ("S" takes the whole string, nulls and all, while "s" stops at the first null).
    x or X expresses the integer portion of a number in hexadecimal format (with a "x" or "X" being used to indicate whether the alphabetic digits should be lower-case or upper-case respectively).
    % just output a percent sign, no argument is converted and the entire conversion specification must be just "%%".
Incidentally, the strfmt built-in, which is available in yoix.string, provides fprintf formatting capabilities without writing the output directly to a stream.
 
 Example:   The program,
import yoix.math.*;
import yoix.stdio.*;

int     i = 65;
double  x = 400 * atan(1);
String  city[10] = "New York";

fprintf(stdout, "A: 1(%d) 2(%d) 3(%05d) 4(%-5d)\n", i, x, i, i);
fprintf(stdout, "B: 1(%c) 2(%o) 3(%x) 4(%#06x)\n", i, i, i, i);
fprintf(stdout, "C: 1(%f) 2(%e) 3(%5.3E) 4(%g)\n", x, x, x, x);
fprintf(stdout, "D: 1(%s) 2(%10.5s) 3(%S)\n", city, city, city);
prints
A: 1(65) 2(314) 3(00065) 4(65   )
B: 1(A) 2(101) 3(41) 4(0x0041)
C: 1(314.159265) 2(3.141593e+02) 3(3.142E+02) 4(314.15927)
D: 1(New York) 2(     New Y) 3(New York\0\0)
on standard output (where \0 is used to represent otherwise invisible null characters).
 
 Return:   int
 
 See Also:   fscanf, printf, scanf, sprintf, sscanf, strfmt, toString

 

Yoix is a registered trademark of AT&T Inc.