Commodore BASIC reference
Every command, function and operator in Commodore BASIC — BASIC V2 as built into the ROMs of the Commodore 64 and VIC-20, and BASIC 4.0 as built into the Commodore PET.
In this reference: Hardware · Escape codes · File formats · Argument notation
Notes and caveats
- A keyword can be typed as a prefix whose last letter is shifted —
pOforPOKE,goSforGOSUB— which the ROM expands to the first keyword in its reserved-word order that the letters begin.PRINTcannot be reached that way (the scan findsPRINT#first), which is why?stands for it. Both forms are shown beside each keyword in the table below, and the search box finds a keyword by either;LISTalways spells the keyword out in full. - BASIC V2 is token-identical across the C64 and VIC-20 — same ROM tokens, same
LISTspellings — so only their hardware (screen size, colours, sound, memory map) differs, as described on the hardware page. - BASIC 4.0 (the PET) is the same core language plus fifteen disk-handling commands. Keywords tagged BASIC 4.0 are those extra disk commands; the C64 and VIC-20 run the V2 core without them.
- The PETSCII escape codes sub-page covers all three machines, though the colour-control codes have no visible effect on the PET's monochrome display.
- The power operator is
↑, which is whatLISTspells back; the caret key is accepted for it when typing. It folds left to right, so2↑3↑2is64. AND,ORandNOTcombine their operands bit by bit —5 AND 3is1— and a true comparison is-1, which is what makesX=X+(A>B)a counting idiom here. There is no integer-division, remainder or exclusive-OR operator: useINT(a/b)anda-b*INT(a/b).
Command Function Operator
| Syntax & description | |
|---|---|
- | <number> - <number> | -<number>Subtracts the right operand from the left, or negates a value when used as a unary prefix. |
* | <number> * <number>Multiplies two numbers. |
/ | <number> / <number>Divides the left operand by the right; dividing by zero gives ?DIVISION BY ZERO ERROR. |
↑ | <number> ↑ <number>Raises the left operand to the power of the right (the C64 up-arrow key); has higher precedence than multiply and divide. |
+ | <number> + <number> | <string> + <string>Adds two numbers, or joins (concatenates) two strings. |
< | <number> < <number> | <string> < <string>Less-than comparison; returns -1 for true and 0 for false. Strings compare by PETSCII code. |
<= | <number> <= <number> | <string> <= <string>Less than or equal. |
<> | <number> <> <number> | <string> <> <string>Not equal. |
= | <var> = <expr> | <expr> = <expr>Assigns a value in a statement, or tests equality in an expression (returning -1 for true and 0 for false). |
> | <number> > <number> | <string> > <string>Greater-than comparison; returns -1 for true and 0 for false. Strings compare by PETSCII code. |
>= | <number> >= <number> | <string> >= <string>Greater than or equal. |
ABSaB | ABS(<number>)Returns the absolute (unsigned) value of the argument. |
ANDaN | <number> AND <number>Bitwise AND of two 16-bit integers, also used to combine truth values where false is 0 and true is -1. |
APPENDaPBASIC 4.0 | APPEND#<file>, <string> [, D<drive>]Opens an existing sequential file positioned at its end so PRINT# adds to it. |
ASCaS | ASC(<string>)Returns the PETSCII code of the first character of the string; an empty string gives ?ILLEGAL QUANTITY ERROR. |
ATNaT | ATN(<number>)Returns the arctangent of the argument, in radians, between -π/2 and π/2. |
BACKUPbABASIC 4.0 | BACKUP D<drive> TO D<drive>Duplicates an entire disk from one drive to another on a dual-drive unit. |
CATALOGcABASIC 4.0 | CATALOG [D<drive>]Displays the disk directory without disturbing the program in memory (synonym of DIRECTORY). |
CHR$cH | CHR$(<number>)Returns the one-character string for a PETSCII code (0–255). Many codes are control codes when printed, such as CHR$(147) to clear the screen. |
CLOSEclO | CLOSE <file>Closes the logical file with the given number, flushing any buffered output to the device. |
CLRcL | CLRClears all variables, arrays and strings and resets the FOR/GOSUB stacks, but leaves the program itself intact. |
CMDcM | CMD <file>[, <expr>]Redirects normal PRINT output to an open file or device (such as a printer) until a PRINT# or CLOSE restores the screen. |
COLLECTcoLBASIC 4.0 | COLLECT [D<drive>]Validates the disk, reclaiming space allocated to improperly closed files. |
CONCATconCBASIC 4.0 | CONCAT <string> TO <string> [, D<drive>]Appends one sequential disk file onto the end of another, leaving the source unchanged. |
CONTcO | CONTResumes a program halted by STOP, END or the RUN/STOP key, provided the program was not edited in the meantime. |
COPYcoPBASIC 4.0 | COPY <string> TO <string>Copies a single disk file to a new name (or another drive). |
COS | COS(<number>)Returns the cosine of the argument, which is given in radians. |
DATAdA | DATA <constant>[, <constant>]…Holds a list of inline numeric or string constants consumed in order by READ; the rest of the statement is stored verbatim, so unquoted text is allowed. |
DCLOSEdCBASIC 4.0 | DCLOSE [#<file>]Closes a disk file opened with DOPEN, or all open files when no logical file number is given. |
DEFdE | DEF FN <name>(<numvar>) = <number>Defines a single-argument numeric user function, later called as FN name(x); the parameter is local to the formula. |
DIMdI | DIM <var>(<number>[, <number>]…)Declares one or more arrays with the given maximum subscripts; indices run from 0, so DIM A(10) gives 11 elements. Undimensioned arrays default to a size of 10. |
DIRECTORYdiRBASIC 4.0 | DIRECTORY [D<drive>]Displays the disk directory without disturbing the program in memory. |
DLOADdLBASIC 4.0 | DLOAD <string> [, D<drive>]Loads a BASIC program from disk by name — the disk equivalent of LOAD. |
DOPENdOBASIC 4.0 | DOPEN#<file>, <string> [, D<drive>] [, W]Opens a disk file to a logical file number for reading, or for writing with W. |
DSAVEdSBASIC 4.0 | DSAVE <string> [, D<drive>]Saves the BASIC program to disk by name — the disk equivalent of SAVE. |
ENDeN | ENDStops the program cleanly and returns to the READY prompt without printing a BREAK message; execution can be resumed with CONT. |
EXPeX | EXP(<number>)Returns e raised to the power of the argument. |
FN | FN <name>(<number>)Calls a user-defined function previously created with DEF FN, substituting the argument into its formula. |
FORfO | FOR <numvar> = <number> TO <number> [STEP <number>]Opens a counting loop that runs until NEXT, stepping the variable by 1 (or by STEP). The body always executes at least once because the limit is tested at NEXT. |
FREfR | FRE(<number>)Returns the number of free BASIC bytes after forcing string garbage collection; the argument is ignored. Results above 32767 appear negative, so add 65536. |
GETgE | GET <var>Reads a single keypress from the keyboard buffer without waiting, returning an empty string (or 0) if no key is pending. This is the standard way to read controls in games. |
GO | GO TO <line>The spaced-out form of GOTO; GO TO and GOTO behave identically. |
GOSUBgoS | GOSUB <line>Calls the subroutine at the given line, saving the return address so a later RETURN comes back to the following statement. |
GOTOgoT | GOTO <line>Jumps unconditionally to the given line number. |
HEADERhEBASIC 4.0 | HEADER <string>, D<drive>, I<id>Formats (news) a disk, writing a name and two-character id. |
IF | IF <number> THEN <line> | <statement>Evaluates the condition (zero is false, non-zero is true) and runs the THEN part only when true. There is no ELSE; THEN <line> is shorthand for THEN GOTO <line>. |
INPUT | INPUT [<prompt>;] <var>[, <var>]…Prints the optional prompt followed by a "? " and reads one or more comma-separated values from the keyboard. It halts the program, so games use GET instead. |
INPUT#iN | INPUT#<file>, <var>[, <var>]…Reads comma- or newline-separated values from an open file or device into the listed variables. The file must first be opened with OPEN. |
INT | INT(<number>)Returns the largest integer not greater than the argument (rounds toward negative infinity), so INT(-1.5) is -2. |
LEFT$leF | LEFT$(<string>, <length>)Returns the leftmost n characters of the string, or the whole string if n is at least its length. |
LEN | LEN(<string>)Returns the number of characters in the string (0–255). |
LETlE | LET <var> = <number> | <string>Assigns a value to a variable. The keyword is optional on the C64, so X=5 and LET X=5 are identical. |
LISTlI | LIST [<line>][-[<line>]]Displays program lines, optionally restricted to a single line or a range; with no argument it lists the whole program. |
LOADlO | LOAD [<filename> [, <device> [, <number>]]]Loads a program from tape (device 1, the default) or disk (device 8), optionally with a secondary address. A secondary address of 1 loads to the original address. |
LOG | LOG(<number>)Returns the natural (base-e) logarithm; the argument must be greater than 0. |
MID$mI | MID$(<string>, <start>[, <length>])Returns a substring starting at the 1-based position for the optional length (default: to the end of the string). |
NEW | NEWErases the current program and clears all variables, leaving BASIC empty. |
NEXTnE | NEXT [<numvar>[, <numvar>]…]Closes the innermost FOR loop, or a named one. Several loop variables can be listed to close nested loops at once. |
NOTnO | NOT <number>Bitwise NOT on a 16-bit signed integer, so NOT X equals -(X+1); used on truth values it logically negates them. |
ON | ON <number> GOTO <line>[, <line>]… | ON <number> GOSUB <line>[, <line>]…Uses the rounded value as a 1-based index to pick which line to GOTO or GOSUB. If the index is 0 or larger than the list, execution falls through to the next statement. |
OPENoP | OPEN <file>, <device> [, <secondary> [, <string>]]Opens a logical file, given its file number, device number, optional secondary address and optional name, for later use by PRINT#, INPUT#, GET# or CMD. |
OR | <number> OR <number>Bitwise OR of two 16-bit integers, also used to combine truth values where false is 0 and true is -1. |
PEEKpE | PEEK(<addr>)Returns the byte (0–255) stored at the given memory address (0–65535); the counterpart to POKE for reading hardware and memory. |
POKEpO | POKE <addr>, <byte>Writes a byte (0–255) to a memory address (0–65535). The C64 has no graphics or sound keywords, so screen, colour, sprite and SID effects are all done by POKEing VIC-II and SID registers. |
POS | POS(<number>)Returns the current cursor column (0-based) on the logical screen line; the argument is ignored. |
PRINT? | PRINT [<expr>][;|, <expr>]…Prints values to the screen; a trailing semicolon suppresses the newline and a comma tabs to the next 10-column field. Printed CHR$ codes also control colour and cursor movement. |
PRINT#pR | PRINT#<file>[, <expr>[;|, <expr>]…]Writes data to an open file or device instead of the screen, using the same formatting rules as PRINT. |
READrE | READ <var>[, <var>]…Assigns the next unread DATA constants to the listed variables, advancing the read pointer. Running past the last DATA gives ?OUT OF DATA ERROR. |
RECORDreCBASIC 4.0 | RECORD#<file>, <number> [, <number>]Positions to a record (and optional byte) within an open relative file. |
REM | REM [<comment>]Marks a comment; the rest of the line is stored verbatim and ignored when the program runs. |
RENAMEreNBASIC 4.0 | RENAME <string> TO <string>Renames a file on the disk. |
RESTOREreS | RESTOREResets the DATA read pointer back to the first DATA statement so READ can re-read the constants from the start. |
RETURNreT | RETURNReturns from a subroutine to the statement after the matching GOSUB; without a pending GOSUB it gives ?RETURN WITHOUT GOSUB ERROR. |
RIGHT$rI | RIGHT$(<string>, <length>)Returns the rightmost n characters of the string, or the whole string if n is at least its length. |
RNDrN | RND(<number>)Returns a random number from 0 to just under 1. A positive argument continues the sequence, 0 reseeds from the system timers, and a negative argument seeds a repeatable sequence. |
RUNrU | RUN [<line>]Clears all variables and starts the program from the lowest line, or from the given line number if one is supplied. |
SAVEsA | SAVE [<filename> [, <device> [, <number>]]]Saves the current program to tape or the named device, optionally with a secondary address that selects, for example, an end-of-tape marker. |
SCRATCHsCBASIC 4.0 | SCRATCH <string> [, D<drive>]Deletes (scratches) a file from the disk. |
SGNsG | SGN(<number>)Returns the sign of the argument: -1 if negative, 0 if zero, 1 if positive. |
SINsI | SIN(<number>)Returns the sine of the argument, which is given in radians. |
SPC(sP | SPC(<number>)Within PRINT, outputs the given number of spaces relative to the current cursor position. |
SQRsQ | SQR(<number>)Returns the square root of the argument; a negative argument gives ?ILLEGAL QUANTITY ERROR. |
STEPstE | STEP <number>Sets the increment added to the loop variable each NEXT in a FOR loop; a negative step counts down. |
STOPsT | STOPHalts the program and prints BREAK with the line number; execution can be resumed with CONT. |
STR$stR | STR$(<number>)Returns the number formatted as a string, exactly as PRINT would show it, with a leading space for non-negative values. |
SYSsY | SYS <addr>Jumps to a machine-code routine at the given address, returning to BASIC on RTS; the A, X, Y and status registers are taken from page-zero locations. |
TAB(tA | TAB(<number>)Within PRINT, moves the cursor to the given absolute column counting from 0. It only moves forward, so it has no effect once the cursor is already past that column. |
TAN | TAN(<number>)Returns the tangent of the argument, which is given in radians. |
THENtH | THEN <line> | <statement>Introduces the action of an IF; THEN followed by a line number is treated as a GOTO to that line. |
TO | TOSeparates the start and limit values in a FOR loop (FOR I=1 TO 10) and follows GO in the spaced GO TO form. |
USRuS | USR(<addr>)Passes the argument in the floating-point accumulator to the machine-code routine whose address is stored in the USR vector ($0311) and returns its result. |
VALvA | VAL(<string>)Parses the leading numeric part of the string and returns it as a number, returning 0 if it does not start with a number. |
VERIFYvE | VERIFY [<filename> [, <device>]]Compares a saved program against the one in memory and reports ?VERIFY ERROR if they differ. |
WAITwA | WAIT <addr>, <mask>[, <mask>]Pauses until a memory location, ANDed with the mask and optionally XORed, is non-zero. Misuse can hang the machine since BASIC stops polling anything else. |
π | πThe built-in constant pi (3.14159265), entered as the single π token. |
Argument notation
Anything in <angle brackets> is a value you supply; everything else is typed exactly as shown. [square brackets] mark an optional part, | separates alternatives, and … means the part before it can repeat. The arguments on this page are:
<number>- a numeric expression
<string>- a string expression
<expr>- a value of either type, where the keyword takes both
<byte>- a value from 0 to 255
<constant>- a literal number or string, not an expression
<var>- a variable of either type
<numvar>- a numeric variable
<line>- a line number
<statement>- one BASIC statement
<comment>- free text, to the end of the line
<name>- a name the program defines for a function or procedure
<start>- the position something starts at, counting from 1
<length>- how many characters
<prompt>- text shown before the input; a literal, not an expression
<file>- an open file, by the number it was opened on
<filename>- the name of a file on tape or disc
<addr>- a memory address
<mask>- a bit mask, one bit per item
<drive>- a drive number, written after a literal D
<device>- a device number: 1 for tape, 8 for disc
<secondary>- a secondary address
<id>- a two-character disk id, written after a literal I
Showing 95 of 95 keywords