C from the top

In this series of articles, I hope to show you how to program using C. The programs will all be very simple to create initially, but by the end, you should be able to create a full blown C application.

You will need some extra bits and pieces though to be able to use these tutorials. They are

  1. A C compiler

  2. A text editor

I have used the Acorn Desktop C application (from CJE Micro's, £125), though there are other versions of C (e.g. Beebug EasyC and GNU C).

The text editor supplied with Acorn C is !SrcEdit, which I find awful!. You can get by with it (or normal !Edit), but for a nicer way to look at things, I would suggest either !StrongEd or !Zap (both available on the Kosovo Orphan Appeal CD). My preference is StrongEd.

Alright, enough of this waffle and on with the show. I've assumed that you will have installed Acorn C and have a directory looking something like this

Filer window

You should now create a directory called (something like) Learning. Inside this, you will need to create the following directories : c, compiled, files, h and o. The text files are saved into the c directory and the compiled apps into the compiled directory.

I would also suggest buying a good C book. My recommendation is Teach Yourself C (3rd ed), Schildt, Osbourne (priced around £25). It is a fabulous book which even after you've finished with, will still be of great use.

Let's make a start - in and out

A very simple C program looks like this:

/* Sample program */

#include <stdio.h> int main(void) { char name[30]; printf("What is your name?"); scanf("%s",name); printf("\n Hello %s",name); return 0; }

But what does it mean?

The #include <stdio.h> is telling the compiler to include the header file stdio.h when compiling the code. This file contains all the information the compiler needs to know to interpret the functions in the source file.

Functions? What functions?

C is almost entirely made up of functions. The form of the function will be discussed at a later date, but suffice to say that this simple source code contains at least 3 functions (2 printf statements and the scanf statement). You don't need to worry about that, just remember that for the application to work, it will need a header file of some description.

int main (void)

Every C program requires a function called main. (void) indicates that there are no arguments being passed to the function with int meaning that it is an integer function.

Unlike BASIC, the fragment of code following this statement has to be enclosed in curly brackets { }. This tells the compiler that all contained there in belongs to the function or loop.

char name[30];

Simply put, this has the same net effect of reserving 30 bytes of memory to be allocated to the variable name. (In BASIC, this is akin to a DIM statement).

The printf statement places text onto the screen, with the scanf waiting for an input.

The second printf statement requires some explaining. The line is

printf("\nHello %s",name);

The \n means newline. It then prints Hello to the screen. %s tells the program that whatever argument is next, it should be a string varible (in this case, name). After this, the return 0; statement tells the computer that the program is over and it should return to normal.

This program works and I would encourage you to type it in, compile and run - something I would encourage as the best way to learn is to do.

When you actually run the program, you will find that for normal entries (such as answering Paul, Fred, Jim or Terry), the program works as it should, however, if you have a space in the entry, then you will find that you will only get up to that space. This is down to C thinking that space is not a normal character.

This can be worked around by using a very similar command gets().

If you replace the scanf( ... ); line with

gets(name);

recompile and run, you should be able to enter spaces into the line. Try it! The reason why the gets() statement works is down to the fact that C stores whatever is being typed until the return key is pressed.

***** BEWARE *****

The variable name has only been allocated 30 bytes of memory. If you type in a 31 character line, the program will crash. (This is exactly the same as in BASIC). This is refered to as not being bounds checked and is reliant on the programmer not reserving too little space.

A possible final solution though is quite simple, but does restrict the user to not being able to use spaces.

The scanf() statement has as it's first argument the "%s" meaning to treat whatever is typed as a text string and that until return is pressed, that whatever is typed is the string. However, it is possible to modify this entry system by preceeding the s with 30 (so the argument looks like "%30s"). This tells the program only to accept the first 30 characters inputted.

Using this modifier provides one method of bounds checking, but also enables a far more powerful method of (say) restricting the number of decimal places printed to.

The only other aspect to note about this program is that every line (except for the #include and int main(void) lines) ends with a semi-colon. The semi colon is used in C to tell the compiler that this is the end of the line to be compiled. The semi colon is not required immediately after a closure of the loop or function brackets.

I think this has been enough of a very gentle introduction to the language.

As you can appreciate, like any computer language, it may take some time to get used to it's bits and pieces. However, with a bit of work, programming in C is not that difficult.

Next time, I shall cover the variable types and other methods of inputting data. Until then, there are 3 mistakes in this code snippet. Can you tell what they are?

#include <stdio.h>;

int main(void)
{
 printf("Hello there\nWho are you? ");
 scanf("%s",name);
 printf("Please to meet you %s",name);
 return 0;
};

answers next time!


email me with your comments

Return