Skip to content

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 — pO for POKE, goS for GOSUB — which the ROM expands to the first keyword in its reserved-word order that the letters begin. PRINT cannot be reached that way (the scan finds PRINT# 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; LIST always spells the keyword out in full.
  • BASIC V2 is token-identical across the C64 and VIC-20 — same ROM tokens, same LIST spellings — 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 what LIST spells back; the caret key is accepted for it when typing. It folds left to right, so 2↑3↑2 is 64.
  • AND, OR and NOT combine their operands bit by bit — 5 AND 3 is 1 — and a true comparison is -1, which is what makes X=X+(A>B) a counting idiom here. There is no integer-division, remainder or exclusive-OR operator: use INT(a/b) and a-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.
ABSaBABS(<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.0APPEND#<file>, <string> [, D<drive>]
Opens an existing sequential file positioned at its end so PRINT# adds to it.
ASCaSASC(<string>)
Returns the PETSCII code of the first character of the string; an empty string gives ?ILLEGAL QUANTITY ERROR.
ATNaTATN(<number>)
Returns the arctangent of the argument, in radians, between -π/2 and π/2.
BACKUPbABASIC 4.0BACKUP D<drive> TO D<drive>
Duplicates an entire disk from one drive to another on a dual-drive unit.
CATALOGcABASIC 4.0CATALOG [D<drive>]
Displays the disk directory without disturbing the program in memory (synonym of DIRECTORY).
CHR$cHCHR$(<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.
CLOSEclOCLOSE <file>
Closes the logical file with the given number, flushing any buffered output to the device.
CLRcLCLR
Clears all variables, arrays and strings and resets the FOR/GOSUB stacks, but leaves the program itself intact.
CMDcMCMD <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.0COLLECT [D<drive>]
Validates the disk, reclaiming space allocated to improperly closed files.
CONCATconCBASIC 4.0CONCAT <string> TO <string> [, D<drive>]
Appends one sequential disk file onto the end of another, leaving the source unchanged.
CONTcOCONT
Resumes a program halted by STOP, END or the RUN/STOP key, provided the program was not edited in the meantime.
COPYcoPBASIC 4.0COPY <string> TO <string>
Copies a single disk file to a new name (or another drive).
COSCOS(<number>)
Returns the cosine of the argument, which is given in radians.
DATAdADATA <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.0DCLOSE [#<file>]
Closes a disk file opened with DOPEN, or all open files when no logical file number is given.
DEFdEDEF FN <name>(<numvar>) = <number>
Defines a single-argument numeric user function, later called as FN name(x); the parameter is local to the formula.
DIMdIDIM <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.0DIRECTORY [D<drive>]
Displays the disk directory without disturbing the program in memory.
DLOADdLBASIC 4.0DLOAD <string> [, D<drive>]
Loads a BASIC program from disk by name — the disk equivalent of LOAD.
DOPENdOBASIC 4.0DOPEN#<file>, <string> [, D<drive>] [, W]
Opens a disk file to a logical file number for reading, or for writing with W.
DSAVEdSBASIC 4.0DSAVE <string> [, D<drive>]
Saves the BASIC program to disk by name — the disk equivalent of SAVE.
ENDeNEND
Stops the program cleanly and returns to the READY prompt without printing a BREAK message; execution can be resumed with CONT.
EXPeXEXP(<number>)
Returns e raised to the power of the argument.
FNFN <name>(<number>)
Calls a user-defined function previously created with DEF FN, substituting the argument into its formula.
FORfOFOR <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.
FREfRFRE(<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.
GETgEGET <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.
GOGO TO <line>
The spaced-out form of GOTO; GO TO and GOTO behave identically.
GOSUBgoSGOSUB <line>
Calls the subroutine at the given line, saving the return address so a later RETURN comes back to the following statement.
GOTOgoTGOTO <line>
Jumps unconditionally to the given line number.
HEADERhEBASIC 4.0HEADER <string>, D<drive>, I<id>
Formats (news) a disk, writing a name and two-character id.
IFIF <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>.
INPUTINPUT [<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#iNINPUT#<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.
INTINT(<number>)
Returns the largest integer not greater than the argument (rounds toward negative infinity), so INT(-1.5) is -2.
LEFT$leFLEFT$(<string>, <length>)
Returns the leftmost n characters of the string, or the whole string if n is at least its length.
LENLEN(<string>)
Returns the number of characters in the string (0–255).
LETlELET <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.
LISTlILIST [<line>][-[<line>]]
Displays program lines, optionally restricted to a single line or a range; with no argument it lists the whole program.
LOADlOLOAD [<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.
LOGLOG(<number>)
Returns the natural (base-e) logarithm; the argument must be greater than 0.
MID$mIMID$(<string>, <start>[, <length>])
Returns a substring starting at the 1-based position for the optional length (default: to the end of the string).
NEWNEW
Erases the current program and clears all variables, leaving BASIC empty.
NEXTnENEXT [<numvar>[, <numvar>]…]
Closes the innermost FOR loop, or a named one. Several loop variables can be listed to close nested loops at once.
NOTnONOT <number>
Bitwise NOT on a 16-bit signed integer, so NOT X equals -(X+1); used on truth values it logically negates them.
ONON <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.
OPENoPOPEN <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.
PEEKpEPEEK(<addr>)
Returns the byte (0–255) stored at the given memory address (0–65535); the counterpart to POKE for reading hardware and memory.
POKEpOPOKE <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.
POSPOS(<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#pRPRINT#<file>[, <expr>[;|, <expr>]…]
Writes data to an open file or device instead of the screen, using the same formatting rules as PRINT.
READrEREAD <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.0RECORD#<file>, <number> [, <number>]
Positions to a record (and optional byte) within an open relative file.
REMREM [<comment>]
Marks a comment; the rest of the line is stored verbatim and ignored when the program runs.
RENAMEreNBASIC 4.0RENAME <string> TO <string>
Renames a file on the disk.
RESTOREreSRESTORE
Resets the DATA read pointer back to the first DATA statement so READ can re-read the constants from the start.
RETURNreTRETURN
Returns from a subroutine to the statement after the matching GOSUB; without a pending GOSUB it gives ?RETURN WITHOUT GOSUB ERROR.
RIGHT$rIRIGHT$(<string>, <length>)
Returns the rightmost n characters of the string, or the whole string if n is at least its length.
RNDrNRND(<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.
RUNrURUN [<line>]
Clears all variables and starts the program from the lowest line, or from the given line number if one is supplied.
SAVEsASAVE [<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.0SCRATCH <string> [, D<drive>]
Deletes (scratches) a file from the disk.
SGNsGSGN(<number>)
Returns the sign of the argument: -1 if negative, 0 if zero, 1 if positive.
SINsISIN(<number>)
Returns the sine of the argument, which is given in radians.
SPC(sPSPC(<number>)
Within PRINT, outputs the given number of spaces relative to the current cursor position.
SQRsQSQR(<number>)
Returns the square root of the argument; a negative argument gives ?ILLEGAL QUANTITY ERROR.
STEPstESTEP <number>
Sets the increment added to the loop variable each NEXT in a FOR loop; a negative step counts down.
STOPsTSTOP
Halts the program and prints BREAK with the line number; execution can be resumed with CONT.
STR$stRSTR$(<number>)
Returns the number formatted as a string, exactly as PRINT would show it, with a leading space for non-negative values.
SYSsYSYS <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(tATAB(<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.
TANTAN(<number>)
Returns the tangent of the argument, which is given in radians.
THENtHTHEN <line> | <statement>
Introduces the action of an IF; THEN followed by a line number is treated as a GOTO to that line.
TOTO
Separates the start and limit values in a FOR loop (FOR I=1 TO 10) and follows GO in the spaced GO TO form.
USRuSUSR(<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.
VALvAVAL(<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.
VERIFYvEVERIFY [<filename> [, <device>]]
Compares a saved program against the one in memory and reports ?VERIFY ERROR if they differ.
WAITwAWAIT <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

Released under GNU GPL v3.0. Some ROM images are third-party copyrighted works, separate to this project, strictly for personal/educational purposes.