PRINT Statement

Syntax

Syntax: PRINT expr [; expr ...]
Source: pb_cg_emit_print_expr (pb_codegen.c:5674)

Description

Outputs text, numbers, or string expressions to the console using the Windows API WriteFile. Handles integers, floating-point values, and string data. Each expression is formatted and written in sequence. The PRINT statement is the primary means of console I/O and is essential for debugging output, status logging, and interactive programs.

Parameters

ParameterDescription
exprAny expression to output (numeric literal, variable, string, or expression). Multiple expressions are supported.
; (semicolon separator)Placed between expressions to suppress a newline. The next PRINT or expression continues on the same line.
(no trailing semicolon)A PRINT statement ending without ; automatically appends a carriage return + line feed (CRLF).
PRINT (alone)A bare PRINT with no expression outputs a blank line (just CRLF).

Example

FUNCTION PBMAIN() AS LONG
    LOCAL x AS LONG
    LOCAL name AS STRING
    
    x = 42
    name = "PowerBASIC"
    
    ' Print a variable with text
    PRINT "The value of x is "; x
    
    ' Print multiple items on one line
    PRINT "Name: "; name; "  Value: "; x
    
    ' Print blank line
    PRINT
    
    ' Print without newline (trailing ;)
    PRINT "Loading";
    PRINT "..."
    PRINT "Done!"
    
    FUNCTION = 0
END FUNCTION

Generated Assembly

Console Output Path

; 1. Format integer via wsprintfA
lea    rcx, [rbp - intbuf_offset]   ; buffer
lea    rdx, [__pb_fmt_i64]          ; format "%I64d"
mov    r8, rax                      ; value
call   [__imp_wsprintfA]            ; wsprintfA(buf, fmt, val)
mov    [rbp - print_len_offset], rax ; save length

; 2. Write to console via WriteFile
mov    rcx, -11                     ; STD_OUTPUT_HANDLE
call   [__imp_GetStdHandle]
mov    [rbp - written_offset], rax
mov    rcx, [rbp - written_offset]  ; hFile
mov    rdx, [rbp - print_ptr_offset] ; lpBuffer
mov    r8, [rbp - print_len_offset]  ; nBytesToWrite
lea    r9, [rbp - written_offset]    ; lpBytesWritten
mov    [rsp + 32], 0                 ; lpOverlapped = NULL
call   [__imp_WriteFile]

String Output

; String address -> RDX, length -> R8 via pb_cg_emit_string_address_len
; Then same WriteFile path as above

Newline

lea    rdx, [__pb_crlf]            ; "\r\n" in .rdata
mov    r8, 2                        ; 2 bytes
; then WriteFile

Float Output

; Format float to string first
lea    r8, [rbp - intbuf_offset]    ; buffer
mov    rdx, 15                      ; precision
call   [__imp__gcvt]                ; _gcvt(value, 15, buf)
mov    [rbp - print_ptr_offset], rax
mov    rcx, rax
call   [__imp_lstrlenA]
mov    [rbp - print_len_offset], rax
; then WriteFile

Imported WinAPI Functions

FunctionDLLPurpose
GetStdHandlekernel32.dllGet console output handle (STD_OUTPUT_HANDLE = -11)
WriteFilekernel32.dllWrite bytes to console/file
wsprintfAuser32.dllFormat integer to string buffer
lstrlenAkernel32.dllGet ANSI string length
_gcvtmsvcrt.dllConvert float to string