[Top] [Prev] [Next] [Bottom]
print, fprint, sprint - print formatted output
print: fn(format: string, *): int;
fprint: fn(fd: ref FD, format: string, *): int;
sprint: fn(format: string, *): string;
Description
These functions format and print their arguments as UTF text.
print
print: fn(format: string, *): int;
## returns number of bytes printed, negative value on error.
Write text to the standard output. Return the number of bytes transmitted or a negative value if an error was encountered when writing the output.
fprint
fprint: fn(fd: ref FD, format: string, *): int;
## returns number of bytes written to fd, negative value on
error.
Write to the fd file descriptor. Return the number of bytes transmitted or a negative value, if an error was encountered when writing the output.
sprint
sprint: fn(format: string, *): string;
## returns formatted string.
Format text into a string and return the string.
Format specifications
Each of these functions converts, formats, and prints its trailing arguments under control of a format string. The format contains two types of objects:
plain characters
|
Plain characters are simply copied to the output stream.
|
format specifications
|
Each format specification provides instructions for formatting arguments provided to the print function.
|
The Limbo compiler recognizes calls to these functions and checks that the arguments match the format specifications in number and type.
Each conversion specification has the following format:
%[flags][f1.f2]verb
The verb is a single character that describes the way the argument should be printed.
The flag is a single character that can control justification, display of signs, or alternate formats.
Up to two numeric strings may be used for field-width specification. The first string is called f1, the second f2. A period can be used to separate them, and if the period is present, then f1 and f2 are taken to be zero if missing. Either or both of the numbers may be replaced with the character *, meaning that the actual number will be obtained from the argument list as an integer.
Integer Formats
Integer Verbs
d
|
Format int arguments in decimal.
|
x
|
Format int arguments in hexadecimal.
|
X
|
Same as x, but display hex digits in upper-case (and print 0X rather than 0x if # flag is present).
|
Integer Flags
u
|
Display the value as an unsigned number.
|
-
|
Display the value left-justified.
|
#
|
Alternate format: for x or X formats, precede the number by 0x or 0X.
|
Integer Field Control
f1
|
Field padding (with blanks): The number is padded on the left (or right, if the - flag is used to specify left justification) with enough blanks to make the field at least f1 characters long.
|
f2
|
Field padding (with zeros): The number is padded on the left with zeros until at least f2 digits appear.
|
Integer Examples
format
|
argument(s)
|
print ("format", argument(s) )
|
%d
|
16rab9cd
|
702925
|
|%9.7d|
|
16rab9cd
|
| 0702925|
|
%#x
|
16rab9cd
|
0xab9cd
|
%#X
|
16rab9cd
|
0XAB9CD
|
%d
|
-1
|
-1
|
%ud
|
-1
|
4294967295
|
Real Formats
e
|
Format real arguments using exponential notation.
|
f
|
Format real arguments in floating point format.
|
g
|
Format real arguments using either f or e format, whichever takes less space.
|
E
|
Same as e, but use E to specify the exponent, rather than e.
|
G
|
Format real arguments using either f or E format, whichever takes less space.
|
Real Verbs
Real Flags
+
|
Display the value with a plus or minus sign.
|
-
|
Display the value left-justified.
|
#
|
Alternate format: the result will always contain a decimal point, and for g conversions, trailing zeros are not removed.
|
Real Field Control
f1
|
Field padding (with blanks): The number is padded on the left (or right, if the - flag is used to specify left justification) with enough blanks to make the field at least f1 characters long.
|
f2
|
Significant figures: The number of digits that are converted after the decimal place for f and e conversions. For g conversions, f2 is the maximum number of significant digits.
|
Real Examples
format
|
argument(s)
|
print ("format", argument (s))
|
%e
|
0.0021555
|
2.155500e-03
|
%+e
|
0.0021555
|
+2.155500e-03
|
%f
|
0.0021555
|
0.002155
|
%g
|
0.0021555
|
0.002155
|
|%7.0f|
|
215.55
|
| 216.|
|
|%.2f|
|
215.55
|
|215.55|
|
|%9.2f|
|
215.55
|
| 215.55|
|
|%*.*f|
|
9, 1, 215.55
|
| 215.6|
|
Character and String Formats
Character and String Verbs
c
|
Displays a single UTF character (int).
|
s
|
Copy a string to the output.
|
%
|
Display a literal % character. Uses no flags.
|
Character and String Flag
-
|
Display the character left-justified.
|
Character and String Field Control
f1
|
Justification: Justify the character or string within a field of f1 characters
|
f2
|
Number of characters: For strings, f2 controls the number of characters printed. The number of characters copied (n) is the size of the string or f2, whichever is smaller. These n characters are justified within a field of f1 characters.
|
Character and String Examples
format
|
argument(s)
|
print ("format", argument (s))
|
%c
|
98
|
b
|
|%s|
|
February
|
|February|
|
|%12s|
|
February
|
| February|
|
|%-12s|
|
February
|
|February |
|
|%.3s|
|
February
|
|Feb|
|
%%
|
(none)
|
%
|
Most Recent Error String
r
|
The r verb takes no arguments. It prints the error string associated with the most recent system error.
|
Most Recent Error String Example
The following example prints an error associated with a file-opening operation. If a program attempts to open a file that does not exist, the format %r directs the print function to print the error string file does not exist
format
|
argument
|
print ("format")
|
%r
|
(none)
|
file does not exist
|
.
Errors
Output longer than 256 bytes is truncated.
The x verb does not apply the 0x prefix when f2 is present.
See Also
-
System Module Overview
-
open, create - open/create a file for reading or writing
[Top] [Prev] [Next] [Bottom]
infernosupport@lucent.com
Copyright © 1997, Lucent Technologies, Inc.. All rights
reserved.