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