C Programming
Total Page:16
File Type:pdf, Size:1020Kb
C Programming Wikibooks.org June 22, 2012 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. An URI to this license is given in the list of figures on page 259. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 245. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 263, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 259. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, we recommend the use of http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting Save Attachment. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk Hünniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. This distribution also contains a configured version of the pdflatex compiler with all necessary packages and fonts needed to compile the LATEX source included in this PDF file. Contents 1 Why learn C? 3 2 History 5 3 What you need before you can learn 7 3.1 Getting Started . .7 3.2 Footnotes . 13 4 Using a Compiler 15 5 A taste of C 19 6 Intro exercise 21 6.1 Introductory Exercises . 21 7 Beginning C 23 8 Preliminaries 25 8.1 Basic Concepts . 25 8.2 Block Structure, Statements, Whitespace, and Scope . 25 8.3 Basics of Using Functions . 26 8.4 The Standard Library . 26 8.5 Comments and Coding Style . 27 9 Compiling 29 9.1 Preprocessor . 29 9.2 Syntax Checking . 30 9.3 Object Code . 30 9.4 Linking . 30 9.5 Automation . 30 10 Structure and style 33 10.1 C Structure and Style . 33 10.2 Introduction . 33 10.3 Line Breaks and Indentation . 34 10.4 Comments . 36 10.5 Links . 39 11 Error handling 41 11.1 Preventing divide by zero errors . 42 11.2 Signals . 42 III Contents 11.3 setjmp . 43 12 Variables 45 12.1 Declaring, Initializing, and Assigning Variables . 45 12.2 Literals . 47 12.3 The Four Basic Data Types . 47 12.4 sizeof ........................................ 49 12.5 Data type modifiers . 49 12.6 const qualifier . 50 12.7 Magic numbers . 50 12.8 Scope . 51 12.9 Other Modifiers . 52 13 Simple Input and Output 57 13.1 Output using printf() . 57 13.2 Other output methods . 59 13.3 fputs() . 60 13.4 Input using scanf() . 60 13.5 Links . 61 14 Simple math 63 14.1 Operators and Assignments . 63 15 Further math 69 15.1 Trigonometric functions . 69 15.2 Hyperbolic functions . 70 15.3 Exponential and logarithmic functions . 71 15.4 Power functions . 73 15.5 Nearest integer, absolute value, and remainder functions . 74 15.6 Error and gamma functions . 76 15.7 Further reading . 77 16 Control 79 16.1 Conditionals . 79 16.2 Loops . 86 16.3 One last thing: goto . 90 16.4 Examples . 91 16.5 Further reading . 91 17 Procedures and functions 93 17.1 More on functions . 94 17.2 Writing functions in C . 94 17.3 Using C functions . 96 17.4 Functions from the C Standard Library . 97 17.5 Variable-length argument lists . 101 18 Preprocessor 105 18.1 Directives . 105 18.2 Useful Preprocessor Macros for Debugging . 113 IV Contents 19 Libraries 119 19.1 Further reading . 121 20 Standard libraries 123 20.1 History . 123 20.2 Design . 124 20.3 ANSI Standard . 124 20.4 Common support libraries . 127 20.5 Compiler built-in functions . 127 20.6 POSIX standard library . 127 21 File IO 129 21.1 Introduction . 129 21.2 Streams . 129 21.3 Standard Streams . 130 21.4 FILE pointers . 130 21.5 Opening and Closing Files . 131 21.6 Other file access functions . 132 21.7 Functions that Modify the File Position Indicator . 133 21.8 Error Handling Functions . 135 21.9 Other Operations on Files . 135 21.10Reading from Files . 137 21.11Writing to Files . 144 21.12References . 152 22 Beginning exercises 153 22.1 Variables . 153 22.2 Simple I/O . 155 22.3 Program Flow . 159 22.4 Functions . 159 22.5 Math . 159 23 In-depth C ideas 161 24 Arrays 163 24.1 Arrays . 163 24.2 Strings . 165 25 Pointers and arrays 167 25.1 Declaring pointers . 168 25.2 Assigning values to pointers . 168 25.3 Pointer dereferencing . ..