diff --git a/src/basics/hello_world.md b/src/basics/hello_world.md index 2dd023c..eab90d8 100644 --- a/src/basics/hello_world.md +++ b/src/basics/hello_world.md @@ -1,29 +1,19 @@ # Hello World! Let's write the classic example, Hello World! But before that, we have two options with how to proceed: -- [Using a project](#using-a-project) - [Running directly](#running-directly) +- [Using a project](#using-a-project) -## Using a project - -Creating a project is quite simple in C3. We can use the `c3c` executable to initialise, build and run our project. - -Let's create a new project: -```sh -$ c3c init myproject -$ cd myproject -``` - -We now have a new project directory and this would have created a few directories within it, a license, a `project.json` and a readme. This includes the `build` directory, which is where your project will be compiled into. In C3 we use the `src` directory and this is where you will find the `main.c3` file. You can see more details about project structure in the [C3 guide](https://c3-lang.org/guide/my-first-project/). +## Running directly -Let's open the main file `src/main.c3` in your editor and write this code: +We can create a new file with the `.c3` extension. Then we can write this code: ```c++ {{#include ../../examples/basics/hello_world.c3}} ``` -We can now run this code with: +We will use the compiler to build, run and then dispose of the executable: ```sh -$ c3c run +$ c3c compile-run --run-once main.c3 ``` This should then print to the console @@ -34,18 +24,37 @@ Hello, World! Program completed with exit code 0. ``` +You can also just compile the file and run the executable directly: + +```sh +$ c3c compile main.c3 -o hello +$ ./hello + +Hello, World! +``` + [Done](#explaining-our-program) -## Running directly +## Using a project -We can create a new file with the `.c3` extension. Then we can write this code: +Creating a project is quite simple in C3. We can use the `c3c` executable to initialise, build and run our project. + +Let's create a new project: +```sh +$ c3c init myproject +$ cd myproject +``` + +We now have a new project directory and this would have created a few directories within it, a license, a `project.json` and a readme. This includes the `build` directory, which is where your project will be compiled into. In C3 we use the `src` directory and this is where you will find the `main.c3` file. You can see more details about project structure in the [C3 guide](https://c3-lang.org/guide/my-first-project/). + +Let's open the main file `src/main.c3` in your editor and write this code: ```c++ {{#include ../../examples/basics/hello_world.c3}} ``` -We will use the compiler to build, run and then dispose of the executable: +We can now run this code with: ```sh -$ c3c compile-run --run-once main.c3 +$ c3c run ``` This should then print to the console @@ -56,15 +65,6 @@ Hello, World! Program completed with exit code 0. ``` -You can also just compile the file and run the executable directly: - -```sh -$ c3c compile main.c3 -o hello -$ ./hello - -Hello, World! -``` - ### Explaining our program You have now written your first C3 program. Let's break down what is happening in our code. We can start with the first line: @@ -73,7 +73,7 @@ You have now written your first C3 program. Let's break down what is happening i module myproject; ``` -Every file must start with a module name. We can think of a module like a namespace, as each file that has the same module name is considered the same module. Module names are not tied to files or the directory they are in, so it is up to you to name them correctly. To avoid clashing, we must use longer and/or more detailed names. This could be something like `projectname::foo::bar::baz`. +Every file should start with a module name, if we omit this, then the compiler will try its best to generate one using the file. We can think of a module like a namespace, as each file that has the same module name is considered the same module. Module names are not tied to files or the directory they are in, so it is up to you to name them correctly. To avoid clashing, we must use longer and/or more detailed names. This could be something like `projectname::foo::bar::baz`. ```c++ import std::io; @@ -87,7 +87,7 @@ fn void main() { } ``` -Functions in C3 use the `fn` keyword to denote a function declaration. It is then followed by the return type (void) and then a function name and its parameters. This should look pretty familiar for anyone who's used a C-style language. The interesting thing here is our `io::printn`. If you tried to remove the `io::` prefix, you will see this error: +Functions in C3 use the `fn` keyword to denote a function declaration. It is then followed by the return type and then a function name and its parameters. This should look pretty familiar for anyone who's used a C-style language. The interesting thing here is our `io::printn`. If you tried to remove the `io::` prefix, you will see this error: ```sh Error: Functions from other modules must be prefixed with the module name.