Hello World
👋 Hello world
Section titled “👋 Hello world”Let’s start with the traditional first program, Hello World in C3:
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. 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.
fn void main() {}🔭 Function scope
Section titled “🔭 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
Section titled “📏 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.
std::io::printn("Hello, World!");io::printn("Hello, World!");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
Section titled “🔧 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:
c3c compile hello_world.c3And run it:
./hello_worldIt 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
Section titled “🏃 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:
$ c3c compile-run hello_world.c3> Program linked to executable 'hello_world'.> Launching hello_world...> Hello, WorldWant more options when compiling? Check the c3c compiler build options.
🎉 Successfully working?
Section titled “🎉 Successfully working?”Congratulations! You’re now up and running with C3.
❓ Need help?
Section titled “❓ Need help?”We’re happy to help on the C3 Discord.