ebook img

Programming in ANSI C - Loughborough University Institutional PDF

258 Pages·2016·0.57 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming in ANSI C - Loughborough University Institutional

Loughborough University Institutional Repository Programming in ANSI C ThisitemwassubmittedtoLoughboroughUniversity’sInstitutionalRepository by the/an author. Citation: DAWSON, R., 2001. Programming in ANSI C. Third Edition. Loughborough: Group D Publications Ltd. Additional Information: • This is a book. Metadata Record: https://dspace.lboro.ac.uk/2134/10054 Version: Published Publisher: Group D Publications Ltd. Please cite the published version. This item was submitted to Loughborough’s Institutional Repository (https://dspace.lboro.ac.uk/) by the author and is made available under the following Creative Commons Licence conditions. For the full text of this licence, please go to: http://creativecommons.org/licenses/by-nc-nd/2.5/ Programming in C ANSI Third Edition Ray Dawson Group D Publications 2 Programming in ANSI C This is an online copy of a book published by Group D Publications Ltd. Apart from this page it is an identical copy of the third edition of “Programming in ANSI C” by Ray Dawson All rights reserved. This online version of the book is provided for personal and educational use only. No part of this book may be reproduced in any form, by photostat, microfilm, retrieval system, or by any other means, without the prior permission of the publisher except in the case of duplication by professional educators for use by their students. Copies of this online version cannot be duplicated and sold for profit by any organisation. Copyright © 2006 by Ray Dawson, Loughborough University. First edition published 1993 (ISBN 1-874152-02-0) Second revised and enlarged edition published 1996 (ISBN 1-874152-03-9) Third edition published in 2001 (ISBN 1-874152-10-1) British Library Cataloguing-in-Publication data A catalogue record for this book is available from the British Library Unix™ is a trademark of AT&T Bell Laboratories DEC™ is a trademark of Digital Equipment Corporation Hard copies of this book should be ordered from the author: Ray Dawson Department of Computer Studies Loughborough University Loughborough, Leicestershire LE11 3TU Telephone: 01509-222679 Fax: 01509-211586 Email: [email protected] Preamble 3 Preamble This third addition of this book has been published by popular demand. I am very pleased by the way the book has been received by students, members of the teaching staff, and by software professionals in industry. On the whole the "no nonsense" approach of getting to the point without introducing hundreds of pages of basic information on how to program has been well received. Only two serious criticisms have been made about the first edition and these have been tackled in later editions. Firstly, some lecturers and some students complained there were no exercises in the book and secondly, some also complained there were not enough examples of C code. By including a set of exercises and a set of sample solutions I believe I have satisfied both requests together. Other improvements are relatively minor, an odd correction here, an odd expanded explanation there, but I believe the net result will be an even better book for students, teachers and software professionals alike. The only difference between the second and third edition is in the binding - this new edition should prove more robust. Acknowledgements I would like to thank Group D Publications for publishing this book. My thanks also go to the Department of Computer Studies, and in particular, Professor Jim Alty, for providing the money and resources to enable this book to be published. Finally I must thank my colleague, Satish Bedi, for his helpful comments on the first edition of this book, and for bringing to my attention the corrections required - he has made a significant contribution towards the improved accuracy of this edition. Dedication I would like thank my wife, Dawn, and my sons, Matthew and Alex, for their support while I was producing this book. I dedicate this book to them. Ray Dawson 4 Programming in ANSI C Contents Part A: The C Language Pages 6-123 Section 1 : Introduction and Overview 12 Section 2 : Constants and Variables 15 Section 3 : Assignments and Expressions 22 Section 4 : Introduction to Simple Input and Output Statements 35 Section 5 : Arrays 40 Section 6 : Conditional Statements 48 Section 7 : Other Control Flow Statements 56 Section 8 : Structures and Unions 63 Section 9 : Introduction to Functions 73 Section 10 : Pointers 92 Section 11 : Storage Classes 106 Section 12 : Input and Output To Files 112 Section 13 : Other C Features 116 Appendix A : Operator Precedence Table 123 Part B: The C Pre-processor Pages 124-135 Section 1 : The 'C' Pre-processor 126 Part C: The Standard C Library Pages 136-173 Section 1 : Introduction to the Standard 'C' Library 141 Section 2 : Output From The Terminal 142 Section 3 : Input From The Terminal 145 Section 4 : Formatted Conversion In Memory 149 Section 5 : File Access Using File Pointers 151 Section 6 : File I/O Functions 153 Section 7 : File Access Using File Descriptor Numbers 160 Section 8 : String Functions 162 Section 9 : Character Functions 164 Section 10 : Mathematical Functions 166 Section 11 : Memory Allocation Functions 168 Section 12 : System Functions 170 Contents 5 Part D: C Program Accuracy and Style Pages 174-203 Section 1 : Run Time Error Check List for C Programs 176 Section 2 : Quality Check List for C Programs 189 Part E: Sample Solutions to the Exercises Pages 204-252 Section 1 : Sample Solutions to C Exercise 1 207 Section 2 : Sample Solutions to C Exercise 2 208 Section 3 : Sample Solutions to C Exercise 3 211 Section 4 : Sample Solutions to C Exercise 4 213 Section 5 : Sample Solutions to C Exercise 5 215 Section 6 : Sample Solutions to C Exercise 6 218 Section 7 : Sample Solutions to C Exercise 7 220 Section 8 : Sample Solutions to C Exercise 8 222 Section 9 : Sample Solutions to C Exercise 9 224 Section 10 : Sample Solutions to C Exercise 10 228 Section 11 : Sample Solutions to C Exercise 11 234 Section 12 : Sample Solutions to C Exercise 12 238 Section 13 : Sample Solutions to C Exercise 13 242 Section 14 : Sample Solutions to C Pre-processor Exercise 247 Index Pages 253-256 Part A : The C Programming Language 6 Programming in ANSI Third Edition PART A The C Language Part A : The C Programming Language 7 Part A : Contents Page Section 1 : Introduction and Overview 12 1.1 'C' History and Background 12 1.2 Example 'C' Program 13 1.3 C Program Structure 13 1.4 C Exercise 1 14 Section 2 : Constants and Variables 15 2.1 Declaring Data Variables 15 2.2 Notes on Variable Types 15 2.3 The Format of Variable Declarations 16 2.4 Where Variables are Declared 17 2.5 Number Constants 18 2.6 Character Constants 18 2.7 Character Constants and String Constants 19 2.8 Initialisation of Variables 20 2.9 "Constant" Variables and the const Qualifier 20 2.10 C Exercise 2 21 Section 3 : Assignments and Expressions 22 3.1 Simple Assignment Statements 22 3.2 Arithmetic Operators 22 3.3 Notes on Arithmetic Operators 23 3.4 Dividing Integers 23 3.5 Shift Operators 24 3.6 The Bitwise Operators: ~ & | and ^ 25 3.7 The ~ Operator 25 3.8 The & Operator 25 3.9 The | Operator 26 3.10 The ^ Operator 26 3.11 Mixing Variable Types 26 3.12 The C Handling of char and short Variables 27 3.13 Converting int Variables to char And short 27 3.14 Mixtures of Variable Types in Expressions 28 3.15 Mixed Variable Type Assignments 29 3.16 Assigning Negative Values to Unsigned Variables 29 3.17 Warning! There Are NO Warnings! 30 3.18 Casts 30 3.19 Different Assignment Operators 31 3.20 Embedded Statements 31 3.21 Using Embedded Statements 32 3.22 Embedded ++ and -- Operators 32 3.23 C Exercise 3 33 Part A : The C Programming Language 8 Page Section 4 : Introduction to Simple Input and Output Statements 35 4.1 Introduction to Input and Output 35 4.2 The getchar() Function 35 4.3 putchar(character) 36 4.4 printf(format,values); 37 4.5 printf Substitution Types 37 4.6 printf Substitution Modifiers 38 4.7 C Exercise 4 39 Section 5 : Arrays 40 5.1 Arrays 40 5.2 Limitations and Dangers in the Use of an Array 40 5.3 Strings 41 5.4 The gets(chararray) Function for Reading Strings 42 5.5 Initialisation of Arrays 42 5.6 Two Dimensional Arrays 43 5.7 Arrays of Arrays 43 5.8 Using Individual Rows 44 5.9 Array Syntax Warning! 44 5.10 Multi Dimensional Arrays 44 5.11 Initialising Multi Dimensional Arrays 45 5.12 C Exercise 5 46 Section 6 : Conditional Statements 48 6.1 The if Statement 48 6.2 Logical (Boolean) Variables 48 6.3 Confusion of = and == 49 6.4 The && and || operators 49 6.5 Common Errors of Multiple Conditions 50 6.6 Confusion of && and || with & and | 51 6.7 Evaluation of Multiple Conditions 51 6.8 The ! Operator 52 6.9 The else Statement 52 6.10 Grouping Statements With {} 52 6.11 Layout of {} Blocks and Code 53 6.12 if (...) if (...) Statements 54 6.13 if ... else if ... else Construction 54 6.14 The ?: Operator Pair 55 6.15 C Exercise 6 55

Description:
Loughborough University. Institutional Repository. Programming in ANSI C. This item was submitted to Loughborough University's Institutional Repository.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.