Q(uick)BASIC Statement: WIDTH

Quick View

WIDTH

A file I/O statement that assigns an output-line width to a file or device or changes the number of columns and lines displayed on the screen

Worth knowing

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

Syntax
  • WIDTH [columns%] [,rows%]
  • WIDTH {#filenumber% | device$}, columns%
  • WIDTH LPRINT columns%
Description/Parameter(s)
columns% The desired width in columns. Screen display width must be 40 or 80 columns.
rows% The desired screen-display height in rows. The value can be 25, 30, 43, 50, or 60, depending on your display adapter and screen mode.
#filenumber% The number of an open file or device.
device$ The name of a device:
  • SCRN:, COM1:, COM2:, LPT1:, LPT2:, LPT3:
Example
OPEN "LPT1:" FOR OUTPUT AS #1 WIDTH #1, 132
Syntax
  • WIDTH [columns][,lines]
  • WIDTH {#filenumber | device},width
  • WIDTH LPRINT width
Description/Parameter(s)

Both files and devices can be assigned an output-line width.

Syntax Description
WIDTH [columns][,lines] Sets the number of columns and lines to display on the screen.
The value of columns must be either 40 or 80. The default value is 80.
The value of lines may be 25, 30, 43, 50, or 60, depending on the display adapter used and the screen mode (see the SCREEN statement help topic for more information). The number of lines displayed when the program started will determine the default value.
WIDTH #filenumber,width Sets the line width of an output device opened as a file (for example, LPT1: or CONS:).
The filenumber argument is the number associated with the file in the OPEN statement.
This form permits altering the width while a file is open, since the statement takes place immediately.
WIDTH device,width Sets the line width of device (a device file name).
The device should be a string expression (for example, "CONS:").
Note that this width assignment is deferred until the next OPEN statement affecting the device; the assignment does not affect output for an already open file.
WIDTH LPRINT width Sets to width the line width of the line printer, for use by subsequent LPRINT statements.
Example

In the following example, the record width for file #1 (the printer) is set to different widths (the sample output also refers to a printer):

'*** Programming example that uses WIDTH *** OPEN "LPT1:" FOR OUTPUT AS #1 Test$ = "1234567890" WIDTH #1, 3 PRINT #1, Test$ WIDTH #1, 4 PRINT #1, Test$ CLOSE

Sample Output:

123 456 789 0 1234 5678 90
Syntax
  • WIDTH {#filenumber% | device$}, width%
  • WIDTH LPRINT width%
  • WIDTH {screenwidth% | ,screenheight% | screenwidth%, screenheight}
Description/Parameter(s)
#filenumber% Identifies an output device for which to change the output-line width.
device$ A string expression with the value
  • SCRN:, COM1:, COM2:, LPT1:, LPT2:, or LPT3:
screenwidth% The maximum number of text characters per line on the display screen; an integer expression with a value of 40 or 80 (default is 80).
screenheight% The maximum number of text lines on the screen; a numeric expression with an integer value of
  • 25, 30, 43, 50, or 60.
width% For WIDTH, the width of the output-line for the file or device. For WIDTH LPRINT an integer value between 1 and 255 (default is 80). A carriage return and line feed will be sent to the printer after this number of characters is printed in a line.
Not all of the argument values are valid in every case; it depends on the installed display adapter and the screen mode established by the most recently executed SCREEN statement.
  • The WIDTH #filenumber%, width% form:
    • Sets the line width of an output device already opened as a file (for example, LPT1: or CONS:).
    • The argument filenumber% is the file number associated with the device through the OPEN statement.
    • The "#" character in front of filenumber% is not optional.
    • The width assignment takes place immediately.
  • The WIDTH device$, width% form:
    • Sets to width% the line width of device$ (a device filename). The device should be a string expression (for example, "LPT1:").
    • The width assignment is deferred until the next OPEN statement affecting the device.
    • The assignment does not affect output for an already open device.
  • The WIDTH LPRINT width% form:
    • Sets the line width of the line printer for use by subsequent LPRINT statements.
    • Equivalent to the statement form: WIDTH "LPT1:", width
  • The WIDTH {screenwidth% | ,screenheight% | screenwidth%, screenheight} form:
    • Sets the number of text character columns and lines to display on the screen.
    • The value of screenwidth% must be either 40 or 80; default is 80.
    • The value of screenheight% may be 25, 30, 43, 50, or 60, depending on the display adapter used and the screen mode.
    • The number of lines that can be displayed when the program is started are listed below:
      Adapter 1Screen mode 2Screenwidth x screenheightColor display
      MDPA080x25Monochrome
      Hercules080x25Monochrome
      380x25Monochrome
      CGA040x25, 80x25Color
      140x25Color
      280x25Color
      EGA040x25, 40x43, 80x25, 80x43Color
      040x25, 40x43, 80x25, 80x43Enhanced color
      040x25, 40x43, 80x25, 80x43Monochrome
      140x25Color or Enhanced color
      280x25Color or Enhanced color
      740x25Color or Enhanced color
      880x25Color or Enhanced color
      EGA980x25, 80x43Enhanced color
      EGA or VGA1080x25, 80x43Monochrome
      VGA040x25, 40x43, 40x50Analog
      080x25, 80x43, 80x50Analog
      140x25Analog
      280x25Analog
      740x25Analog
      880x25Analog
      980x25, 80x43Analog
      1080x25, 80x43Analog
      1180x30, 80x60Analog
      1280x30, 80x60Analog
      1340x25Analog
      MCGA040x25, 80x25Color or Analog
      140x25Color or Analog
      280x25Color or Analog
      1180x30, 80x60Analog
      1340x25Analog
      • 1 See SCREEN Statement for a description of the different adapters.
      • 2 Screen mode 4 has a screen width and screen height of 80x25 in text format. For a list of the computers supported by BASIC screen mode 4, see the SCREEN Statement .
Example

This example uses the WIDTH statement to set different widths for file #1 (a terminal on COM1; the sample output shown below also refers to a printer or terminal attached to COM1).

CLS 'Open the port at the proper baud rate OPEN "COM1:9600,N,8,1,ASC,LF" FOR OUTPUT AS #1 'Set Up a test string Test$ = "1234567890" 'Change width to 3 WIDTH #1, 3 PRINT #1, Test$ 'Change width to 4 WIDTH #1, 4 PRINT #1, Test$ 'Delay 3 seconds so you can observe the results on the terminal. SLEEP 3 'Close the file CLOSE #1

Sample Output:

123 456 789 0 1234 5678 90