Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

CMD

Keyword Abbreviation Token (hex) Version(s) Classification
CMD C{Shift+M} 9D 1.0+ Command and Statement

 
Syntax  
CMD fileNumber [ , [ { , | ; } ] ... [ { expression TAB(n) SPC(n) } [ { , | ; } [ { expression TAB(n) SPC(n) } ] ] ... ] ]
 
Parameters Type Legal Value(s) Default Value Note(s)
fileNumber Integer       
expression any
any
 
 
n Integer  0 ~ 255     
 
 
Purpose  
Set default BASIC output to a fileNumber; optionally "print" an expression(s).

 
 
Remarks  
CMD is pretty versatile; it will accept any number or type of expressions.  In most programs, no expressions are given; the statement is simply used to change BASIC's default output to the specified fileNumber (see below).  The number of expressions is limited only by the length of a BASIC program line; you may have zero expressions.  Each expression may be of numeric or string type; each may be a literal value (e.g., the number 7 or the string "hello"), a variable, a simple function (such as ABS(X) or STR$(X)), or a "BASIC forumula" (which includes actual mathematical forumulas, such as 2*X+1, and so-called "string formulas" like A$+CHR$(X)).
 
Optionally, CMD allows printing an expression; it also allows you to "print" spaces (code 32) with the SPC(n) preposition or nothing with the TAB(n) preposition.  If the value of n is not legal (see above) then an ILLEGAL QUANTITY ERROR is generated.  The SPC preposition will add n spaces after the last expression (if any).  The TAB preposition does nothing because the system has no idea what the "current column" of the output device may be.
 
The fileNumber must be the number used by a previous OPEN command; otherwise a FILE NOT OPEN ERROR is generated.  In some cases, OPEN will not generate an error, even though it "failed" (for example, device not present); in these cases, CMD will also not generate an error.  If the target device happens to be the display screen (quite rare) then many secret variables may be updated by printing control codes (see the PETSCII table).
 
After CMD has successfully executed, further normal BASIC output will sent to the specified fileNumber.  This typically includes the output of PRINT and LIST, but also include "informational" messages such as "LOADING" and "READY."  If an error occurs (or after a PRINT# fileNumber statement is executed), then BASIC will revert to the defalut (active display screen) output.
 
CMD is often used to redirect screen output to a printer; for example, to generate a "hard copy" of a program listing.
 
The optional comma (,) will seperate surrounding expressions by a "virtual tab stop" which occurs once every 10 columns, IF THE OUTPUT DEVICE IS THE SCREEN; otherwise the results are undefined (device-specific)...
 
The optional semicolon (;) will not separate surrounding expressions; the will "run together".  This is obvious with strings (see examples), but most numerical expressions will appear separated because CMD will include a trailing space behind each number, and positive numbers (which are very common) will have a leading space.  (See STR$ for details about how BASIC formats numeric values.)  I hate to complicate things, but (contrary to the Syntax shown above) BASIC will often allow you to omit a semicolon with the same effect as including one between two expressions; this is possible when there is no ambiguity between two expressions.  To be nagative, this makes for hard-to-read (sloppy) code, but on the positive side this feature allows programs to be more compact (thus less memory use and faster execution).  Anyway, if a PRINT statement ends with a semicolon, the output position is not changed; that is, no "new line" (see below) is sent to the output device.
 
If the statement does not end with one of the two optional punctuation marks (a comma or semicolon), then CMD will end by sending a "new line" to the output device.  By default, this is a terminating carriage return (code 13).  Under some cases (see OPEN) it may also send a terminating line feed (code 10).
 
Some versions of BASIC (those > 2) allow a USING clause which allows for further customization of how each expression is generated.
 
Besides the possible errors mentioned above, any invalid expression will generate an error (for example SYNTAX ERROR, or TYPE MISMATCH ERROR); this also applies to the n parameter of the SPC and TAB prepositions.
 
Using a PRINT# fileNumber [ expr...] statement will send expr... (if any) to the fileNumber and then re-direct BASIC to use the default (active display screen) output device.  This is highly recommended prior to CLOSE.  The reason is that some devices will buffer data until a carriage return (or maybe line feed) is detected; the PRINT# statement (without a trailing comma or semicolon) will send this code(s) and thus "flush" any buffered data prior to CLOSE.
  
Examples:
OPEN 4,4      :REM open file#4 to printer
  
READY.
CMD 4         :REM redirect output to printer note "READY." was sent too!
LIST          note that "READY." was also sent to the device
PRINT#4       :REM restore screen as output device

READY.
CLOSE 4

READY.
 
 
Compare With  
 
Contrast With  
 
See Also  
CLOSE, OPENSPC, TAB,  USING  

© H2Obsession, 2014
Comments