The GNU C Programming Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Edition 4.1 The GNU C Programming Tutorial Mark Burgess Faculty of Engineering, Oslo College Ron Hale-Evans Copyright c 2002 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; there being no Invariant Section, with the Front-Cover Texts being \A GNU Manual", and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled \GNU Free Documentation License". (a) The FSF's Back-Cover Text is: \You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development." Function pointers i Table of Contents Preface ...................................... xi 1 Introduction............................... 1 1.1 The advantages of C..................................... 1 1.2 Questions for Chapter 1 ................................. 2 2 Using a compiler........................... 3 2.1 Basic ideas about C ..................................... 3 2.2 The compiler ........................................... 4 2.3 File names .............................................. 4 2.4 Errors .................................................. 5 2.4.1 Typographical errors ............................ 6 2.4.2 Type errors .................................... 6 2.5 Questions for Chapter 2 ................................. 6 3 The form of a C program................... 9 3.1 A word about style ..................................... 10 3.2 Comments ............................................. 10 3.3 Example 1 ............................................. 11 3.4 Questions for Chapter 3 ................................ 11 4 Functions ................................ 13 4.1 Function names ........................................ 13 4.2 Function examples ..................................... 14 4.3 Functions with values................................... 14 4.4 Function prototyping ................................... 15 4.5 The exit function...................................... 16 4.6 Questions for Chapter 4 ................................ 17 5 Variables and declarations................. 19 5.1 Integer variables ....................................... 19 5.1.1 The char type................................. 20 5.1.2 Floating point variables ........................ 20 5.2 Declarations ........................................... 21 5.3 Initialization ........................................... 22 5.4 The cast operator ...................................... 22 5.4.1 Cast operator demo............................ 23 5.5 Storage classes ......................................... 24 5.5.1 External variables ............................. 24 5.5.2 Static variables ................................ 25 5.5.3 Other storage classes........................... 25 5.6 Questions for Chapter 5 ................................ 25 ii 6 Scope .................................... 27 6.1 Global Variables ....................................... 27 6.2 Local Variables ........................................ 28 6.3 Communication via parameters.......................... 28 6.4 Scope example ......................................... 29 6.5 Questions for Chapter 6 ................................ 29 7 Expressions and operators................. 31 7.1 The assignment operator................................ 31 7.1.1 Important note about assignment ............... 32 7.2 Expressions and values ................................. 32 7.3 Expressions ............................................ 33 7.4 Parentheses and Priority ................................ 34 7.5 Unary Operator Precedence ............................. 34 7.6 Special Assignment Operators ++ and -- ................. 34 7.7 More Special Assignments .............................. 35 7.8 Comparisons and logic .................................. 37 7.9 Logical operators....................................... 38 7.9.1 Inclusive OR .................................. 38 7.10 Questions for Chapter 7 ............................... 39 8 Parameters ............................... 41 8.1 Parameters in function prototypes ....................... 42 8.2 Value Parameters ...................................... 42 8.3 Actual parameters and formal parameters ................ 43 8.4 Variadic functions ...................................... 45 8.5 Questions for Chapter 8 ................................ 45 9 Pointers.................................. 47 9.1 Pointer operators....................................... 47 9.2 Pointer types .......................................... 49 9.3 Pointers and initialization............................... 51 9.4 Variable parameters .................................... 51 9.4.1 Passing pointers correctly ...................... 52 9.4.2 Another variable parameter example ............ 53 9.5 Questions for Chapter 9 ................................ 54 10 Decisions................................ 55 10.1 if ................................................... 55 10.2 if... else........................................... 56 10.3 Nested if statements .................................. 57 10.4 The ?...:. operator ................................ 59 10.5 The switch statement ................................. 59 10.6 Example Listing ...................................... 60 10.7 Questions for Chapter 10 .............................. 62 Function pointers iii 11 Loops ................................... 63 11.1 while ................................................ 63 11.2 do...while........................................... 64 11.3 for .................................................. 65 11.4 The flexibility of for .................................. 67 11.5 Terminating and speeding loops ........................ 69 11.5.1 Terminating loops with break ................. 69 11.5.2 Terminating loops with return ................ 69 11.5.3 Speeding loops with continue ................. 70 11.6 Nested loops .......................................... 71 11.7 Questions for Chapter 11 .............................. 71 12 Preprocessor directives................... 73 12.1 A few directives ....................................... 73 12.2 Macros ............................................... 74 12.2.1 Macro functions .............................. 76 12.3 Extended macro example .............................. 77 12.4 Questions............................................. 80 13 Libraries ................................ 81 13.1 Header files ........................................... 81 13.2 Kinds of library ....................................... 83 13.3 Common library functions ............................. 84 13.3.1 Character handling ........................... 85 13.4 Mathematical functions ................................ 88 13.5 Questions for Chapter 13 .............................. 90 14 Arrays .................................. 91 14.1 Array bounds ......................................... 92 14.2 Arrays and for loops .................................. 92 14.3 Multidimensional arrays ............................... 96 14.4 Arrays and nested loops ............................... 98 14.5 Initializing arrays ..................................... 99 14.6 Arrays as Parameters................................. 101 14.7 Questions for Chapter 14 ............................. 102 15 Strings................................. 105 15.1 Conventions and declarations ......................... 105 15.2 Initializing strings .................................... 105 15.3 String arrays......................................... 106 15.4 String library functions ............................... 108 15.5 Questions for Chapter 15 ............................. 111 iv 16 Input and output ....................... 113 16.1 High-level file routines ................................ 115 16.1.1 Opening a file ............................... 115 16.1.2 Closing a file ................................ 116 16.1.3 Block input and output ...................... 117 16.1.4 File position ................................ 120 16.1.5 Stream buffering ............................ 121 16.1.6 End-of-file and error functions ................ 122 16.2 String output and input .............................. 123 16.2.1 Unformatted string output ................... 123 16.2.1.1 puts ............................... 123 16.2.1.2 fputs .............................. 124 16.2.2 Formatted string output ..................... 125 16.2.2.1 printf ............................. 125 16.2.2.2 Formatted output conversion specifiers ........................................ 126 16.2.3 fprintf .................................... 129 16.2.4 asprintf ................................... 130 16.2.5 Deprecated formatted string output functions.. 131 16.2.5.1 sprintf............................ 131 16.2.6 String input................................. 132 16.2.6.1 getline............................ 132 16.2.6.2 getdelim .......................... 133 16.2.7 Deprecated string input functions ............. 134 16.2.7.1 gets ............................... 134 16.2.7.2 fgets .............................. 135 16.2.8 Formatted string input....................... 136 16.2.8.1 sscanf ............................. 136 16.2.8.2 Formatted input conversion specifiers ........................................ 138 16.2.9 Deprecated formatted string input functions ... 140 16.2.9.1 scanf .............................. 140 16.2.9.2