Beej's Guide to C Programming
Total Page:16
File Type:pdf, Size:1020Kb
Beej’s Guide to C Programming Brian “Beej Jorgensen” Hall v0.6.29, Copyright © September 26, 2021 Contents 1 Foreword 1 1.1 Audience .......................................... 1 1.2 Platform and Compiler .................................. 2 1.3 Official Homepage .................................... 2 1.4 Email Policy ....................................... 2 1.5 Mirroring ......................................... 2 1.6 Note for Translators ................................... 2 1.7 Copyright and Distribution ................................ 3 2 Hello, World! 4 2.1 What to Expect from C ................................... 4 2.2 Hello, World! ....................................... 5 2.3 Compilation Details .................................... 7 2.4 Building with gcc ..................................... 7 2.5 Building with clang .................................... 7 2.6 Building from IDEs .................................... 8 2.7 C Versions ........................................ 8 3 Variables and Statements 10 3.1 Variables ......................................... 10 3.1.1 Variable Names .................................. 10 3.1.2 Variable Types .................................... 11 3.1.3 Boolean Types ................................... 12 3.2 Operators and Expressions ................................ 13 3.2.1 Arithmetic ..................................... 13 3.2.2 Ternary Operator .................................. 13 3.2.3 Pre-and-Post Increment-and-Decrement ....................... 14 3.2.4 The Comma Operator ............................... 15 3.2.5 Conditional Operators ............................... 15 3.2.6 Boolean Operators ................................. 15 3.2.7 The sizeof Operator ............................... 16 3.3 Flow Control ........................................ 17 3.3.1 The if-else statement ............................... 18 3.3.2 The while statement ................................ 19 3.3.3 The do-while statement .............................. 19 3.3.4 The for statement ................................. 20 3.3.5 The switch Statement ................................ 21 4 Functions 24 4.1 Passing by Value ..................................... 25 4.2 Function Prototypes ................................... 26 4.3 Empty Parameter Lists ................................... 27 5 Pointers—Cower In Fear! 28 i CONTENTS ii 5.1 Memory and Variables .................................. 28 5.2 Pointer Types ....................................... 30 5.3 Dereferencing ....................................... 31 5.4 Passing Pointers as Arguments .............................. 32 5.5 The NULL Pointer ..................................... 33 5.6 A Note on Declaring Pointers ............................... 33 5.7 sizeof and Pointers .................................... 34 6 Arrays 35 6.1 Easy Example ...................................... 35 6.2 Getting the Length of an Array .............................. 36 6.3 Array Initializers ..................................... 36 6.4 Out of Bounds! ...................................... 38 6.5 Multidimensional Arrays ................................. 39 6.6 Arrays and Pointers .................................... 40 6.6.1 Getting a Pointer to an Array ............................ 40 6.6.2 Passing Single Dimensional Arrays to Functions . 40 6.6.3 Changing Arrays in Functions ............................ 41 6.6.4 Passing Multidimensional Arrays to Functions ................... 42 7 Strings 44 7.1 Constant Strings ...................................... 44 7.2 String Variables ....................................... 44 7.3 String Variables as Arrays ................................ 45 7.4 String Initializers ..................................... 45 7.5 Getting String Length ................................... 46 7.6 String Termination .................................... 46 7.7 Copying a String ...................................... 47 8 Structs 49 8.1 Declaring a Struct .................................... 49 8.2 Struct Initializers ..................................... 50 8.3 Passing Structs to Functions ............................... 50 8.4 The Arrow Operator .................................... 51 8.5 Copying and Returning structs ............................. 52 9 File Input/Output 53 9.1 The FILE* Data Type ................................... 53 9.2 Reading Text Files ..................................... 54 9.3 End of File: EOF ..................................... 55 9.3.1 Reading a Line at a Time .............................. 55 9.4 Formatted Input ..................................... 56 9.5 Writing Text Files ..................................... 57 9.6 Binary File I/O ....................................... 57 9.6.1 struct and Number Caveats ............................ 59 10 typedef: Making New Types 61 10.1 typedef in Theory ..................................... 61 10.1.1 Scoping ....................................... 61 10.2 typedef in Practice .................................... 61 10.2.1 typedef and structs ................................ 61 10.2.2 typedef and Other Types ............................. 63 10.2.3 typedef and Pointers ............................... 63 10.2.4 typedef and Capitalization ............................ 63 10.3 Arrays and typedef .................................... 64 CONTENTS iii 11 Pointers II: Arithmetic 65 11.1 Pointer Arithmetic .................................... 65 11.1.1 Adding to Pointers ................................. 65 11.1.2 Changing Pointers ................................. 66 11.1.3 Subtracting Pointers ................................. 67 11.2 Array/Pointer Equivalence ................................ 68 11.2.1 Array/Pointer Equivalence in Function Calls .................... 68 11.3 void Pointers ....................................... 69 12 Manual Memory Allocation 74 12.1 Allocating and Deallocating, malloc() and free() .................... 74 12.2 Error Checking ...................................... 75 12.3 Allocating Space for an Array .............................. 76 12.4 An Alternative: calloc() ................................ 76 12.5 Changing Allocated Size with realloc() ......................... 77 12.5.1 Reading in Lines of Arbitrary Length ....................... 78 12.5.2 realloc() with NULL ............................... 80 12.6 Aligned Allocations ................................... 80 13 Scope 82 13.1 Block Scope ....................................... 82 13.1.1 Where To Define Variables ............................. 82 13.1.2 Variable Hiding .................................. 83 13.2 File Scope ........................................ 83 13.3 for-loop Scope ....................................... 84 13.4 A Note on Function Scope ................................. 84 14 Types II: Way More Types! 85 14.1 Signed and Unsigned Integers .............................. 85 14.2 Character Types ..................................... 86 14.3 More Integer Types: short, long, long long ....................... 87 14.4 More Float: double and long double ......................... 89 14.4.1 How Many Decimal Digits? ............................ 90 14.4.2 Converting to Decimal and Back ........................... 91 14.5 Constant Numeric Types ................................. 92 14.5.1 Hexadecimal and Octal ............................... 93 14.5.2 Integer Constants .................................. 93 14.5.3 Floating Point Constants .............................. 95 15 Types III: Conversions 97 15.1 String Conversions ..................................... 97 15.1.1 Numeric Value to String ............................... 97 15.1.2 String to Numeric Value .............................. 98 15.2 Numeric Conversions ................................... 100 15.2.1 Boolean ...................................... 100 15.2.2 Integer to Integer Conversions . 100 15.2.3 Integer and Floating Point Conversions . 101 15.3 Implicit Conversions ....................................101 15.3.1 The Integer Promotions ...............................101 15.3.2 The Usual Arithmetic Conversions . 101 15.3.3 void* ....................................... 102 15.4 Explicit Conversions ................................... 102 15.4.1 Casting ....................................... 102 16 Types IV: Qualifiers and Specifiers 104 CONTENTS iv 16.1 Type Qualifiers .......................................104 16.1.1 const ........................................104 16.1.2 restrict ..................................... 106 16.1.3 volatile ......................................107 16.1.4 _Atomic .......................................107 16.2 Storage-Class Specifiers ..................................107 16.2.1 auto .........................................107 16.2.2 static ........................................107 16.2.3 extern ....................................... 108 16.2.4 register ..................................... 109 16.2.5 _Thread_local .................................. 110 17 Multifile Projects 111 17.1 Includes and Function Prototypes ..............................111 17.2 Dealing with Repeated Includes ............................. 113 17.3 static and extern ....................................114 17.4 Compiling with Object Files ................................114 18 The Outside Environment 115 18.1 Command Line Arguments ................................ 115 18.1.1 The Last argv is NULL ................................117 18.1.2 The Alternate: char **argv ............................117 18.1.3 Fun Facts .....................................