Q(uick)BASIC Statement: PRINT USING

Quick View

PRINT USING

A device I/O statement that prints strings or numbers using a specified format

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • PRINT [#filenumber%,] USING formatstring$; expressionlist [{; | ,}]
  • LPRINT USING formatstring$; expressionlist [{; | ,}]
Description/Parameter(s)
filenumber% The number of an open sequential file.
formatstring$; A string expression, specifies the format. Characters in the formatstring$ have the following meaning:
Characters that format a numeric expression
#Digit position.
.Decimal point position.
,Placed left of the decimal point, prints a comma every third digit.
+Position of number sign.
^^^^Prints in exponential format.
-Placed after digit, prints trailing sign for negative numbers.
$$Prints leading $.
**Fills leading spaces with *.
**$Combines ** and $$.
Characters used to format a string expression
&Prints entire string.
!Prints only the first character of the string.
\ \Prints first n characters, where n is the number of blanks between slashes + 2.
Characters used to print literal characters
_Prints the following formatting character as a literal.
 Any character not in this table is printed as a literal.
expressionlist A list of one or more numeric or string expressions to print, separated by commas, semicolons, spaces, or tabs.
{; | ,} Determines where the next output begins:
  • ; means print immediately after the last value.
  • , means print at the start of the next print zone.
  •   Print zones are 14 characters wide.
Example
a = 123.4567 PRINT USING "###.##"; a LPRINT USING "+###.####"; a a$ = "ABCDEFG" PRINT USING "!"; a$ LPRINT USING "\ \"; a$
Syntax
  • PRINT USING formatstring; expressionlist [{,|;}]
Description/Parameter(s)
  • formatstring, a string-expression, specifies the format. Characters in formatstring have the following meaning:
    Characters that format a numeric expression
    #Digit position.
    .Decimal point position.
    ,Placed left of the decimal point, prints a comma every third digit.
    +Position of number sign.
    ^^^^Prints in exponential format.
    -Placed after digit, prints trailing sign for negative numbers.
    $$Prints leading $.
    **Fills leading spaces with *.
    **$Combines ** and $$.
    Characters used to format a string expression
    &Prints entire string.
    !Prints only the first character of the string.
    \ \Prints first n characters, where n is the number of blanks between slashes + 2.
    Characters used to print literal characters
    _Prints the following formatting character as a literal.
     Any character not in this table is printed as a literal.
  • expressionlist contains the items to be printed
  • optional "," or ";" at the end of the expressionlist are cursor control characters. See details below

The formatstring is a string literal (or variable) containing literal characters to print (such as labels) and special formatting characters. These formatting characters determine the field and the format of the printed strings or numbers. Spaces, commas, and semicolons in the expressionlist have the same meaning they do in a PRINT statement.

The expressionlist contains the string expressions or numeric expressions to be printed, separated by semicolons.

When PRINT USING is used to print strings, you may use one of three formatting characters to format the string field, as described in the following list:

Character Description
! Only the first character in the given string is to be printed.
\ \ Prints 2 + n characters from the string, where n is the number of spaces between the two backslashes. If the backslashes are typed with no spaces, two characters are printed. With one space, three characters are printed, and so on. If the field is longer than the string, the string is left-justified in the field and padded with spaces on the right.
& Indicates a variable-length string field. When the field is specified with the ampersand (&), the string is output without modification.

When PRINT USING is used to print numbers, the following special characters can be used to format the numeric field:

Character Description
# Represents each digit position. Digit positions are always filled. If the number to be printed has fewer digits than positions specified, the number is right- justified (preceded by spaces) in the field.
. Prints a decimal point. A decimal point may be inserted at any position in the field. If the format string specifies that a digit is to precede the decimal point, the digit is always printed (as 0, if necessary). Numbers are rounded as necessary.
+ Causes the sign of the number (plus or minus) to be printed before the number (if it appears at the beginning of the format string) or after (if it appears at the end of the format string).
- Causes a negative number to be printed with a trailing minus sign if it appears at the end of the format string.
** Causes leading spaces in the numeric field to be filled with asterisks. The double asterisk also specifies positions for two more digits.
$$ Causes a dollar sign to be printed to the immediate left of the formatted number. The $$ specifies two more digit positions, one of which is the dollar sign.
**$ Combines the effects of the double-asterisk and double- dollar-sign symbols. Leading spaces are asterisk-filled and a dollar sign is printed before the number. The **$ symbols specify three more digit positions, one of which is the dollar sign. When negative numbers are printed, the minus sign appears to the immediate left of the dollar sign.
, If the comma appears to the left of the decimal point in a format string, it causes a comma to be printed to the left of every third digit left of the decimal point. If it appears at the end of the format string, it is printed as part of the string. A comma specifies another digit position. The comma has no effect if used with exponential (^^^^ or ^^^^^) format.
^^^^ Specifies exponential format. You can also use five carets (^^^^^) to allow E+xxx to be printed for larger numbers. Any decimal point position may be specified. The significant digits are left-justified and the exponent is adjusted. Unless a leading +, trailing +, or - is specified, one digit position is used to the left of the decimal point to print a space or a minus sign.
_ An underscore in the format string prints the next character as a literal character. A literal underscore is printed as the result of two underscores ( __ ) in the format string.
Note: If the number to be printed is larger than the specified numeric field, a percent sign (%) is printed in front of the number. If rounding causes the number to exceed the field, a percent sign is printed in front of the rounded number. If the number of digits specified exceeds 24, an error message results that reads "Illegal function call."
Examples

These examples show the use of string- and numeric-formatting characters with PRINT USING.

Here is an example of using string-formatting characters:

CLS ' Clear screen A$ = "LOOK" : B$ = "OUT" PRINT USING "!"; A$; B$ 'First characters of A$ and B$. PRINT USING " "; A$; B$ 'Two spaces between backslashes, 'prints four characters from A$. PRINT USING " "; A$; B$; "!!" 'Three spaces, prints A$ and 'a blank. PRINT USING "!"; A$; 'First character from A$ and PRINT USING "&"; B$ 'all of B$ on one line.

Sample Output:

LO LOOKOUT LOOK OUT !! LOUT

Here is an example showing the effects of different combinations of numeric formatting characters:

'Format and print numeric data. CLS ' Clear screen PRINT USING "##.##"; .78 PRINT USING "###.##"; 987.654 PRINT USING "##.## "; 10.2, 5.3, 66.789, .234 PRINT USING "+##.## "; -68.95, 2.4, 55.6, -.9 PRINT USING "##.##- "; -68.95, 22.449, -7.01 PRINT USING "**#.# "; 12.39, -0.9, 765.1 PRINT USING "$$###.##"; 456.78 PRINT USING "**$##.##"; 2.34 PRINT USING "####,.##"; 1234.5 PRINT USING "##.##^^^^"; 234.56 PRINT USING ".####^^^^-"; -888888 PRINT USING "+.##^^^^"; 123 PRINT USING "+.##^^^^^"; 123 PRINT USING "_!##.##_!"; 12.34 PRINT USING "##.##"; 111.22 PRINT USING ".##"; .999

Sample Output:

0.78 987.65 10.20 5.30 66.79 0.23 -68.95 +2.40 +55.60 -0.90 68.95- 22.45 7.01- *12.4 *-0.9 765.1 $456.78 ***$2.34 1,234.50 2.35E+02 .8889E+06- +.12E+03 +.12E+003 !12.34! %111.22 %1.00
Syntax
  • PRINT USING formatstring$; expressionlist [;]
Description/Parameter(s)
formatstring$; Specifies the format, using special characters.
expressionlist The values that are printed on the screen.
Characters that format a numeric expression
#Digit position.
.Decimal point position.
,Placed left of the decimal point, prints a comma every third digit.
+Position of number sign.
^^^^Prints in exponential format.
-Placed after digit, prints trailing sign for negative numbers.
$$Prints leading $.
**Fills leading spaces with *.
**$Combines ** and $$.
Characters used to format a string expression
&Prints entire string.
!Prints only the first character of the string.
\ \Prints first n characters, where n is the number of blanks between slashes + 2.
Characters used to print literal characters
_Prints the following formatting character as a literal.
 Any character not in this table is printed as a literal.
  • Semicolons, spaces, or tabs can be used in expressionlist to separate items. In contrast with the PRINT statement, delimiters in expressionlist used with PRINT USING have no effect on item placement.
  • The argument formatstring$ is a string literal (or variable) that contains literal characters to print (such as labels) and special formatting characters. These formatting characters determine the field and the format of the printed strings or numbers.

Important

  • If the number to be printed is larger than the specified numeric field, a percent sign (%) is printed in front of the number. If rounding causes the number to exceed the field, a percent sign is printed in front of the rounded number. If the number of digits specified exceeds 24, BASIC generates the error message, "Illegal function call."

String Variable Formatting Characters

Character Rules
! Only the first character in the given string is to be printed.
\ \ Prints 2 + n characters from the string, where n is the number of spaces between the two backslashes. For example, if the backslashes are typed with no spaces, two characters are printed; with one space, three characters are printed, and so on. If the field is longer than the string, the string is left-justified in the field and padded with spaces on the right.
& The string is output without modification.

Numeric Variable Formatting Characters

Character Rules
# Represents each digit position. Digit positions are always filled. If the number to be printed has fewer digits than positions specified, the number is right-justified (preceded by spaces) in the field.
. Prints a decimal point. A decimal point can be inserted at any position in the field. If the format string specifies that a digit is to precede the decimal point, the digit is always printed (as 0, if necessary). Numbers are rounded as necessary.
+ Prints the sign of the number (+ or -) before the number (if it appears at the beginning of the format string) or after (if it appears at the end).
- Prints a negative number with a trailing minus sign if it appears at the end of the format string.
** Fills leading spaces in the numeric field with asterisks. Also specifies positions for two more digits.
$$ Prints a dollar sign to the immediate left of the formatted number. Specifies two more digit positions, one of which is the dollar sign.
**$ Combines the effects of the double-asterisk and double-dollar-sign symbols. Leading spaces are filled with asterisks and a dollar sign is printed before the number. Specifies three more digit positions, one of which is the dollar sign. When negative numbers are printed, the minus sign appears to the immediate left of the dollar sign.
, If the comma appears to the left of the decimal point in a format string, it makes a comma print to the left of every third digit left of the decimal point. If the comma appears at the end of the format string, it is printed as part of the string. Specifies another digit position. Has no effect if used with exponential (^^^^ or ^^^^^) format.
^^^^ Specifies exponential format. Five carets (^^^^^) allows D+xxx to be printed for larger numbers. Any decimal point position may be specified. The significant digits are left-justified and the exponent is adjusted. Unless a leading +, trailing +, or - is specified, one digit position is used to the left of the decimal point to print a space or a minus sign.
_ An underscore in the format string prints the next character as a literal character. A literal underscore is printed as the result of two underscores ( __ ) in the format string.
Examples

These two examples show the use of string- and numeric-formatting characters with the PRINT USING statement.

CLS 'Clear screen. A$ = "LOOK": B$ = "OUT" PRINT USING "!"; A$; B$ 'First characters of A$ and B$. PRINT USING "\ \"; A$; B$ 'Two spaces between backslashes, 'prints four characters from A$. PRINT USING "\ \"; A$; B$; "!!" 'Three spaces, prints A$ and 'a blank. PRINT USING "!"; A$; 'First character from A$ and PRINT USING "&"; B$ 'all of B$ on one line.

Sample Output:

LO LOOKOUT LOOK OUT !! LOUT

This example shows the effect of different combinations of numeric-formatting characters.

CLS 'Clear screen. PRINT USING "##.##"; .78 PRINT USING "###.##"; 987.654 PRINT USING "##.## "; 10.2; 5.3; 66.789; .234 PRINT USING "+##.## "; -68.95; 2.4; 55.6; -.9 PRINT USING "##.##- "; -68.95; 22.449; -7.01 PRINT USING "**#.# "; 12.39; -.9; 765.1 PRINT USING "$$###.##"; 456.78 PRINT USING "**$##.##"; 2.34 PRINT USING "####,.##"; 1234.5 PRINT USING "##.##^^^^"; 234.56 PRINT USING ".####^^^^-"; -888888 PRINT USING "+.##^^^^"; 123 PRINT USING "+.##^^^^^"; 123 PRINT USING "_!##.##_!"; 12.34 PRINT USING "##.##"; 111.22 PRINT USING ".##"; .999

Sample Output:

0.78 987.65 10.20 5.30 66.79 0.23 -68.95 +2.40 +55.60 -0.90 68.95- 22.45 7.01- *12.4 *-0.9 765.1 $456.78 ***$2.34 1,234.50 2.35E+02 .8889E+06- +.12E+03 +.12E+003 !12.34! %111.22 %1.00