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


Keyword Abbreviation Token (hex) Version(s) Classification
PRINT# P{Shift+R} 98 1.0+ Command and Statement

PRINT# fileNumber [ , [ { , | ; } ] ... [ { expression TAB(n) SPC(n) } [ { , | ; } [ { expression TAB(n) SPC(n) } ] ] ... ] ]
Parameters Type Legal Value(s) Default Value Note(s)
fileNumber Integer  0 ~ 255     Must be an OPEN file#
expression any
n Integer  0 ~ 255     
Print an expression(s) to the device corresponding to fileNumber.

PRINT# is pretty versatile; it will accept any number or type of expressions; the only requirement is the fileNumber which must correspond with an OPEN'd file.  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)).
The PRINT# keyword has its own token, completely different from PRINT.  This means you can't abbreviate PRINT# with ?# and also means you can't have any space between PRINT and #.  You may have space(s) between PRINT# and the fileNumber.
Besides printing an expression, you may also "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 BASIC has no idea of the "current column" of the output device.
By opening a "file" to device 3, PRINT# sends its output to the active display screen (the C128 has two display screens and one is active; all other CBMs only have one screen which is always active).  This is rarely done because PRINT (without #) sends to the screen by default.  Anyway, when the output is going to the screen, many secret variables can be updated by "printing" control codes (see the PETSCII table).
The optional comma (,) will seperate surrounding expressions by a "virtual tab stop" which occurs once every 10 columns (SCREEN DISPLAY ONLY).  Note most CBM machines do not have Tab stops on the display screen; the C128 does have Tab stops (which may be defined anywhere) but they are ignored by PRINT# (it always uses the 10-column rule).  If a PRINT# statement ends with a comma, the output is set to the next "virtual tab stop" and no "new line" (see below) is sent to the output device.
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 PRINT# 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 PRINT# 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.
If fileNumber was not previously OPEN'd, then a FILE NOT OPEN ERROR is generated; otherwise, PRINT# does not (itself) generate an error, however 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.
PRINT#8, 29+35
                   :REM sends 64 and RETURN to the file
PRINT#8, "HELLO";  :REM sends "HELLO" (no RETURN) to file

PRINT#8, A$","B$   :REM useful for REL files (two fields)

PRINT#8            :REM just sends a carriage return [chr$(13)]


Compare With  
Contrast With  
See Also  

© H2Obsession, 2014