Printf Function

Action

Prints a formatted expression to the results file.

Syntax

Printf (sFormat, aArgs, ...)
Variable Description
sFormat The format to use. STRING.
aArgs Any number of arguments to print. ANYTYPE.

Notes

  • Printf prints a formatted value to the results file.

  • The format string you specify describes the format of each argument, in order. The format string can include any Ansi C printf format specifiers, including:
    • %s (if the argument that follows is a string)

    • %d (if the argument that follows is an integer)

    • %o (if the argument that follows is an integer you want formatted in octal format)

    • %x (if the argument that follows is an integer you want formatted in hexadecimal format)

    • %f (if the argument that follows is a real number)

    • You can precede any of these format characters with an integer to specify the width to use in the output. Use an asterisk (*) to determine the width from an argument in the list, for example:
      Printf("%*s", 5, "abc")
      // Prints __abc (the underscores represent spaces)
  • By default, the output is right-justified. A negative width causes left-justification.

  • For real numbers, you can also specify a second number, which is the precision of the output, in the form width.precision. You can use an asterisk as the precision to read the precision from the argument list.

  • Printf does not automatically supply a new-line character at the end of the line, as Print does. To output a carriage return at the end of the line, use the text "\n" in the format string. To output a backslash (\), enter two backslashes in the format string, as in the following:
    Printf ("%s\\%s", sMyString, sMyString)
    // Prints abc\abc
  • Similarly, to output a percent sign (%), specify two percent signs in the format string.

Example

[-] testcase PrintfExample ()
	[ ] STRING sMyString = "abc"
	[ ] INTEGER iMyInt = 123 
	[ ] NUMBER nMyReal = 45.7 
	[ ] Printf ("%s", sMyString) // prints abc 
	[ ] Printf ("%4d", iMyInt) // prints 123
	[ ] Printf ("%4.2f", nMyReal) // prints 45.70
	[ ] Printf ("%s%d", sMyString, iMyInt) // prints abc123 
	[ ] Printf ("%-4s%d", sMyString, iMyInt) // prints abc 123