Skip to content

Commit

Permalink
Re-arrange hello world page. Note about module declaration added
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb-o committed Sep 21, 2024
1 parent b35a72e commit e14d1db
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/basics/hello_world.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit e14d1db

Please sign in to comment.