Let's start with some hands-on explanations of how to use the compiler. Type in the following archetypal program using your favourite PC text editor.

#include "stdlib.h"

/* The famous Hello World program! */

void main() { printf("Hello world!\n"); }

Please take care with capital and small letters: C is a case-sensitive language. Save it as hello.c. Exit the editor, and enter the following:

cc65 hello

After a while, the DOS or Linux prompt will be displayed. Your program is now compiled. Yes, that was all. If you get any error messages, you probably made a typo. Check the file and try again.

Now, let us run the program. A successful compilation will yield a .out file (hello.out in this case). This is a ‘tape’ file. Run Euphoric and load it:

Ready CLOAD "HELLO.OUT" Hello world!

Ready

So, there you have it: your first C program on the Oric. Now, let us see why it does what it does.

To understand that, we need an important piece of information: the C compiler is not a single program, but a pipeline of different programs:

  • The Preprocessor is responsible for preparing the source code for compilation. It handles all lines beginning with hash (#). It outputs the source code it inputs, with a few changes we'll discuss soon.
  • The Compiler reads the source code and translates it into Assembly Language (I make it sound so easy).
  • The Assembler translates the Assembly source code into machine language.
  • Finally, the Linker reads one or more machine language files (object files), as well as some libraries, and creates an executable program.

In the case of the Oric compiler, the linker (by Vaggelis Blathras) runs between the compiler and assembler, and there is also an extra program which translates the final machine code object file into an Oric ‘tape’ file.

All this might sound a bit too technical (and definitely useless). However, each of these stages has its own command set. In the Hello World program, for example, the first line is handled by the preprocessor.

The #include "stdlib.h" directive instructs the preprocessor to literally include the file stdlib.h at the point in hello.c where the #include command was seen. The preprocessor actually processes and outputs the contents of the specified file, and then goes on with the rest of the original file (of course, #include directives can be nested).

The next line is a comment. Anything between /* and */ is considered a comment. Comments may be located anywhere in a line. They may span lines.

Next, we define a function. A C program is split into functions, each of which calls other functions. Even the main program is a function, called ‘main’. This function is automatically called when the program starts. void means the function returns nothing (this makes C functions differ from the mathematical concept of a function). main is the name of the function. ‘()’ means that the function accepts no parameters. Note that you have to include the parentheses even if the function accepts no parameters. The parentheses are C's way of knowing that we are calling or declaring a function (something like the $ in BASIC strings).

The curly brackets (‘{’ and ‘}’) denote the beginning and end of the body of the function. printf() is a function (as you might guess by the ‘()’). It prints the string passed to it as a parameter (note that C strings are enclosed in double quotes: "Hello world!\n"). printf() is obviously not defined in our little program. It is not defined by C itself, either (remember, C has no built-in functions). It's declared inside stdlib.h1. This is why we need to #include "stdlib.h". By the way, the \n at the end of the string means ‘start a new line’. C actually translates it to the ASCII code for CR (Carriage Return, CTRL-M or RETURN).

Another interesting point is the final semicolon (;). In C, all declarations and statements end with this symbol. Try not to forget it; the compiler will stop with all sorts of weird error messages. Since semicolons are used to delimit declarations and statements, white space (spaces, TABs and RETURNs) is not important to the compiler. As long as you use semicolons, you might as well write your whole program in a single line (I wouldn't recommend it: it makes debugging a nightmare).

  • 1. in ANSI C, printf() is declared in stdio.h, the standard input/output functions library. Things are different on the Oric, but they might change as the Oric C compiler progresses.

Tags: 


Add new comment

Leave a comment