Skip to content

My first Hello World

Let’s start with a simple “Hello World” program. The simplest Hello World you can write in C3 looks like this:

import std::io;
fn void main()
{
io::printn("Hello, World!");
}

The import statement imports other modules, and we want printn which is in std::io

Next we define a function, which starts with the fn keyword followed by the return type. In this case we don’t need to return anything, so we use void. Then follows the name of the function, main, followed by the parameter list, which is empty.

fn void main() {}

{ and } signifies the start and end of the function respectively. Inside we have a single call to the function printn in std::io. We use the last part of the path “io” in front of the function to identify what module it belongs to.

The io::printn function takes a single argument and prints it, followed by a line feed After this the function ends and the program terminates.

fn void main()
{
io::printn("Hello, World!");
}

Compiling the program

Let’s take the above program and put it in a file called hello_world.c3.

We can then compile it:

Terminal window
$ c3c compile hello_world.c3

And run it:

Terminal window
$ ./hello_world

It should print Hello, World! and return back to the command line prompt. If you are on Windows, you will have hello_world.exe instead. Call it in the same way.

Compiling and running

When we start out it can be useful to compile and then have the compiler start the program immediately. We can do that with compile-run:

Terminal window
$ c3c compile-run hello_world.c3
> Program linked to executable 'hello_world'.
> Launching hello_world...
> Hello, World

If you followed along so far: Congratulations! You’re now up and running with C3.