amiga shopper complete amiga c

Download Amiga Shopper Complete Amiga C

If you can't read please download the document

Post on 17-Sep-2015

27 views

Category:

Documents

11 download

Embed Size (px)

DESCRIPTION

Amiga Shopper presents the Complete Amiga C language.

TRANSCRIPT

  • Complete igaC

    Everything you need to start programming your Amiga in (

    Cliff Ramshaw

    ~

    ~ BOOKS

  • Complete Amiga C Copyright 1994 Future Publishing All rights reserved. No part of this book may be duplicated, stored in a retrieval system or used as part of any other book, database, program or other commercial application without the publisher's express written permission.

    Book design and production Rod Lawton

    Cover design Rod Lawton

    Cover illustration Paul Kidby

    Author Cliff Ramshaw

    First published in 1994 by Future Publishing, Beauford Court, 30 Monmouth Street, Bath, Avon BA1 2BW.

    ISBN 1 898275 106

    Printed in the UK by Beshara Press

    Acknowledgement of copyright and trademarks Amiga Includes and Development Tools Version 1.3 Copyright 1985-1988 Commodore-Amiga, Inc. Amiga Includes and DevelopmentTools Version 2.0 Copyright 1990-1991 Commodore-Amiga, Inc. All rights reserved. Distributed under license from Commodore. Amiga, AmigaDOSTM, Kickstart, Intuition and Workbench are trademarks of Commodore-Amiga, Inc. This book contains copyright or trademark product names owned by the companies which produce them. Description of these products without mention of their legal status does not constitute a challenge to this status. The author and Future Publishing fully acknowledge such copyright names or trademarks.

    The included disks The software on these disks is not guaranteed for use in any specific situation. The publishers accept no liability for problems arising from its use. The contents of the disk are copyright, and are not to be copied or distributed in the public domain. If one or more of your disks fails to work, you should return the disk with a brief description of the problem to: Complete Amiga C Disk Returns, DisCopy Labs, Units 2/3, Omega Technology Centre, Drayton Fields, Daventry, Northants NN11 5RT, U.K.

    Commodore libraries and includes This software is designed to be used solely in conjunction with the Commodore Amiga computer. COMMODORE DOES NOT WARRANT, GUARANTEE, NOR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THE PROGRAM, IN TERMS OF CORRECTNESS, ACCURACY, RELIABILITY, CURRENTNESS OR OTHERWISE, RATHER, THE PROGRAM IS PROVIDED "AS-IS", WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILlTY AND FITNESS FOR A PARTICULAR PURPOSE. RISK AS TO THE RESULTS AND PERFORMANCE OF THE PROGRAM IS ASSUMED BY LICENSEE. COMMODORE SHALL NOT BE HELD TO ANY LIABILITY WITH RESPECT TO ANY CLAIM BY LICENSEE OR A THIRD PARTY ON ACCOUNT OF, OR ARISING FROM, THE USE OF THE PROGRAM PURSUANT TO THIS AGREEMENT.

  • y

    Contents Preface x

    Introduction XI What 'Complete Amiga (' will teach you How to use the book What is DICE? Amiga libraries and includes

    Chapter 1: Basic concepts Processors and memory - how they work together

    1

    Programming languages Functions and sub-routines

    Chapter 2: Setting up your system Backing up your'bICt disks

    15

    Floppy disk installation Hard disk installation Final steps What you've got

    Chapter 3: Basic C programming Analysing a simple program

    25

    Numbers and variables Variable types Expressions, operators and operands Handling user input Mixing variable types

    Complete Amiga C

  • vi

    Chapter 4: Decision-making Reducing statements to 'true' or 'false'

    39

    'Switching' statements 'If' statements & 'logical expressions' Relational operators Loops 'Goto' statements and 'labels' 'Do while ... ' and 'while ... ' Logical operators (AND, OR, NOT)

    Chapter 5: loops elaborated Arrays

    59

    Symbolic constants and pre-processing Loop counters - the 'for' loop Character variables Escape sequences String variables Initialising arrays Handling string arrays

    Chapter 6: Functions Modular program planning

    81

    Functions, and how they're constructed Prototyping functions Function' side effects'

    Chapter 7: Pointers More about decision-rnaking

    97

    Modifying arguments with pointers Pointers and arrays

    Complete Amiga C

  • vii

    Chapter 8: Pointers and strings Two-dimensional arrays

    121

    Modifying our' sort' program Standard library string functions Initialising arrays on declaration Sharing variables amongst functions

    Chapter 9: Writing a spreadsheet The planning stage

    143

    Designing the program How the program works

    Chapter 1 0: Recursion Modifying our spreadsheet

    161

    Dato structures Recursive data structures

    Chapter 11: linked lists How lists work

    185

    Lists of any type Pointers to functions

    Chapter 12: Additional concepts More variable types

    209

    Pre-processing Oxal and hexadecimal Bitwise operators Precedence Automatic variables

    Complete Amiga C

  • viii

    Chapter 13: Interfacing with the machine Handling files

    233

    C and the Shell Useful pre-defined functions

    Chapter 14: Eliminating errors Errors of language

    245

    Errors of meaning Three debugging tips

    Chapter 15: Using Amiga libraries Amiga-specific C

    255

    Exec Using libraries - examples

    Chapter 16: 'Four-in-a-Row' Planning a game

    273

    Programming a computer opponent The game code

    Index 305

    Epilogue 313

    Complete Amiga C

  • ix

    About the author Cliff Ramshaw is the editor of Amiga Shopper, the UK's best-selling serious Amiga magazine, which each month produces articles for Amiga owners who are interested in learning more about their machine. Before that he worked on the magazine for two years as first technical editor and then deputy editor.

    As well as working previously as a programmer in a business environment, Cliff has also produced many commercial games and has a number of books on games programming to his credit, including Zap Pow Boom - Arcade And Other Games for the VIC-20, VIC Innovative Computing and The Commodore 64 Games Book.

    After working for many years with BASIC and assembly language, Cliff finally discovered C - a damned fine language and no mistake - and has never looked back. One of his main motivations for writing this book was the feeling that other C tutorials are aimed at people already conversant with general programming techniques, whereas he feels there is no reason whatsoever why it cannot be taught to peop~ with no previous programming experience.

    Thanks to The author would like to acknowledge the help of the following: Toby Simpson of Millennium, for his help in removing the bugs from the Four In A Row program, and Mark Harman of North London University for his polymorphic list code and the game engine for the Four In A Row program.

    Complete Amiga C

  • ][

    Complete Amigo C

    Preface Sooner or later, most computer owners get around to programming. Maybe it's not the reason they bought the machine, and maybe it's not the first thing they wanted to do with it, but the lure of custom-designed software, the creative satisfaction out of producing a program and even the prospect of selling it usually tempt us all into dabbling with programming.

    You can opt for a 'beginners' language like BASIC. Easy to learn, but slow and c1unky and not remotely 'professional'. Or you can jump in at the deep end and learn assembler - the nearest you can get to raw 'machine language'. The results are as good as the machine can deliver, but you need a certain sort of mind ... and have to be prepared to lose it.

    Or you can learn C. C is the 'professionals' programming language. A version exists for every machine under the sun, so you only ever have to learn the language once. You write your programs in a language that the ordinary human mind can grasp, and then the C compiler turns it into fast machine language. The C programming language is the best compromise there is between understandability and efficiency.

    Which is just how we like to think of 'Complete Amiga C' .

    We've put everything we can into it so that you can get as much as possible out of it. Good luck, and have fun!

  • xi

    Introduction We thought long and hard about a title for this book. In the end we chose 'Complete Amiga C' because we were able to put together a complete package - one that incorporated a book that told you how to program in C, a compiler that let you actually do so and the Commodore 'library' and 'include' files that let you use the Amiga's specific hardware features, e.g. its advanced graphics and sound.

    We started off wanting to produce a book on programming the Amiga. C is the obvious language to write about - it's the professionals' choice. Then we thought, 'how can we make this book better'? Simple. By including a C compiler so that readers could not only read about C but try it out too. From there it seemed pretty logical to choose DICE, one of the best C compilers available. And rather than providing the shareware version only, we decided to go the whole hog and sort out a deal on the full, registered version.

    This wasn't an especially cheap solution. But with a bit of negotiation with our various suppliers, we kept the price down to 24.95. Which, we think, is not bad for a book and a complete C programming environment. If you bought any of the leading commercial packages you could expect to pay up to ten times that amount. We're rather pleased with that.

    What we realised we couldn't do, though, was produce a book containing everything there was to know about C programming on the Amiga. The subject is just t