Hello World
👋 Hello world
Let’s start with the traditional first program, Hello World in C3:
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. We don’t need to return anything, so return void
. The function name main
then follows, followed by the function’s parameter list, which is empty.
🔭 Function scope
{
and }
signifies the start and end of the function respectively,
we call this the function’s scope. Inside the function scope we have a single function
call to printn
inside std::io
. We use the last part of the path “io” in front of
the function to identify what module it belongs to.
📏 Imports can use a shorthand
We could have used the original longer path: std::io::printn
if we wanted, but we can shorten it to just the lowest level module like io::printn
. This is the convention in C3 and is is known as “path-shortening”, it avoids writing long import paths that can make code harder to read.
The io::printn
function takes a single argument and prints it, followed by a newline, then the function ends and the program terminates.
🔧 Compiling the program
Let’s take the above program and put it in a file called hello_world.c3
.
We can then compile it with:
And run it:
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
:
Want more options when compiling? Check the c3c compiler build options.
🎉 Successfully working?
Congratulations! You’re now up and running with C3.
❓ Need help?
We’re happy to help on the C3 Discord.