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