DIM
BASIC keyword | |
Keyword: | DIM |
Abbreviation: | D Shift+I |
Type: | Command |
Token code: | 134/$86 |
Handling routine in BASIC ROM: |
45185–45194 $B081–B08A |
List of all BASIC keywords |
Remark: This article describes the BASIC command DIM in BASIC V2 at the Commodore 64.
Type: Command General Programming-Syntax: DIM <Variable>(<d1>[,<d2>[,<d3>...]])[, <Variable>(<d1>[,<d2>[,<d3>...]])...]
The BASIC command DIM allocates space in array memory for a new array, with one dimension for each of the dimension sizes d1, d2, d3, etc. given in the DIM statement.
Note that the dimension sizes represent the last, or "highest", index number available. Since the lower limit for the indices is always fixed at zero, specifying a dimension of n reserves space for n+1 variables "along" that dimension.
Omitting the list of dimension sizes causes a ?SYNTAX ERROR IN line. Previously DIMensioned arrays cannot be "re-dimensioned" through a second DIM command; this will raise a ?REDIM'D ARRAY ERROR IN line.
Note that using an array without DIMensioning is actually legal in Commodore BASIC: If a BASIC program e.g. starts assigning values to an array not "declared" in a prior DIM statement, the interpreter "auto-DIMensions" the array to a size of 10+1=11 elements along each specified dimension. Like other arrays, such "auto-DIMensioned" arrays cannot be re-dimensioned later through an explicit DIM command.
Indices to arrays are handled internally by signed positive 16-bit integers, which in theory gives them a range from 0 to 32767, resulting in up to 32768 variables along that dimension. Indices outside this range will raise an ?ILLEGAL QUANTITY ERROR. The number of dimensions are counted in a single-byte register, and thus has a limit at 255 dimensions. But memory limitations will cause an ?OUT OF MEMORY ERROR IN line long before an array attains such sizes.
Examples[edit]
DIM A$(4) One-dimensional array with room for 4+1=5 strings. DIM C%(2,3) Two-dimensional array with 2+1=3 integers along the first dimension, and 3+1=4 dimensions along the second, giving a total of 12 integers. DIM T(8,1,5) Three-dimensional array with (8+1)×(1+1)×(5+1)=108 real variables. DIM B$(CT%) DIMensioning a string array according to a size held in a variable
Q(10)=10 Bad coding, but legal - automatic DIMensioning, whereas Q(10) has got the value 10 and Q(0) until Q(9) the value 0 !
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