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:
| |
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:
| |
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:
| |
Installing Gforth
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
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:
| |
This demonstrates:
: HELLO- Start defining a new word namedHELLO;- End the definitionHELLO- 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:
| |
The comments ( -- ) indicate stack effects: nothing in, nothing out. Each word does one thing, and they compose together.
Key Concepts
- Words are the building blocks - Everything in Forth is a word, including numbers and operators
- Whitespace separates words - The space after
."is essential - Immediate execution - Words execute as soon as you type them (unless you’re defining)
- The stack is central - Though not visible here, data flows through the stack
- Interactive development - Test as you go, define small words, compose them
Common Beginner Mistakes
Forgetting the space after ."
| |
Missing the final quote
| |
Forgetting CR for newline
| |
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