printfstatement

NAME
SYNTAX
DESCRIPTION
USAGE
ENGINEERING NOTATION
NULL
EXAMPLES
COPYRIGHT
SEE ALSO

NAME

printf, fprintf, sprintf − RWP*Load Simulator formatted output

SYNTAX

printfstatement ::=
  sprintf identifier , concatenation { , concatenation }
| sprintf || identifier , concatenation { , concatenation }
| fprintf identifier , concatenation { , concatenation }
| printf concatenation { , concatenation }

DESCRIPTION

The statements in the printf family are very similar to their equivalents in C, and will in fact use those to do actual formatting.

The first two statements using sprintf write to a string, the first to the beginning of the string, and the second with || after the keyword to the end of the string. Both will return an error if the string does not have sufficient length.

The latter two statements, fprintf and printf write to a named file respectively stdout.

USAGE

All four require at least one concatenation as argument which normally consists of characters to be output combined with conversion specifiers starting with a % character. To output a terminating newline, the newline must be part of this concatenation. The characters after the % character are interpreted identical to the equivalents in C with some minor changes:

The flags generally work like in C and e.g. 0, - are useful to 0-pad respectively to left-justify.

You can provide a field width as an integer or using * in which case the value will be taken as the next argument to the statement. You can similarly use . followed by an integer or * to provide a precision. Neither of these can use an integer followed by $ to ask for a specific argument.

Only a limited set of standard conversion specifiers are useful: d, i or x for integers, e, f, g for doubles and s for strings.

ENGINEERING NOTATION

You can output double values using engineering notation, i.e. with an SI prefix such as k for kilo, n for nano, etc. This is done using the m or M conversion specifier, and is useful when the absolute value is in the range [1.0e-30;9.99e+32] or is exactly zero. The precision has the same effect as for the standard e conversion implying the output will be rounded and have one more significant digit than given by the precision. The precision must be 2 or higher, and the field width should not be set.

The symbol for micro, µ, i.e. 1e-6 is by default using the UTF-8 encoding, which is the two hexadecimal values 0xc2 and 0xb5; you must use the $musymbol directive to change this if your terminal is not using the UTF-8 (or compatible) character set.

The difference between the two specifiers is that M outputs one space character between the number and the SI-prefix, while the m outputs the SI-prefix directly after the number.

The decimal point is not output if no significant digits are output after it. This can only happen if the precision is 2 and the absolute value to be output is at least 100.

NULL

In rwloadsim, integer and double can be null and such values will by default be output as the empty string. You can provide one of the flags z, b, n, N or t to change this behavior. The flag z will cause null to be output as zero, b will cause a blank, i.e. one or more space characters, n or N will cause the output ’null’ or ’NULL’. Finally, the flag t will cause an argument to be taken as a string that will be output if a value is null.

EXAMPLES

If you execute the following:

integer a := 42, b;
double x := 12.345678;
string s := "fourtytwo";

# Show 0-padding and output of null
printf "|%07d|%d|\n", a, b;
printf "|%07d|%nd|\n", a, b;

b:=4;
# Show left justification and
# how field width and precision
# are provided as parameters
printf "|%-*.*f|\n", a/3,b,x;


# Show append to existing string
# with negative field width causing
# left justification
sprintf || s,"=%*d|", -b, a;
printline s;

the following output will be produced

|0000042||
|0000042|null|
|12.3457       |
fourtytwo=42  |

The following example shows how NULL can be output as respectively the text null or the user provided text "nothing":

integer a;
printf "|%Nd|%td|\n", a, "nothing", a;

which will produce the following output:

|NULL|nothing|

This example shows how the m specifier can be used:

double tt := 12.34e-6;
printf "the time is %.3ms\n", tt;

while will produce this output with the µ symbol in its UTF-8 representation.

the time is 12.34µs

COPYRIGHT

Copyright © 2023 Oracle Corporation
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl

SEE ALSO

statement(1rwl), filestatement(1rwl), expression(1rwl), directive(1rwl)