9C /

Introduction to programming with Plan 9 C

Here is a basic hello world program in Plan 9 C dialect.

Plan 9 C is based on regular plain old C

#include <u.h>
#include <libc.h>

void
main(void)
{
	print("Hello, world!");
}

Let's take it apart, to see what each part does.

#include <u.h>
#include <libc.h>

Header files <u.h> and <libc.h> are most likely to be included with most Plan 9 C programs. u.h includes some non-standard (as in ANSI) often used data types, such as Rune and uintptr, as well as some of bunch of machine dependent stuff.

libc.h on other hand is as it's name suggests, includes many basic functions such as strlen() or print()

void
main(void)

Here we define a function called main(), with return type void, which means there is no value needs to be returned.
if you have already done C or C++ programming, you may have noticed instead int main(), we've used void main(). In Plan 9, we use string errors (aka errstr) rather than numerical return values for main(). which we will get into later.

{
	print("Hello, world!");
}

This part is also know as body of function, includes what a function should do when it is called. and, yes. that is print, not printf(). they are largely same however.

And we are ready to compile our first program, write it into a file and compile it. depends on your machine architect, compiler and loader will differ, you may see your machine architect by:

; echo $objtype

mine is 386, consult 2c manual page to see which compiler your machine has. for example 8c is for 386, 6c is for amd64 and 5c is for arm etc.

; 8c hello_world.c && 8l hello_world.8
; 8.out
Hello, world!

Congrats, you've written your first C program in Plan 9