Example: barber

Programming in ANSI C - karadev.net

Loughb oroughUniversityInstitutionalRep ositoryProgramminginANSICT hisitemwassubmittedtoLoughb oroughUniversity'sInstitutionalRep :DAWSON,R., : Thisisab o : : This item was submitted to Loughborough s Institutional Repository ( ) by the author and is made available under the following Creative Commons Licence conditions. For the full text of this licence, please go to: Programming in ANSICT hird 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.

Part A : The C Programming Language 9 Page Section 7 : Other Control Flow Statements 56 7.1 The while Statement 56 7.2 The do .. while Statement 57 7.3 The switch Statement 57 7.4 The for Statement 59 7.5 The break and continue Statements 60 7.6 The Comma Operator 60 7.7 The goto Statement 61 7.8 C Exercise 7 62 Section 8 : Structures and Unions 63

Tags:

  Programming, C programming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Programming in ANSI C - karadev.net

1 Loughb oroughUniversityInstitutionalRep ositoryProgramminginANSICT hisitemwassubmittedtoLoughb oroughUniversity'sInstitutionalRep :DAWSON,R., : Thisisab o : : This item was submitted to Loughborough s Institutional Repository ( ) by the author and is made available under the following Creative Commons Licence conditions. For the full text of this licence, please go to: Programming in ANSICT hird 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.

2 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: Preamble 3 Preamble This third addition of this book has been published by popular demand.

3 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.

4 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.

5 Ray Dawson4 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.

6 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 ANSIP rogramming in Third EditionPART AThe C LanguagePart A.

7 The C Programming Language 7 Part A : ContentsPageSection 1 : Introduction and Overview12 'C' History and Background12 'C' Program13 Program Structure13 Exercise 114 Section 2 : Constants and Variables15 Data Variables15 on Variable Types15 Format of Variable Declarations16 Variables are Declared17 Constants18 Constants18 Constants and String Constants19 of Variables20 "Constant" Variables and the const Qualifier20 C Exercise 221 Section 3 : Assignments and Expressions22 Assignment Statements22 Operators22 on Arithmetic Operators23 Integers23 Operators24 Bitwise Operators: ~ & | and ^25 ~ Operator25 & Operator25 | Operator26 The ^ Operator26 Mixing Variable Types26 The C Handling of char and short Variables27 Converting int Variables to char And short 27 Mixtures of Variable Types in Expressions28 Mixed Variable Type Assignments29 Assigning Negative Values to Unsigned Variables29 Warning!

8 There Are NO Warnings!30 Casts30 Different Assignment Operators31 Embedded Statements31 Using Embedded Statements32 Embedded ++ and -- Operators32 C Exercise 333 Part A : The C Programming Language 8 PageSection 4 : Introduction to Simple Input and Output Statements35 to Input and Output35 getchar() Function35 (character)36 (format,values);37 Substitution Types37 Substitution Modifiers38 Exercise 439 Section 5 : Arrays40 and Dangers in the Use of an Array40 gets(chararray) Function for Reading Strings42 of Arrays42 Dimensional Arrays43 of Arrays43 Individual Rows44 Syntax Warning!44 Multi Dimensional Arrays44 Initialising Multi Dimensional Arrays45 C Exercise 546 Section 6 : Conditional Statements48 if Statement48 (Boolean) Variables48 of = and == 49 && and || operators49 Errors of Multiple Conditions50 of && and || with & and |51 of Multiple Conditions51 !

9 Operator52 else Statement52 Grouping Statements With {}52 Layout of {} Blocks and Code53 (..) if (..) Statements54 .. else if .. else Construction54 The ?: Operator Pair55 C Exercise 655 Part A : The C Programming Language 9 PageSection 7 : Other Control Flow Statements56 while Statement56 do .. while Statement57 switch Statement57 for Statement59 break and continue Statements60 Comma Operator60 goto Statement61 Exercise 762 Section 8 : Structures and Unions63 is a Structure?63 Type Declarations63 Declarations64 Structure Members65 Whole Structures65 of Structures66 Bit Fields66 Structure Bit Fields67 Union Declaration68 Referencing Whole Unions69 Why Use a Union?

10 69 Nesting Structures and Unions70 Initialising Unions71 C Exercise 872 Section 9 : Introduction to Functions73 is a Function?73 Use a Function?73 Call, Definition and Declaration73 Simple Function Example74 On Using Functions75 Variables76 Variables77 Variables in Different Functions78 and Local Variables of the Same Name79 Function Parameters80 Notes on Function Parameters80 Function Parameter Limitations81 Notes on the Function Prototype82 The Use of the Elipses ..83 Function Return Values83 Function Return Types84 Part A : The C Programming Language 10 Page Declaring Function Return Types85 The return Statement86 Further Notes on Function Return Values86 Structures as Function Parameters87 Structure Return Values88 Arrays Used With Functions88 Unusual Properties of Array Parameters89 C Exercise 990 Section 10 : is a Pointer and Why Use One?


Related search queries