C Programming
Total Page:16
File Type:pdf, Size:1020Kb
C Programming en.wikibooks.org November 24, 2013 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. A URI to this license is given in the list of figures on page 273. 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 265. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 277, 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 273. 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, you can use the pdfdetach tool including in the poppler suite, or the http://www. pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save the attachment to a file. 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. Contents 1 Why learn C? 3 2 History 7 3 What you need before you can learn 9 3.1 Getting Started . 9 3.2 Footnotes . 12 4 Using a Compiler 13 5 A taste of C 19 6 Intro exercise 21 6.1 Introductory Exercises . 21 7 Beginning C 25 8 Preliminaries 27 8.1 Basic Concepts . 27 8.2 Block Structure, Statements, Whitespace, and Scope . 27 8.3 Basics of Using Functions . 29 8.4 The Standard Library . 30 9 Compiling 31 9.1 Preprocessor . 31 9.2 Syntax Checking . 32 9.3 Object Code . 32 9.4 Linking . 32 9.5 Automation . 33 10 Structure and style 35 10.1 C Structure and Style . 35 10.2 Introduction . 35 10.3 Line Breaks and Indentation . 36 10.4 Comments . 38 10.5 Links . 41 11 Error handling 43 11.1 Preventing divide by zero errors . 44 11.2 Signals . 44 11.3 setjmp . 45 III Contents 12 Variables 47 12.1 Declaring, Initializing, and Assigning Variables . 47 12.2 Literals . 49 12.3 The Four Basic Data Types . 49 12.4 sizeof ...................................... 51 12.5 Data type modifiers . 52 12.6 const qualifier . 52 12.7 Magic numbers . 53 12.8 Scope . 54 12.9 Other Modifiers . 54 13 Simple Input and Output 59 13.1 Output using printf() . 59 13.2 Other output methods . 61 13.3 Input using scanf() . 62 13.4 Links . 63 14 Simple math 65 14.1 Operators and Assignments . 65 15 Further math 73 15.1 Trigonometric functions . 73 15.2 Hyperbolic functions . 74 15.3 Exponential and logarithmic functions . 75 15.4 Power functions . 77 15.5 Nearest integer, absolute value, and remainder functions . 78 15.6 Error and gamma functions . 80 15.7 Further reading . 81 16 Control 83 16.1 Conditionals . 83 16.2 Loops . 90 16.3 One last thing: goto . 94 16.4 Examples . 96 16.5 Further reading . 96 17 Procedures and functions 97 17.1 More on functions . 98 17.2 Writing functions in C . 98 17.3 Using C functions . 101 17.4 Functions from the C Standard Library . 101 17.5 Variable-length argument lists . 106 18 Preprocessor 109 18.1 Directives . 109 18.2 Useful Preprocessor Macros for Debugging . 118 19 Libraries 125 19.1 What to put in header files . 127 IV Contents 19.2 Further reading . 128 20 Standard libraries 129 20.1 History . 129 20.2 Design . 130 20.3 ANSI Standard . 130 20.4 Common support libraries . 132 20.5 Compiler built-in functions . 133 20.6 POSIX standard library . 133 21 File IO 135 21.1 Introduction . 135 21.2 Streams . 135 21.3 Standard Streams . 136 21.4 FILE pointers . 137 21.5 Opening and Closing Files . 137 21.6 Other file access functions . 138 21.7 Functions that Modify the File Position Indicator . 139 21.8 Error Handling Functions . 141 21.9 Other Operations on Files . 142 21.10 Reading from Files . 143 21.11 Writing to Files . 151 21.12 References . 159 22 Beginning exercises 161 22.1 Variables . 161 22.2 Simple I/O . 163 22.3 Program Flow . 165 22.4 Functions . 166 22.5 Math . 166 22.6 Recursion . 167 23 In-depth C ideas 179 24 Arrays 181 24.1 Arrays . 181 24.2 Strings . 183 25 Pointers and arrays 185 25.1 Declaring pointers . 186 25.2 Assigning values to pointers . 187 25.3 Pointer dereferencing . 188 25.4 Pointers and Arrays . 189 25.5 Pointers in Function Arguments . 191 25.6 Pointers and Text Strings . 192 25.7 Pointers to Functions . 192 25.8 Practical use of function pointers in C . 194 25.9 Examples of pointer constructs . 196 25.10 sizeof . ..