Skip to content

Commit

Permalink
Updated hello world.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Jan 30, 2024
1 parent 3ade667 commit 46e9f54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/sample.astro
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
class="flex items-center gap-x-3.5 py-2 px-3 rounded-md text-sm text-gray-800 hover:bg-gray-100 focus:ring-2 focus:ring-blue-500 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-300"
href="https://github.com/c3lang/c3c/releases/download/latest/c3-macos.zip"
>
Macos
macOS
</a>
<a
class="flex items-center gap-x-3.5 py-2 px-3 rounded-md text-sm text-gray-800 hover:bg-gray-100 focus:ring-2 focus:ring-blue-500 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-300"
Expand Down
80 changes: 73 additions & 7 deletions src/content/docs/guide/my-first-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,79 @@ sidebar:
order: 3
---

```cpp
module test;
import std::io;
The simplest "Hello World" we can write is this:

fn int main(String[] args)
```c
import std::io;
fn void main()
{
io::printn("Hello, World!");
return 0;
io::printn("Hello, World!");
}
```
```

The `import` statement imports other modules, and we want `println` which
is in `std::io`. Import is always recursive, so `import std::io` actually
imports both `std::io` and its sub-modules, such as `std::io::path` (which
handles file paths).

You could even do `import std;`, which would import the entire standard library
into your project!

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

"main" is a bit special as it is also the entry point to the program. For Unix-like OSes there
are a few different variants, for example we might declare it as `fn void main(String[] args)`.
In that case the parameter "args" contains a *sub array* of strings, which correspond to the
command line arguments, with the first one being the name of the application itself.

`{` 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. We could also have used `std::io::printn`
if we wanted. Just a part of the module path, like "io::printn", is known as "path-shortening"
and is the common way of referring to functions (avoid `std::io::printn`, it's not idiomatic).

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.

### Compiling the program

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

We can then compile it:

```
> c3c compile hello_world.c3
```

And run it:

```
> ./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 `compiler-run`:

```
> 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.





0 comments on commit 46e9f54

Please sign in to comment.