[comp.sys.atari.st] C programming styles on the ST

braner@batcomputer.tn.cornell.edu (braner) (12/13/86)

[]

If you were wondering how come some (executable) programs are rather
large (30K) for what they do, while others are compact (5K), read on...

Having taken a look at the Dec. issue of Compute! magazine+disk (which
has various interesting stuff, including C source code for a simple
"Shell" program and Pascal source code for a data-graphing package),
I'd like to express the following:

When writing a C program for the ST, there are four styles possible:

	(1)  A standard, portable style, using printf(), scanf(), etc
		(need to #include <stdio.h>)

	(2)  A GEMDOS style using Cconrs(), Cconws(), etc
		(need to #include <osbind.h>)

	(3)  A free mix of both styles
		(need to #include BOTH <stdio.h> and <osbind.h>)

	(4)  A structured mix of both, using conditional compilation:

		#if AtST
			Cconws("blah blah blah\r\n");
		#else
			printf("blah blah blah\n");
		#endif

Examples of (1) are programs ported from UNIX as-is.
An example of (2) is my program SDECODE.
Examples of (3) are the Compute! programs.
An example of (4) is my version of microEMACS.

The advantage of (1) is portability, the advantage of (2) is compactness.
Your compiler's printf() library module is rather large, and is bound to
cause linking to the floating-point library too even if you do not use
any floating-point in your program.  Short utility programs in style (1)
(when compiled) can easily be twice as large as in style (2).  The latter
can be almost as compact as programs written in assembly language.

Style (3) is the worst: bulky AND nonportable!  There are ways to print
out integers, say, without using printf(): some compilers have a library
function itoa() (integer to ascii string) which can be used separately
from the prinf() stuff.  Or you can easily make your own.  Here is a
routine to interpret a number off a string:

int atoi(cp)
	register char *cp;		/* string address	*/
{
	register int c, x=0;
	while ((c=(*cp++)-'0')>=0 && c<=9)  x = 10*x + c;
	return (x);
}

For file-handling in style (2) use Fread(), Fwrite(), etc (the GEMDOS
calls) instead of the UNIX-style fread(), etc of style (1).  The
former functions, if used right, can work a LOT faster than the latter,
which is one reason to use style (4).

Hope this helps somebody...

- Moshe Braner