ZetCode

FreeBasic Print Keyword

last modified June 16, 2025

The FreeBasic Print keyword is used to display output to the console. It is one of the most fundamental and frequently used statements. Print can output text, numbers, variables, and expressions with formatting.

Basic Definition

In FreeBasic, Print is a statement that writes data to the standard output (usually the console). It automatically converts values to strings and adds a newline by default.

Print can handle multiple expressions separated by commas or semicolons. Commas add tab spacing between items, while semicolons concatenate them. The statement is versatile for both simple and complex output needs.

Simple Print Statement

This example demonstrates the basic usage of the Print statement.

print_simple.bas
Print "Hello, FreeBasic!"
Print 42
Print 3.14159

Here we print a string literal, an integer, and a floating-point number. Each Print statement outputs its argument and moves to a new line. FreeBasic automatically converts numbers to their string representation.

Print with Multiple Items

Print can output multiple values in a single statement using separators.

print_multiple.bas
Dim fname As String = "Alice"
Dim age As Integer = 25

Print "Name:"; fname; "Age:"; age
Print "Name:", fname, "Age:", age

The first Print uses semicolons to concatenate items without spacing. The second uses commas which insert tab stops between items. This shows how separator choice affects output formatting.

Print Without Newline

The trailing semicolon suppresses the automatic newline.

print_no_newline.bas
Print "Loading";
For i As Integer = 1 To 3
    Print ".";
    Sleep 500
Next
Print " Done!"

This creates a progress indicator by printing dots on the same line. The trailing semicolon after "Loading" prevents the line break. Each dot appears after a half-second delay, building the output gradually.

Print with Expressions

Print can evaluate and display the results of expressions directly.

print_expressions.bas
Dim a As Integer = 10
Dim b As Integer = 5

Print "Sum:"; a + b
Print "Product:"; a * b
Print "Average:"; (a + b) / 2

Here we perform calculations directly within the Print statements. The expressions are evaluated before being converted to strings. This demonstrates Print's ability to handle complex output needs.

Print with Formatting

FreeBasic provides formatting options for numeric output.

print_formatting.bas
Dim price As Double = 19.99
Dim quantity As Integer = 3

Print Using "Price: $$###.##"; price
Print Using "Quantity: #####"; quantity
Print Using "Total: $$####.##"; price * quantity

The Using clause provides precise control over output format. Dollar signs and hash symbols define the number format. This is useful for financial or scientific applications needing specific decimal precision.

Print with Variables and Literals

Print can mix variables and literals in various combinations.

print_mixed.bas
Dim city As String = "Paris"
Dim population As Integer = 2161000

Print "The city of "; city; " has a population of"; population; "people."
Print "That's approximately"; population \ 1000; "thousand inhabitants."

This example combines string variables, numeric variables, and string literals in meaningful output. The backslash operator performs integer division for the approximate population figure.

Print Special Characters

Special characters like quotes and tabs can be included in output.

print_special_chars.bas
Print "She said, ""Hello, there!"""
Print "Column1" + Chr(9) + "Column2"
Print "Line1" + Chr(10) + "Line2"

Double quotes are escaped by doubling them. Chr(9) inserts a tab, and Chr(10) adds a newline. These techniques allow precise control over output formatting when needed.

Best Practices

This tutorial covered the FreeBasic Print keyword with practical examples showing its versatility for console output. Mastering Print is essential for debugging and user communication in FreeBasic programs.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.