Beginner

Hello World in Forth

Your first Forth program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Forth’s version is elegantly simple.

The Code

Create a file named hello.fth:

1
2
." Hello, World!" CR
bye

Understanding the Code

  • ." Hello, World!" - The ." word (pronounced “dot-quote”) prints the string that follows it up to the closing ". Note the space after ." - it’s required because ." is a word, and words in Forth are separated by spaces.
  • CR - Outputs a carriage return (newline). This moves the cursor to the next line.
  • bye - Exits the Forth interpreter. Without this, Gforth would stay in interactive mode after running the file.

Why the Space After ." ?

In Forth, everything is separated by whitespace. The ." is a complete word that tells the interpreter “start printing”. The space after it is the delimiter. This is different from most languages where " would immediately start a string.

Running with Docker

The easiest way to run Forth without installing it locally:

1
2
3
4
5
# Pull the Gforth image
docker pull forthy42/gforth:latest

# Run the program
docker run --rm -v $(pwd):/app -w /app forthy42/gforth:latest gforth hello.fth -e bye

Note: The -e bye ensures Gforth exits after running the file. If your file already contains bye, you can omit this.

Running Locally

If you have Gforth installed:

1
2
3
4
5
# Run the file
gforth hello.fth -e bye

# Or interactively
gforth

Installing Gforth

macOS (Homebrew):

1
brew install gforth

Ubuntu/Debian:

1
sudo apt install gforth

Windows: Download from gforth.org or use WSL.

Expected Output

Hello, World!

Interactive Hello World

You can also run Forth interactively. Start Gforth and type:

gforth
Gforth 0.7.3, Copyright (C) ...
ok ." Hello, World!" CR
Hello, World!
ok bye

The ok prompt indicates Gforth is ready for input. This interactive style is fundamental to Forth development.

A Word-Based Version

In Forth, you typically define reusable words. Here’s Hello World as a defined word:

1
2
3
: HELLO ." Hello, World!" CR ;
HELLO
bye

This demonstrates:

  • : HELLO - Start defining a new word named HELLO
  • ; - End the definition
  • HELLO - Execute the word we just defined

The Stack in Action

While Hello World doesn’t use the stack much, let’s see a slightly expanded example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
: GREET ( -- )
    ." Hello, " ;

: WORLD ( -- )
    ." World!" CR ;

: HELLO-WORLD ( -- )
    GREET WORLD ;

HELLO-WORLD
bye

The comments ( -- ) indicate stack effects: nothing in, nothing out. Each word does one thing, and they compose together.

Key Concepts

  1. Words are the building blocks - Everything in Forth is a word, including numbers and operators
  2. Whitespace separates words - The space after ." is essential
  3. Immediate execution - Words execute as soon as you type them (unless you’re defining)
  4. The stack is central - Though not visible here, data flows through the stack
  5. Interactive development - Test as you go, define small words, compose them

Common Beginner Mistakes

Forgetting the space after ."

1
2
."Hello"   \ WRONG - no space after ."
." Hello"  \ CORRECT

Missing the final quote

1
2
." Hello, World!    \ WRONG - needs closing "
." Hello, World!"   \ CORRECT

Forgetting CR for newline

1
2
." Line 1" ." Line 2"       \ Prints on same line
." Line 1" CR ." Line 2"    \ Prints on separate lines

Historical Note

When Charles Moore created Forth in the late 1960s, this simple Hello World program represented a radical departure from existing languages. The stack-based, interactive nature meant you could immediately see results - a luxury when computer time was expensive and batch processing was the norm.

Forth’s influence extends far beyond its current niche. The PostScript language that powers printers, the Open Firmware boot loader used by Apple and Sun, and modern concatenative languages like Factor all trace their roots to Forth’s elegant simplicity.

Next Steps

Continue to explore Forth by learning about the stack and basic arithmetic operations.

Running Today

All examples can be run using Docker:

docker pull forthy42/gforth:latest
Last updated: