PRINT

From C64-Wiki
Jump to navigationJump to search
Disambiguation The title of this article is ambiguous. PRINT (Disambiguation).
BASIC keyword
Keyword: PRINT
Abbreviation: ?
Type: Command
Token code: 153/$99
Handling routine
in BASIC ROM:
43680–43751
$AAA0–AAE7
List of all BASIC keywords


Remark: This article describes the BASIC command PRINT in BASIC V2 at the Commodore 64.

Type: Command
General Programming-Syntax: PRINT [<expression>] [[;|,]<expression>...]

The BASIC command PRINT is used to print data to the current output device, normally the screen. The argument list might consist of string or numerical expressions concatenated by an optional separator. Such expressions could include variables (strings, integers or floating point) and any allowed mathematical term or known BASIC function. Especially when to print graphic and control characters or any string "as is" they must be enclosed with double quotes (" ") as usual for string constants. If the argument list does not conform to BASIC's expression syntax the error message ?SYNTAX ERROR aborts the execution.

  • A PRINT command normally finishes by outputting a carriage return, moving to the first column of the next line. However...
    • a trailing comma will instead move the cursor to the next tab column, which are 10 characters wide, while...
    • a trailing semicolon will not move the cursor at all and subsequent text will appear immediately after the last output.
    • a trailing output function (SPC() or TAB()) will move the cursor to the position according to the function and subsequent text will appear immediately after this position.
  • Items can also be entered with no separators as long they could be identified as separate expressions in which case they will be treated as separated by a semicolon. By the nature of the character parser (CHRGET) spaces are ignored and consecutive letters or digits are combined to form variable name or number. See the examples section.
  • Numbers will be printed with a leading space for positive values or minus sign for negative, and always with a trailing space.
  • The BASIC command CMD can be employed to redirect the screen output of the PRINT command to other devices, like printers or diskdrives.

Examples[edit | edit source]

The items following PRINT are expressions, which may contain operators, functions, variables, string constants or numeric constants. The expressions are evaluated before printing, so they can include arbitrary formulas. For instance:

PRINT 12+2
PRINT 12-1
PRINT 12*2
PRINT 12/2
PRINT COS(2)LOG(1)
PRINT COS(2)+SQR(2)
PRINT A$+"HELLO"

PRINT statements may have multiple expressions on the line, normally separated by semicolon, comma, output function, or if the expression is clearly different than the one before it, there is no need for a separator at all:

PRINT A$," IS A STRING"
PRINT "HELLO";" WORLD"
PRINT "HELLO"" WORLD"
PRINT "HELLO"SPC(1)"WORLD"
PRINT "THE VALUE OF A IS";A;" AND B IS"B

In the last example, variable A is a numeric value and will thus be printed with a leading (or a minus sign in case of an negative value) and trailing space, so no extra spaces are needed in the surrounding string constants to properly space out the output. Note that variable B has no separator and is thus treated as if there is a semicolon. BASIC ignores spaces, so when the separator is not used, the items must be clearly different. For instance:

PRINT A$B$" AND MORE"

Will work, as the three items, the string variables A$, B$ and the string constant, are read as separate items. However:

PRINT A B C

Will be interpreted as:

PRINT ABC

As there are no extra characters to distinguish the variable names after the spaces are. The same is true for multiple numeric constants:

PRINT 10 20+10

Would result in "1030" because it would interpret the first two numbers as a single number, 1020, and then add 10 to it.

Typical usage:

10 A$="Hello ": B$="What is your name": D$="C64"
20 PRINT A$"? "B$; :INPUT C$
30 PRINT: PRINT, :PRINT A$C$
40 PRINT: PRINT "My name is ";D$


Combination of input and output:

10 INPUT "NAME, PLEASE";N$
20 PRINT "YOUR NAME IS " CHR$(34) N$ CHR$(34) "!"

To print the character (") one has to use the function CHR$()!


PRINT AT emulation:

From other BASIC variants well-known command PRINT AT (output on a specific position on screen) does not exist in BASIC 2.0, but can be emulated by means of

PRINT LEFT$("{HOME}{25 × CRSR DOWN}",Y) SPC(X);

The value for X (row) is expected to be in the range from 0 to 39 and for Y (column) in the range from 1 to 25.
The last position on the screen is X=39 and Y=25, but has the side-effect that the whole screen content is scrolled up one row if just one more character is printed.

The functionality can be also reached by using the KERNAL's function PLOT.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT