Q(uick)BASIC Function: SPC

Quick View

SPC

A device I/O function that skips n spaces in a PRINT or LPRINT statement

Worth knowing

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

Syntax
  • SPC(n%)
Description/Parameter(s)
n% The number of spaces to skip; a value in the range 0 through 32,767.
Example
PRINT "Text1"; SPC(10); "Text2"
Syntax
  • SPC(n)
Description/Parameter(s)

n, a numeric expression that returns an integer value between 0 and 32,767, is the number of spaces you want in print line.

SPC may only be used with PRINT and LPRINT statements. A semicolon (;) is assumed to follow the SPC(n) command.

Example
CLS 'Clear the screen PRINT "OVER";SPC(15) "THERE"

Sample Output:

OVER THERE
Syntax
  • SPC(n%)
Description/Parameter(s)

SPC does not return a value to the program; instead, it prints one or more spaces to the output device or file, starting at the current print position.

  • A SPC function can be used in the PRINT, LPRINT, or PRINT # statements.
  • BASIC assumes that a semicolon (;) immediately follows any use of the SPC function, whether you explicitly code it or not. For example, the following two PRINT statements are equivalent:

  • PRINT SPC(10); FixLen1$; SPC(10); FixLen2$; SPC(10); FixLen3$
  • PRINT SPC(10); FixLen1$; SPC(10); FixLen2$; SPC(10); FixLen3$

Usage Notes

  • Note that the SPC function does more than move the text cursor to a new print position. For screen output it also overwrites any existing characters on a display screen with blanks.
  • The n% blank characters are printed starting at the current PRINT position.
  • The leftmost print position on an output line is always 1; to have any effect, the value of n% must be >= 1.
  • The rightmost print position is the current line width of the output device (which can be set with the WIDTH statement).
  • The behavior of a SPC function depends on the relationship between three values: n%, the output-line print position when the SPC function is executed, and the current output-line width:
    • If n% is greater than the output-line width, then SPC calculates
      (n% MOD width)
      and lays down that many blanks, starting at the current PRINT position.
    • If the difference between the current print position and the output-line width is less than n% (or n% MOD width), then the SPC function skips to the beginning of the next line and lays down
      (n% - (width - current print position))
      blanks.
Example

This example uses the SPC statement to insert a number of spaces within a printed line using the PRINT statement.

CLS 'Clear the screen. PRINT "The following line is printed using standard screen print zones." PRINT : PRINT "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" PRINT : PRINT PRINT "The next line is printed using the SPC statement to achieve the" PRINT "same results." PRINT PRINT "Column 1"; SPC(6); "Column 2"; SPC(6); "Column 3"; PRINT SPC(6); "Column 4"; SPC(6); "Column 5"