ebook img

C from theory to practice PDF

516 Pages·2014·2.44 MB·English
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 C from theory to practice

George S. Tselikis | Nikolaos D. Tselikas InformatIon technology C C From Theory to Practice F r o m Designed for a compulsory fundamental course, C: From Theory to Practice T uses a hands-on approach to teach the C programming language, using numerous h examples and a clear, concise presentation. Easy to use and classroom e tested, this textbook includes more than 500 exercises and examples of o progressive difficulty to help students in understanding all the aspects and r y peculiarities of C. The exercises test students on various levels of programming, and the examples enhance their concrete understanding of programming t o know-how. Divided into three parts, this book: P • Introduces the basic concepts of C, like getting input from r a a user, C’s operators, selection statements, and loops. c • Emphasizes major features of C, such as arrays, pointers, t functions, and strings. i C c • Covers advanced topics, such as like searching and sorting e From Theory to Practice arrays’ algorithms, structures and unions, memory management, the preprocessor, and files. The book tests the skills of beginners and advanced developers by providing an easy-to-read compilation of the C theory enriched with tips and advice as well as difficulty-scaled solved programming exercises. It decodes the secrets of the C language, providing inside information and programming T s knowledge through practical examples and meaningful advice. The examples e l are designed to be short, concrete, and substantial, quickly giving students ik i the know-how they need. s | T s e l ik | 500+ difficulty-scaled solved programming exercises a s | Substantial, non-tiring, and easy-to-read presentation of the C theory | Emphasis on the complex aspects of the C language K21756 ISBN-13: 978-1-4822-1450-5 | Tips to avoid programming bugs and implement efficient and 90000 clear C programs 9 781482 214505 K21756_Cover_mech.indd All Pages 1/22/14 3:19 PM C From Theory to Practice C From Theory to Practice George S. Tselikis | Nikolaos D. Tselikas Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Group, an informa business CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2014 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Version Date: 20140108 International Standard Book Number-13: 978-1-4822-1451-2 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid- ity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or uti- lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy- ing, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents Preface ...........................................................................................................................................xiii Acknowledgments ........................................................................................................................xv Authors ........................................................................................................................................xvii 1. Introduction to C.....................................................................................................................1 History of C ..............................................................................................................................1 ANSI Standard .........................................................................................................................1 Advantages of C .......................................................................................................................2 Disadvantages of C ..................................................................................................................2 C Program Life Cycle ..............................................................................................................2 Write a C Program ...................................................................................................................3 First C Program ........................................................................................................................3 #include Directive ...........................................................................................................3 main() Function .................................................................................................................3 Add Comments ...................................................................................................................4 Compilation ..............................................................................................................................5 Common Errors ...................................................................................................................6 Linking ......................................................................................................................................6 Run the Program ......................................................................................................................6 Reference ...................................................................................................................................7 2. Data Types, Variables, and Data Output ...........................................................................9 Variables ....................................................................................................................................9 Rules for Naming Variables ...................................................................................................9 Variable Name Conventions .................................................................................................10 Declaring Variables ...............................................................................................................10 Assigning Values to Variables ..............................................................................................12 Constants .................................................................................................................................13 #define Directive .................................................................................................................13 printf() Function ................................................................................................................14 Escape Sequences ..............................................................................................................15 Conversion Specifications ................................................................................................16 Return Value ......................................................................................................................17 Printing Variables .............................................................................................................18 Optional Fields ..................................................................................................................19 Precision ........................................................................................................................19 Field Width ....................................................................................................................21 Prefix ..............................................................................................................................22 Flags ...............................................................................................................................22 Type Casting ...........................................................................................................................23 Exercises ..................................................................................................................................24 Unsolved Exercises ................................................................................................................27 v vi Contents 3. Getting Input with scanf() ..............................................................................................31 scanf() Function ..................................................................................................................31 scanf() Examples ............................................................................................................31 Use of Ordinary Characters ............................................................................................33 scanf() Return Value .....................................................................................................33 Exercises ..................................................................................................................................34 Unsolved Exercises ................................................................................................................40 4. Operators ................................................................................................................................41 Assignment Operator ............................................................................................................41 Arithmetic Operators ............................................................................................................41 Increment and Decrement Operators .................................................................................42 Relational Operators ..............................................................................................................44 Exercises ..................................................................................................................................44 Not Operator ! ........................................................................................................................45 Exercises ..................................................................................................................................46 Compound Operators ...........................................................................................................46 Exercise ....................................................................................................................................47 Logical Operators...................................................................................................................47 Operator && ........................................................................................................................47 Operator ||.........................................................................................................................48 Exercises ..................................................................................................................................48 Comma Operator ...................................................................................................................50 Exercise ....................................................................................................................................51 sizeof Operator ...................................................................................................................52 enum Type ...............................................................................................................................52 Bitwise Operators ..................................................................................................................53 & Operator ..........................................................................................................................54 | Operator ..........................................................................................................................54 ˄ Operator ...........................................................................................................................54 ∼ Operator ..........................................................................................................................54 Exercise ....................................................................................................................................55 Shift Operators .......................................................................................................................55 >> Operator ........................................................................................................................55 << Operator ........................................................................................................................56 Exercises ..................................................................................................................................56 Operator Precedence .............................................................................................................61 Unsolved Exercises ................................................................................................................61 5. Program Control ...................................................................................................................63 if Statement ...........................................................................................................................63 Common Errors .................................................................................................................64 if-else Statement ................................................................................................................64 Nested if Statements ............................................................................................................65 Exercises ..................................................................................................................................68 Conditional Operator ?: .......................................................................................................76 Exercises ..................................................................................................................................77 switch Statement ..................................................................................................................79 switch versus if .............................................................................................................82 Contents vii Exercises ..................................................................................................................................82 Unsolved Exercises ................................................................................................................86 6. Loops .......................................................................................................................................89 for Statement .........................................................................................................................89 Omitting Expressions .......................................................................................................91 Exercises ..................................................................................................................................92 break Statement ....................................................................................................................94 continue Statement .............................................................................................................94 Exercises ..................................................................................................................................95 Nested Loops ........................................................................................................................103 Exercises ................................................................................................................................105 while Statement ..................................................................................................................109 Exercises ................................................................................................................................110 do-while Statement ...........................................................................................................118 Exercises ................................................................................................................................119 goto Statement ....................................................................................................................121 Unsolved Exercises ..............................................................................................................122 7. Arrays ....................................................................................................................................125 Declaring Arrays ..................................................................................................................125 Accessing Array Elements ..................................................................................................126 Array Initialization ..............................................................................................................127 Exercises ................................................................................................................................128 Two-Dimensional Arrays ....................................................................................................140 Two-Dimensional Array Declaration ...........................................................................140 Accessing the Elements of a Two-Dimensional Array ..............................................140 Two-Dimensional Array Initialization ........................................................................141 Exercises ................................................................................................................................142 Unsolved Exercises ..............................................................................................................151 8. Pointers .................................................................................................................................153 Pointers and Memory ..........................................................................................................153 Declaring Pointers ...............................................................................................................153 Pointer Initialization ............................................................................................................154 NULL Value ............................................................................................................................155 Use a Pointer .........................................................................................................................155 Exercises ................................................................................................................................157 void* Pointer .......................................................................................................................163 Use of const Keyword .......................................................................................................163 Pointer Arithmetic ...............................................................................................................163 Pointers and Integers ......................................................................................................164 Subtracting Pointers........................................................................................................164 Comparing Pointers ........................................................................................................165 Exercises ................................................................................................................................165 Pointers and Arrays .............................................................................................................168 Exercises ................................................................................................................................171 Arrays of Pointers ................................................................................................................180 Exercises ................................................................................................................................181 viii Contents Pointer to Pointer .................................................................................................................182 Exercises ................................................................................................................................183 Pointers and Two-Dimensional Arrays ............................................................................184 Exercises ................................................................................................................................187 Pointer to Function ..............................................................................................................187 Exercise ..................................................................................................................................189 Array of Pointers to Functions ...........................................................................................189 Unsolved Exercises ..............................................................................................................191 9. Characters .............................................................................................................................193 char Type .............................................................................................................................193 Exercises ................................................................................................................................194 getchar() Function ...........................................................................................................198 Exercises ................................................................................................................................198 10. Strings ...................................................................................................................................203 String Literals .......................................................................................................................203 Storing Strings ......................................................................................................................203 Exercises ................................................................................................................................204 Writing Strings .....................................................................................................................205 Exercise ..................................................................................................................................207 Pointers and String Literals ................................................................................................207 Exercises ................................................................................................................................209 Read Strings ..........................................................................................................................210 For a Safe Reading of Strings .............................................................................................212 Exercises ................................................................................................................................213 String Functions ...................................................................................................................217 strlen() Function .........................................................................................................217 Exercises ................................................................................................................................218 strcpy() Function ........................................................................................................224 Exercises ................................................................................................................................225 strncpy() Function ......................................................................................................227 strcat() Function .........................................................................................................227 strcmp() and strncmp() Functions .........................................................................228 Exercises ................................................................................................................................229 Two-Dimensional Arrays and Strings ..............................................................................236 Exercises ................................................................................................................................237 Unsolved Exercises ..............................................................................................................240 11. Functions ..............................................................................................................................243 Function Declaration ...........................................................................................................243 Return Type ..........................................................................................................................243 Function Parameters ............................................................................................................244 Function Definition .............................................................................................................244 return Statement ...............................................................................................................245 Function Call ........................................................................................................................246 Function Call without Parameters ................................................................................246 Function Call with Parameters .....................................................................................248 Passing Values .................................................................................................................249

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.