I've been playing around with this and, I have to say, the Wiki is useful but not very helpful, and I can't find many clear examples that'd be good for beginners to get their teeth into. This, in my opinion, is due to lack of good commenting in the source code examples, and also it being written by many technically minded people who forget that sometimes things need to be explained in English, which is a language that I'm quite fond of as it can be very clear and concise if used well. So, I intend to write a beginners guide to C programming for the ZX Spectrum in the near future.
Anyway, after scratching my head for a few hours, I wrote a simple 'Hello World!' type program. This isn't a challenge, of course, but I wanted to change the colours of the text and bypass the 64-column mode which is what the z88dk compiler defaults to when you use it. I also got it to drop down to assembly for a short routine which sets up the default colours for the whole screen. Anyway, more importantly, I've commented it quite well so even if you're a complete novice at programming and would struggle with something simple, it should be straight forward enough. Note that there is a much simpler way to do the same thing, but simple doesn't always tell you everything that you need to know. Oh, and it also uses a look-up table, something that I always find myself building even if it's not always strictly necessary.
Here's the code:
/** This does something similar to the classic
* "Hello World!" code that's a popular starting
* point for learning programming. */
#include < stdio.h > // Remove the spaces here as Blogger thinks it's an HTML tag
// Here are our default colour attributes
#define INK 7
#define PAP 2
#define FLA 0
#define BRI 1
#define INV 0
#define BOR 5
// This is used in the setup function, which drops into
// machine code - see my blog for more information:
#define COL 128*FLA+64*BRI+8*PAP+INK
// Forward declarations of functions:
static void main(void);
static void setup(void);
static void hello(void);
static void magazine(void);
// Global variables:
static int i=0;
// Here is an array which will set up the default colour
// attributes for printing our message to the screen,
// 255 is used as a 'terminator' to say "we've finished here"
static int screensetup=
// This is the 'entry point' of our program:
// This will call a routine to set the default
// colour arrtibutes for the whole screen as
// defined above:
// This does the same for outputting out character
// see http://www.z88dk.org/wiki/doku.php?id=platform:zx
// for more information under the heading "The standard
// ZX Spectrum console driver" - hopefully, these numbers
// will now make more sense!
// The %c means 'print character code' or something similar
// Increase i to read the next element of the array:
// Calls our functions, firstly hello:
// and now magazine:
// This function sets up the default colours for our screen as
// defined above:
// Sets default ink and paper colour, then clears screen
// Sets border colour
// Here is where the magic happens, can you tell what it does?
// And what about this bad boy then?
Call the source code "HelloMagazine.c" (obviously without the quotation marks) and place it in the same directory as the z88dk 'bin' folder, go to your command line prompt and compile it with:
zcc +zx -lndos -create-app -o hello HelloMagazine.c
it should create a file called "hello.tap", simply load this into your emulator and watch with amazement as the magic actually happens!
PS, I'm certainly not endorsing any printed-matter publication which has all of the latest celeb goss or whatever - it's a joke as I've read too many times about "Hello World!" being the default starting point, so I always try to avoid it.