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 (on special offer at £125 from CJE Micro's), 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.

OK, 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:

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 going through it, will still be of great use.

Ins and outs

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 all 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 three functions (two 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. The (void) bit 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 therein belongs to the function or loop.

char name[30];

Simply put, this has the same net effect as reserving 30 bytes of memory to be allocated to the variable name (this is akin to Basic's 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. The %s tells the program that whatever argument is next, it should be a string variable (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 it and run it - I would say that 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, 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 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 referred to as not being bounds checked and is caused by the programmer reserving too little space.

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

The scanf() statement has, as its first argument, the %s meaning to treat whatever is typed as a text string and that, until return is pressed, 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 of the input.

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.

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 semicolon. The semicolon is used in C to tell the compiler that this is the end of the line to be compiled. The semicolon 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, as with any computer language, it may take some time to get used to its 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 three mistakes in this code snippet:

#include <stdio.h>;
int main(void)
{
 printf("Hello there\nWho are you?");
 scanf("%s",name);
 printf("Nice to meet you %s",name);
 return 0;
};

Can you see them? Answers next time!


Source: Archive Magazine - 13.1
Publication: Archive Magazine
Contributor: Paul Johnson