C Programming
Total Page:16
File Type:pdf, Size:1020Kb
C Programming en.wikibooks.org December 15, 2017 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 277. 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 257. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 281, 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 277. 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 Beginning C 17 6 Intro exercise 19 6.1 The ”Hello, World!”Program .......................... 19 6.2 References .................................... 21 7 Preliminaries 23 7.1 Block Structure, Statements, Whitespace, and Scope . 23 7.2 Basics of Using Functions ........................... 25 7.3 The Standard Library ............................. 26 7.4 References .................................... 26 8 Compiling 27 8.1 Preprocessor ................................... 27 8.2 Syntax Checking ................................ 28 8.3 Object Code ................................... 28 8.4 Linking ...................................... 28 8.5 Automation ................................... 29 8.6 References .................................... 30 9 Structure and style 31 9.1 C Structure and Style ............................. 31 9.2 Introduction ................................... 31 9.3 Line Breaks and Indentation .......................... 32 9.4 Comments .................................... 34 9.5 References .................................... 37 10 Variables 39 10.1 Declaring, Initializing, and Assigning Variables . 39 10.2 Literals ...................................... 41 10.3 The Four Basic Data Types .......................... 41 III Contents 10.4 sizeof ..................................... 43 10.5 Data type modifiers ............................... 44 10.6 const qualifier ................................. 45 10.7 Magic numbers ................................. 45 10.8 Scope ....................................... 46 10.9 Other Modifiers ................................. 46 10.10 References .................................... 50 11 Error handling 51 11.1 Preventing divide by zero errors ........................ 52 11.2 Signals ...................................... 52 11.3 setjmp ...................................... 53 12 Simple Input and Output 55 12.1 Output using printf() ............................ 55 12.2 Other output methods ............................. 58 12.3 Input using scanf() .............................. 58 12.4 References .................................... 59 13 Simple math 61 13.1 Arithmetic Operators .............................. 61 13.2 Assignment Operators ............................. 62 13.3 Logical Operators ................................ 62 13.4 Relational and Equality Operators ...................... 63 13.5 Type Casting .................................. 64 13.6 The Shift Operators (which may be used to rotate bits) . 64 13.7 Bitwise Operators ................................ 66 13.8 Comma Operator ................................ 66 13.9 References .................................... 67 14 Further math 69 14.1 Trigonometric functions ............................ 69 14.2 Hyperbolic functions .............................. 70 14.3 Exponential and logarithmic functions .................... 71 14.4 Power functions ................................. 73 14.5 Nearest integer, absolute value, and remainder functions . 74 14.6 Error and gamma functions .......................... 76 14.7 References .................................... 77 15 Control 79 15.1 Conditionals ................................... 79 15.2 Loops ...................................... 86 15.3 One last thing: goto .............................. 90 15.4 Examples .................................... 91 15.5 References .................................... 92 16 Procedures and functions 93 16.1 More on functions ................................ 94 16.2 Writing functions in C ............................. 94 IV Contents 16.3 Using C functions ................................ 96 16.4 Functions from the C Standard Library .................... 97 16.5 Variable-length argument lists ......................... 99 16.6 References .................................... 101 17 Preprocessor 103 17.1 Directives .................................... 103 17.2 Useful Preprocessor Macros for Debugging . 113 18 Libraries 119 18.1 What to put in header files . 121 18.2 References .................................... 122 19 Standard libraries 123 19.1 History ...................................... 123 19.2 Design ...................................... 124 19.3 ANSI Standard ................................. 124 19.4 Common support libraries . 126 19.5 Compiler built-in functions . 126 19.6 POSIX standard library ............................ 126 19.7 References .................................... 127 20 File IO 129 20.1 Introduction ................................... 129 20.2 Streams ..................................... 129 20.3 Standard Streams ................................ 130 20.4 FILE pointers .................................. 130 20.5 Opening and Closing Files . 131 20.6 Other file access functions . 132 20.7 Functions that Modify the File Position Indicator . 133 20.8 Error Handling Functions . 135 20.9 Other Operations on Files . 136 20.10 Reading from Files ............................... 137 20.11 Writing to Files ................................. 145 20.12 References .................................... 153 21 Beginning exercises 155 21.1 Variables ..................................... 155 21.2 Simple I/O ................................... 156 21.3 Program Flow .................................. 156 21.4 Functions .................................... 157 21.5 Math ....................................... 157 21.6 Recursion .................................... 157 22 In-depth C ideas 161 23 Arrays 163 23.1 Arrays ...................................... 163 23.2 Strings ...................................... 165 V Contents 23.3 References .................................... 166 24 Pointers and arrays 167 24.1 Declaring pointers ................................ 168 24.2 Assigning values to pointers . 168 24.3 Pointer dereferencing .............................. 169 24.4 Pointers and Arrays ............................... 170 24.5 Pointers in Function Arguments . 173 24.6 Pointers and Text Strings . 173 24.7 Pointers to Functions .............................. 174 24.8 Practical use of function pointers in C . 175 24.9 Examples of pointer constructs . 177 24.10 sizeof ....................................... 178 24.11 External Links ................................. 180 25 Memory management 181 25.1 The malloc function .............................. 181 25.2 The calloc function .............................. 183 25.3 The realloc function ............................. 183 25.4 The free function ............................... 183 25.5 References .................................... 185 26 Strings 187 26.1 Syntax ...................................... 187 26.2 The <string.h> Standard Header . 188 26.3 Examples .................................... 201 26.4 References .................................... 201 27 Complex types 203 27.1 Data structures ................................. 203 27.2 Enumerations .................................. 204 28 Networking in UNIX 207 28.1 A simple client ................................. 207 28.2 A simple server ................................. 209 28.3 Useful network functions ............................ 210 28.4 FAQs ....................................... 210 29 Common practices 211 29.1 Dynamic multidimensional arrays . 211 29.2 Constructors and destructors . 213 29.3 Nulling freed pointers .............................. 214 29.4 Macro conventions ............................... 214 29.5 Further reading ................................. 215 30 C and beyond 217 31 Particularities of C 219 31.1 References .................................... 221 VI Contents 32 Language Overloading and Extensions 223 32.1 External links .................................