READ

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: READ
Abbreviation: R, Shift+E
Type: Command
Token code: 135/$87
Handling routine
in BASIC ROM:
44038–44283
$AC06–ACFB
List of all BASIC keywords


Remark: This article describes the BASIC command READ in BASIC V2 on the Commodore 64.

Typ: Command
General Programming-Syntax: READ <variable> [,<variable>]...

The BASIC command READ is used for reading constant values from DATA lines into the indicated variables. This command is able to read more constants at once with a variable list separated by commas.

If using the wrong type of variable (for example read a character string into a numerical variable like float or integer) the BASIC error ?SYNTAX ERROR IN line occurs with a reference of the corresponding DATA line. Starting with BASIC 3.5 the error message changes to ?TYPE MISMATCH ERROR and the line number points directly to the READ statement that triggered the condition. Such behavior can be prevented by generally using string variables.
With a value that falls outside the expected range of a READ variable, e.g. the value is outside the range of an integer, BASIC breaks with error ?ILLEGAL QUANTITY ERROR.
If more constants are read than values exist in DATA lines the BASIC error ?OUT OF DATA appears.

The BASIC command RESTORE resets the current DATA read pointer to beginning of the program. A succeeding READ searches for the first DATA statement where the DATA read pointer is adjusted to. How long a READ takes depends on how many program lines are to be processed to advance the DATA read pointer to the next DATA line. See DATA for an advice where to place DATA lines.


Examples[edit | edit source]

Read in different data types[edit | edit source]

10 DATA 1,2,3, APPLE, "COMMODORE 64", ."DOUBLE-QUOTES", "WITH,COMMA" 
20 READ A
30 READ B%, C, D$
40 READ E$
50 READ F$, G$
60 PRINT A, B%, C, D$, E$, F$, G$

Characters like space, double quotes, comma, colon are handled correctly, giving following output:

 1         2         3        APPLE
COMMODORE 64        ."DOUBLE-QUOTES"
WITH,COMMA

Get values by means of a loop[edit | edit source]

10 FOR X=0 TO 11: READ A(X): PRINT A(X),;:NEXT
20 DATA 10,20,.,40,50,,70,80,0,100,-,110


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