Not Known
Programming: General
ZX Spectrum 48K

Adam Denning
Chris Bourne


Adam Denning lays bare the mysteries of C, a new language for the Spectrum which is faster than Forth and simpler than Pascal.

IF THE SPECTRUM was pitched against the BBC Micro for serious applications, it would not be at all surprising if it came out on top amongst programmers. That is almost entirely due to the efforts of one small software company up in Bedfordshire, Hisoft.

The company's release of its acclaimed Pascal compiler over a year ago, coupled with the simultaneous release of its machine code development system Devpac, has gained it and the Spectrum a higher credibility factor than Sinclair Research could ever manage by itself.

Now the company has released another compiler, this time for the language C. C is widely regarded as the language of the future, not least because of its unique position as the language in which Unix™ is written. Unix is now the de facto standard operating system for any 16-bit computer, with versions even available for the 68000 extended BBC Micro and soon the QL.

C is integral to all that, gaining a reputation as the systems language most likely to succeed. It has two main advantages over other languages like Basic and Pascal: First, C is standard, so programs are in practice portable - as opposed to the theoretical portability of Pascal and the mess created by the myriad versions of Basic. Second, it is block structured and far less formal than rivals of the Pascal ilk.

Two long words here, both of which need definition. A portable language is one which has exactly the same form on all machines, which means a program written in language X on machine Y will run without alteration on another machine Z that also supports language X. Only C gets truly close to that level of portability.

A block structured language is one in which each task in a program is broken down into small, easily manageable modules. That type of programming is much in vogue, with Pascal being the most shouted-about progenitor. The trend probably started with the BCPL language, developed by Dr Martin Richards in Cambridge, but Pascal and C soon became more widely used.

BCPL is in fact the grandfather of C, as two Americans, Dennis Ritchie and Brian Kernighan, took the offspring of BCPL - called B - and came up with C. The only book worth reading on C was written by those two Americans, and is called The C Programming Language published by Prentiss Hall and costing £16.95.

So the Spectrum now provides more opportunity for the budding systems programmer than any other home micro widely available. The provision of C gives distinct advantages to the Spectrum owner, as it is compiled into Z80 machine code and therefore runs faster than most other languages available - it beats the hell out of Forth.

Moreover, anyone contemplating a career in computing is going to need to know about the language, and this is the cheapest option there is. The average programmer will also find it useful as it is easier to use than Pascal and easier to learn. It can do almost everything which Spectrum Basic can do without any of the disadvantages.

Hisoft C is supplied on a cassette, containing everything - the compiler, the editor, the standard input/output header and a library of useful system routines. Once loaded the program asks if you wish to save the compiler and editor to microdrive. That follows Hi- soft philosophy of making all its products microdrive compatible, so that a user equipped with those devices need never be hampered by having to return to cassette tapes again.

The editor supplied as an integral part of the compiler is the standard Hisoft line editor used on all its source code based products, but programs can be written without recourse to the editor once you are familiar with both C and the implementation. The editor is invoked by pressing CAPS SHIFT and 1, i.e. EDIT, simultaneously, and from thereon all functions are standard until the c commnd is used to return to the C compiler.

C programs created that way can be saved to and loaded from tape or microdrive, and of course any section of saved source can be included in a compilation with the #include directive. That is standard practice in C compilers as it is usual for at least one header file containing all the #define definitions and standard i/o to be included in the source file.

Hisoft C has something else which, as far as we know, is unique - programs can be entered directly into the compiler just by typing them in, and subsequently invoked either by telling the machine that the end of the source file has been reached - by pressing SYMBOL SHIFT i - or by going into direct mode with #direct+ and typing a suitable invocation such as:


That direct mode also means that individual functions can be tested as stand alone programs, or quick calculations can be done. For instance, you might want to know the hexadecimal equivalent of 23456. All you have to do (assuming you are in direct mode) is type:

printf("%x/n", 23456);

and your answer will be printed out. You can leave direct mode by typing #direct-.

Before we look at some C programs and discuss how to use the language, we must look at some of the imperfections of the current version of Hisoft C. The most noticeable of those is that invocation of a non-existent function resets the machine, losing the compiler and returning to Basic.

The float type is also missing, which means that floating point numbers cannot be operated on. Oddly, the scanf function - the C equivalent of INPUT - is not here either, but it is fairly simple to write the parts of the function that you require.

Those are really the shortcomings worth mentioning, and even those will not be there for long. Hisoft will give a free update to purchasers of the current version of the compiler, and all the facilities mentioned as lacking are going to be included at some stage.

A program consists of a number of functions, of which one must be called main. That is where execution begins when the program is run. Of course, Hisoft's direct mode obviates the need for that, but it is best to follow the standard language definition so that later program development on other machines will not be hindered by silly mistakes.

The program in Figure 1 converts any number between 0 and 65535 into its hexadecimal and binary equivalents - a bit simple, perhaps, but then how many of us have programmed in C on the Spectrum before?

The program is simpler than it looks, but introduces a lot of C's more interesting properties. It was very hurriedly written, so it is by no means the best example of C programming.

The first thing we do in all three functions is to declare our variables, so that main has two integer variables and a character string (an array) 16 characters long, readn has three integers and a smaller string array and binary has two integers.

The only function that returns a result is readn, and the type of that result is indicated by declaring the function itself as having a type - integer in this case.

We also see that binary has two parameters passed to it. We have to tell it what type those parameters are, which is the purpose of the two lines after the function declaration.

The body of the program is controlled by main. It first prints a prompt and then uses our readn function to read in a decimal number, passing the integer value to the variable n. The binary function is then called to convert this into a 16 character binary string in the array b, and then the answers are printed out. The printf function can handle the printing of numbers in hex, but not binary, so we then use a for loop to print out the 16 digits of b in reverse order.

Notice the structure of the 'for loop' - the start value first, then the end condition and finally the increment. The ++ here is just the C way of incrementing.

As carriage returns and various other 'non-printing' characters are rather hard to represent on screen, C uses the backlash (\) as an escape character, so that various symbols after the slash are converted to various characters. Thus, \n is a new line character.

The readn function is a very simple method of reading in a number from the keyboard, and has two distinct disadvantages. It can only handle digits, so no sign must be included, and if you type in more digits than there are places in the s array, you will crash the computer. It is, however, quite fast. It simply reads in each digit from the keyboard at a time, using the getchar function until it finds a new line character. The != symbol means 'not equal to'.

The resulting string is then converted to decimal by taking each digit and progressively adding into ten times the total - a very standard method. That is then returned as the result.

A 'for loop' is also used by binary, this time decrementing from 15 to 0 to access each element of the array in reverse order. We use bitwise ANDing (the & operator) on powers of two to generate each binary digit. Each power of two is formed by shifting one left the requisite number of places (the << operator - pure BCPL).

That is by no means the extent of C's usefulness, and its reputation for systems programming is well founded but difficult to demonstrate briefly. It is the most welcome addition to the Spectrum catalogue since Sinclair introduced the Microdrives, and at the price cannot be missed. Hisoft is at: 180 High Street North, Dunstable, Bedfordshire. Tel. 0582 696421.

Memory: 48K
Price: £25.00


Screenshot Text

Figure 1.