0% found this document useful (0 votes)
26 views40 pages

Beej S Guide To C Programming Brian Beej Jorgensen Hall PDF Download

Beej's Guide to C Programming by Brian Beej Jorgensen Hall is a comprehensive resource for learning C programming, covering topics from basic syntax to advanced concepts like pointers and functions. The document includes detailed sections on variables, flow control, and compilation processes, making it suitable for both beginners and experienced programmers. It also provides links for downloading the book and related resources.

Uploaded by

zakiulduban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views40 pages

Beej S Guide To C Programming Brian Beej Jorgensen Hall PDF Download

Beej's Guide to C Programming by Brian Beej Jorgensen Hall is a comprehensive resource for learning C programming, covering topics from basic syntax to advanced concepts like pointers and functions. The document includes detailed sections on variables, flow control, and compilation processes, making it suitable for both beginners and experienced programmers. It also provides links for downloading the book and related resources.

Uploaded by

zakiulduban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Beej s Guide to C Programming Brian Beej

Jorgensen Hall pdf download

https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Beej s Guide to C Programming Brian Beej Jorgensen Hall

https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall-5/

Beej s Guide to C Programming Brian Beej Jorgensen Hall

https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall-6/

Beej s Guide to C Programming Brian Beej Jorgensen Hall

https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall/

Deathly Delinquency 1st Edition Eva Chase

https://ebookmeta.com/product/deathly-delinquency-1st-edition-
eva-chase/
The Last Hard Boy The Hard Boys 3 1st Edition Sam
Crescent

https://ebookmeta.com/product/the-last-hard-boy-the-hard-
boys-3-1st-edition-sam-crescent/

Making and Breaking the Grid A Graphic Design Layout


Workshop Second Edition Timothy Samara

https://ebookmeta.com/product/making-and-breaking-the-grid-a-
graphic-design-layout-workshop-second-edition-timothy-samara/

Proficiency Based Grading in the Content Areas Insights


and Key Questions for Secondary Schools Adapting
Evidence Based Grading for Content Area Teachers 1st
Edition Wendy Custable
https://ebookmeta.com/product/proficiency-based-grading-in-the-
content-areas-insights-and-key-questions-for-secondary-schools-
adapting-evidence-based-grading-for-content-area-teachers-1st-
edition-wendy-custable/

Sustainable Design: HCI, Usability and Environmental


Concerns 2nd Edition Tomayess Issa

https://ebookmeta.com/product/sustainable-design-hci-usability-
and-environmental-concerns-2nd-edition-tomayess-issa/

Security Observability with eBPF 1st Edition Jed


Salazar

https://ebookmeta.com/product/security-observability-with-
ebpf-1st-edition-jed-salazar/
One Last Smile 1st Edition Gs Rhodes

https://ebookmeta.com/product/one-last-smile-1st-edition-gs-
rhodes/
Beej’s Guide to C Programming

Brian “Beej Jorgensen” Hall

v0.9.13, Copyright © March 29, 2023


Contents

1 Foreword 1
1.1 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Platform and Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Official Homepage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Email Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.6 Mirroring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Note for Translators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 Copyright and Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.9 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Hello, World! 5
2.1 What to Expect from C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Compilation Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Building with gcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Building with clang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Building from IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 C Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Variables and Statements 11


3.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.1 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.5 Conditional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.6 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.7 The sizeof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.3 Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3.1 The if-else statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.3 The do-while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.4 The for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.5 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

4 Functions 25
4.1 Passing by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.2 Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3 Empty Parameter Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

i
CONTENTS ii

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.5 The NULL Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.6 A Note on Declaring Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.7 sizeof and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

6 Arrays 36
6.1 Easy Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.2 Getting the Length of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.3 Array Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
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 . . . . . . . . . . . . . . . . . . . 41
6.6.3 Changing Arrays in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.6.4 Passing Multidimensional Arrays to Functions . . . . . . . . . . . . . . . . . . . 43

7 Strings 45
7.1 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 50
8.1 Declaring a Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.2 Struct Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.3 Passing Structs to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.4 The Arrow Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
8.5 Copying and Returning structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
8.6 Comparing structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

9 File Input/Output 54
9.1 The FILE* Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
9.2 Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
9.3 End of File: EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
9.3.1 Reading a Line at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
9.4 Formatted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.5 Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
9.6 Binary File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
9.6.1 struct and Number Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

10 typedef: Making New Types 62


10.1 typedef in Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.1.1 Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.2 typedef in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.2.1 typedef and structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.2.2 typedef and Other Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.2.3 typedef and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
CONTENTS iii

10.2.4 typedef and Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64


10.3 Arrays and typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

11 Pointers II: Arithmetic 66


11.1 Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.1.1 Adding to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.1.2 Changing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
11.1.3 Subtracting Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
11.2 Array/Pointer Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
11.2.1 Array/Pointer Equivalence in Function Calls . . . . . . . . . . . . . . . . . . . . 70
11.3 void Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
12.6 Aligned Allocations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

13 Scope 84
13.1 Block Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
13.1.1 Where To Define Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
13.1.2 Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
13.2 File Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
13.3 for-loop Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.4 A Note on Function Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

14 Types II: Way More Types! 88


14.1 Signed and Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
14.2 Character Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
14.3 More Integer Types: short, long, long long . . . . . . . . . . . . . . . . . . . . . . 90
14.4 More Float: double and long double . . . . . . . . . . . . . . . . . . . . . . . . . 92
14.4.1 How Many Decimal Digits? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
14.4.2 Converting to Decimal and Back . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.5 Constant Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5.1 Hexadecimal and Octal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5.2 Integer Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5.3 Floating Point Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

15 Types III: Conversions 100


15.1 String Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
15.1.1 Numeric Value to String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
15.1.2 String to Numeric Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15.2 char Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
15.3 Numeric Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
15.3.1 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
15.3.2 Integer to Integer Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
15.3.3 Integer and Floating Point Conversions . . . . . . . . . . . . . . . . . . . . . . 105
15.4 Implicit Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
15.4.1 The Integer Promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
15.4.2 The Usual Arithmetic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 105
15.4.3 void* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
CONTENTS iv

15.5 Explicit Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106


15.5.1 Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

16 Types IV: Qualifiers and Specifiers 108


16.1 Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
16.1.1 const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
16.1.2 restrict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
16.1.3 volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
16.1.4 _Atomic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
16.2 Storage-Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
16.2.1 auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
16.2.2 static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
16.2.3 extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
16.2.4 register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
16.2.5 _Thread_local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

17 Multifile Projects 116


17.1 Includes and Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
17.2 Dealing with Repeated Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
17.3 static and extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
17.4 Compiling with Object Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

18 The Outside Environment 120


18.1 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
18.1.1 The Last argv is NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
18.1.2 The Alternate: char **argv . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
18.1.3 Fun Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
18.2 Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
18.2.1 Other Exit Status Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
18.3 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
18.3.1 Setting Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
18.3.2 Unix-like Alternative Environment Variables . . . . . . . . . . . . . . . . . . . . . 127

19 The C Preprocessor 129


19.1 #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
19.2 Simple Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
19.3 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
19.3.1 If Defined, #ifdef and #endif . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
19.3.2 If Not Defined, #ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
19.3.3 #else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
19.3.4 General Conditional: #if, #elif . . . . . . . . . . . . . . . . . . . . . . . . . 132
19.3.5 Losing a Macro: #undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
19.4 Built-in Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
19.4.1 Mandatory Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
19.4.2 Optional Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
19.5 Macros with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
19.5.1 Macros with One Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
19.5.2 Macros with More than One Argument . . . . . . . . . . . . . . . . . . . . . . . . 137
19.5.3 Macros with Variable Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 138
19.5.4 Stringification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
19.5.5 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
19.6 Multiline Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
19.7 Example: An Assert Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
19.8 The #error Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
19.9 The #pragma Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CONTENTS v

19.9.1 Non-Standard Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142


19.9.2 Standard Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
19.9.3 _Pragma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
19.10 The #line Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
19.11 The Null Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

20 structs II: More Fun with structs 145


20.1 Initializers of Nested structs and Arrays . . . . . . . . . . . . . . . . . . . . . . . . 145
20.2 Anonymous structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
20.3 Self-Referential structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
20.4 Flexible Array Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
20.5 Padding Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
20.6 offsetof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
20.7 Fake OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
20.8 Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
20.8.1 Non-Adjacent Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
20.8.2 Signed or Unsigned ints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
20.8.3 Unnamed Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
20.8.4 Zero-Width Unnamed Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
20.9 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
20.9.1 Unions and Type Punning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
20.9.2 Pointers to unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
20.9.3 Common Initial Sequences in Unions . . . . . . . . . . . . . . . . . . . . . . . . 157
20.10 Unions and Unnamed Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
20.11 Passing and Returning structs and unions . . . . . . . . . . . . . . . . . . . . . . . 160

21 Characters and Strings II 162


21.1 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
21.1.1 Frequently-used Escapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
21.1.2 Rarely-used Escapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
21.1.3 Numeric Escapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

22 Enumerated Types: enum 166


22.1 Behavior of enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
22.1.1 Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
22.1.2 Trailing Commas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
22.1.3 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
22.1.4 Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
22.2 Your enum is a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

23 Pointers III: Pointers to Pointers and More 170


23.1 Pointers to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
23.1.1 Pointer Pointers and const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
23.2 Multibyte Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
23.3 The NULL Pointer and Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
23.4 Pointers as Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
23.5 Casting Pointers to other Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
23.6 Pointer Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
23.7 Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

24 Bitwise Operations 181


24.1 Bitwise AND, OR, XOR, and NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
24.2 Bitwise Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

25 Variadic Functions 183


CONTENTS vi

25.1 Ellipses in Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183


25.2 Getting the Extra Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
25.3 va_list Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
25.4 Library Functions That Use va_lists . . . . . . . . . . . . . . . . . . . . . . . . . . 186

26 Locale and Internationalization 187


26.1 Setting the Localization, Quick and Dirty . . . . . . . . . . . . . . . . . . . . . . . . . . 187
26.2 Getting the Monetary Locale Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
26.2.1 Monetary Digit Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
26.2.2 Separators and Sign Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
26.2.3 Example Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
26.3 Localization Specifics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

27 Unicode, Wide Characters, and All That 192


27.1 What is Unicode? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
27.2 Code Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
27.3 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
27.4 Source and Execution Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
27.5 Unicode in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
27.6 A Quick Note on UTF-8 Before We Swerve into the Weeds . . . . . . . . . . . . . . . 196
27.7 Different Character Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
27.7.1 Multibyte Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
27.7.2 Wide Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
27.8 Using Wide Characters and wchar_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
27.8.1 Multibyte to wchar_t Conversions . . . . . . . . . . . . . . . . . . . . . . . . . 198
27.9 Wide Character Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
27.9.1 wint_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
27.9.2 I/O Stream Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
27.9.3 I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
27.9.4 Type Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
27.9.5 String and Memory Copying Functions . . . . . . . . . . . . . . . . . . . . . . 200
27.9.6 String and Memory Comparing Functions . . . . . . . . . . . . . . . . . . . . . . 201
27.9.7 String Searching Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
27.9.8 Length/Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
27.9.9 Character Classification Functions . . . . . . . . . . . . . . . . . . . . . . . . . 202
27.10 Parse State, Restartable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
27.11 Unicode Encodings and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
27.11.1 UTF-8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
27.11.2 UTF-16, UTF-32, char16_t, and char32_t . . . . . . . . . . . . . . . . . . . . 205
27.11.3 Multibyte Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
27.11.4 Third-Party Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

28 Exiting a Program 207


28.1 Normal Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
28.1.1 Returning From main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
28.1.2 exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
28.1.3 Setting Up Exit Handlers with atexit() . . . . . . . . . . . . . . . . . . . . . 208
28.2 Quicker Exits with quick_exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
28.3 Nuke it from Orbit: _Exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
28.4 Exiting Sometimes: assert() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
28.5 Abnormal Exit: abort() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

29 Signal Handling 211


29.1 What Are Signals? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
29.2 Handling Signals with signal() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
CONTENTS vii

29.3 Writing Signal Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212


29.4 What Can We Actually Do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
29.5 Friends Don’t Let Friends signal() . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

30 Variable-Length Arrays (VLAs) 217


30.1 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
30.2 sizeof and VLAs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
30.3 Multidimensional VLAs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
30.4 Passing One-Dimensional VLAs to Functions . . . . . . . . . . . . . . . . . . . . . . 219
30.5 Passing Multi-Dimensional VLAs to Functions . . . . . . . . . . . . . . . . . . . . . . 220
30.5.1 Partial Multidimensional VLAs . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
30.6 Compatibility with Regular Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
30.7 typedef and VLAs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
30.8 Jumping Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
30.9 General Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

31 goto 224
31.1 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
31.2 Labeled continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
31.3 Bailing Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
31.4 Labeled break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
31.5 Multi-level Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
31.6 Tail Call Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
31.7 Restarting Interrupted System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
31.8 goto and Thread Preemption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
31.9 goto and Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
31.10 goto and Variable-Length Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

32 Types Part V: Compound Literals and Generic Selections 233


32.1 Compound Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
32.1.1 Passing Unnamed Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . 234
32.1.2 Unnamed structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
32.1.3 Pointers to Unnamed Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
32.1.4 Unnamed Objects and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
32.1.5 Silly Unnamed Object Example . . . . . . . . . . . . . . . . . . . . . . . . . . 236
32.2 Generic Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

33 Arrays Part II 240


33.1 Type Qualifiers for Arrays in Parameter Lists . . . . . . . . . . . . . . . . . . . . . . . 240
33.2 static for Arrays in Parameter Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
33.3 Equivalent Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

34 Long Jumps with setjmp, longjmp 244


34.1 Using setjmp and longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
34.2 Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
34.2.1 The Values of Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
34.2.2 How Much State is Saved? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
34.2.3 You Can’t Name Anything setjmp . . . . . . . . . . . . . . . . . . . . . . . . . 246
34.2.4 You Can’t setjmp() in a Larger Expression . . . . . . . . . . . . . . . . . . . . 246
34.2.5 When Can’t You longjmp()? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
34.2.6 You Can’t Pass 0 to longjmp() . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
34.2.7 longjmp() and Variable Length Arrays . . . . . . . . . . . . . . . . . . . . . . . 247

35 Incomplete Types 248


35.1 Use Case: Self-Referential Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
CONTENTS viii

35.2 Incomplete Type Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249


35.3 Other Incomplete Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
35.4 Use Case: Arrays in Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
35.5 Completing Incomplete Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

36 Complex Numbers 252


36.1 Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
36.2 Assigning Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
36.3 Constructing, Deconstructing, and Printing . . . . . . . . . . . . . . . . . . . . . . . . 253
36.4 Complex Arithmetic and Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
36.5 Complex Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
36.5.1 Trigonometry Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
36.5.2 Exponential and Logarithmic Functions . . . . . . . . . . . . . . . . . . . . . . 256
36.5.3 Power and Absolute Value Functions . . . . . . . . . . . . . . . . . . . . . . . . 256
36.5.4 Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

37 Fixed Width Integer Types 257


37.1 The Bit-Sized Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
37.2 Maximum Integer Size Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
37.3 Using Fixed Size Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
37.4 Limits of Fixed Size Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
37.5 Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

38 Date and Time Functionality 261


38.1 Quick Terminology and Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
38.2 Date Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
38.3 Initialization and Conversion Between Types . . . . . . . . . . . . . . . . . . . . . . . 262
38.3.1 Converting time_t to struct tm . . . . . . . . . . . . . . . . . . . . . . . . . 263
38.3.2 Converting struct tm to time_t . . . . . . . . . . . . . . . . . . . . . . . . . 263
38.4 Formatted Date Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
38.5 More Resolution with timespec_get() . . . . . . . . . . . . . . . . . . . . . . . . . 265
38.6 Differences Between Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

39 Multithreading 267
39.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
39.2 Things You Can Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.3 Data Races and the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.4 Creating and Waiting for Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
39.5 Detaching Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
39.6 Thread Local Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
39.6.1 _Thread_local Storage-Class . . . . . . . . . . . . . . . . . . . . . . . . . . 275
39.6.2 Another Option: Thread-Specific Storage . . . . . . . . . . . . . . . . . . . . . 275
39.7 Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
39.7.1 Different Mutex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
39.8 Condition Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
39.8.1 Timed Condition Wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
39.8.2 Broadcast: Wake Up All Waiting Threads . . . . . . . . . . . . . . . . . . . . . 285
39.9 Running a Function One Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

40 Atomics 286
40.1 Testing for Atomic Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
40.2 Atomic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
40.3 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
40.4 Acquire and Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
40.5 Sequential Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
CONTENTS ix

40.6 Atomic Assignments and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292


40.7 Library Functions that Automatically Synchronize . . . . . . . . . . . . . . . . . . . . 293
40.8 Atomic Type Specifier, Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
40.9 Lock-Free Atomic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
40.9.1 Signal Handlers and Lock-Free Atomics . . . . . . . . . . . . . . . . . . . . . . 296
40.10 Atomic Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
40.11 Atomic structs and unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
40.12 Atomic Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
40.13 Memory Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
40.13.1 Sequential Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.13.2 Acquire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.13.3 Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.13.4 Consume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.13.5 Acquire/Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.13.6 Relaxed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40.14 Fences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
40.15 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

41 Function Specifiers, Alignment Specifiers/Operators 302


41.1 Function Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
41.1.1 inline for Speed—Maybe . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
41.1.2 noreturn and _Noreturn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
41.2 Alignment Specifiers and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
41.2.1 alignas and _Alignas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
41.2.2 alignof and _Alignof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Chapter 1

Foreword

C is not a big language, and it is not well served by a big book.


–Brian W. Kernighan, Dennis M. Ritchie
No point in wasting words here, folks, let’s jump straight into the C code:
E((ck?main((z?(stat(M,&t)?P+=a+'{'?0:3:
execv(M,k),a=G,i=P,y=G&255,
sprintf(Q,y/'@'-3?A(*L(V(%d+%d)+%d,0)

And they lived happily ever after. The End.


What’s this? You say something’s still not clear about this whole C programming language thing?
Well, to be quite honest, I’m not even sure what the above code does. It’s a snippet from one of the entries in
the 2001 International Obfuscated C Code Contest1 , a wonderful competition wherein the entrants attempt
to write the most unreadable C code possible, with often surprising results.
The bad news is that if you’re a beginner in this whole thing, all C code you see probably looks obfuscated!
The good news is, it’s not going to be that way for long.
What we’ll try to do over the course of this guide is lead you from complete and utter sheer lost confusion
on to the sort of enlightened bliss that can only be obtained through pure C programming. Right on.
In the old days, C was a simpler language. A good number of the features contained in this book and a lot
of the features in the Library Reference volume didn’t exist when K&R wrote the famous second edition of
their book in 1988. Nevertheless, the language remains small at its core, and I hope I’ve presented it here in
a way that starts with that simple core and builds outward.
And that’s my excuse for writing such a hilariously large book for such a small, concise language.

1.1 Audience
This guide assumes that you’ve already got some programming knowledge under your belt from another
language, such as Python2 , JavaScript3 , Java4 , Rust5 , Go6 , Swift7 , etc. (Objective-C8 devs will have a par-
1
https://www.ioccc.org/
2
https://en.wikipedia.org/wiki/Python_(programming_language)
3
https://en.wikipedia.org/wiki/JavaScript
4
https://en.wikipedia.org/wiki/Java_(programming_language)
5
https://en.wikipedia.org/wiki/Rust_(programming_language)
6
https://en.wikipedia.org/wiki/Go_(programming_language)
7
https://en.wikipedia.org/wiki/Swift_(programming_language)
8
https://en.wikipedia.org/wiki/Objective-C

1
Chapter 1. Foreword 2

ticularly easy time of it!)


We’re going to assume you know what variables are, what loops do, how functions work, and so on.
If that’s not you for whatever reason the best I can hope to provide is some honest entertainment for your
reading pleasure. The only thing I can reasonably promise is that this guide won’t end on a cliffhanger… or
will it?

1.2 How to Read This Book


The guide is in two volumes, and this is the first: the tutorial volume!
The second volume is the library reference9 , and it’s far more reference than tutorial.
If you’re new, go through the tutorial part in order, generally. The higher you get in chapters, the less
important it is to go in order.
And no matter your skill level, the reference part is there with complete examples of the standard library
function calls to help refresh your memory whenever needed. Good for reading over a bowl of cereal or
other time.
Finally, glancing at the index (if you’re reading the print version), the reference section entries are italicized.

1.3 Platform and Compiler


I’ll try to stick to Plain Ol’-Fashioned ISO-standard C10 . Well, for the most part. Here and there I might go
crazy and start talking about POSIX11 or something, but we’ll see.
Unix users (e.g. Linux, BSD, etc.) try running cc or gcc from the command line–you might already have a
compiler installed. If you don’t, search your distribution for installing gcc or clang.
Windows users should check out Visual Studio Community12 . Or, if you’re looking for a more Unix-like
experience (recommended!), install WSL13 and gcc.
Mac users will want to install XCode14 , and in particular the command line tools.
There are a lot of compilers out there, and virtually all of them will work for this book. And a C++ compiler
will compile a lot of (but not all!) C code. Best use a proper C compiler if you can.

1.4 Official Homepage


This official location of this document is https://beej.us/guide/bgc/15 . Maybe this’ll change in the future, but
it’s more likely that all the other guides are migrated off Chico State computers.

1.5 Email Policy


I’m generally available to help out with email questions so feel free to write in, but I can’t guarantee a
response. I lead a pretty busy life and there are times when I just can’t answer a question you have. When
that’s the case, I usually just delete the message. It’s nothing personal; I just won’t ever have the time to give
the detailed answer you require.
9
https://beej.us/guide/bgclr/
10
https://en.wikipedia.org/wiki/ANSI_C
11
https://en.wikipedia.org/wiki/POSIX
12
https://visualstudio.microsoft.com/vs/community/
13
https://docs.microsoft.com/en-us/windows/wsl/install-win10
14
https://developer.apple.com/xcode/
15
https://beej.us/guide/bgc/
Chapter 1. Foreword 3

As a rule, the more complex the question, the less likely I am to respond. If you can narrow down your
question before mailing it and be sure to include any pertinent information (like platform, compiler, error
messages you’re getting, and anything else you think might help me troubleshoot), you’re much more likely
to get a response.
If you don’t get a response, hack on it some more, try to find the answer, and if it’s still elusive, then write
me again with the information you’ve found and hopefully it will be enough for me to help out.
Now that I’ve badgered you about how to write and not write me, I’d just like to let you know that I fully
appreciate all the praise the guide has received over the years. It’s a real morale boost, and it gladdens me to
hear that it is being used for good! :-) Thank you!

1.6 Mirroring
You are more than welcome to mirror this site, whether publicly or privately. If you publicly mirror the site
and want me to link to it from the main page, drop me a line at [email protected].

1.7 Note for Translators


If you want to translate the guide into another language, write me at [email protected] and I’ll link to your
translation from the main page. Feel free to add your name and contact info to the translation.
Please note the license restrictions in the Copyright and Distribution section, below.

1.8 Copyright and Distribution


Beej’s Guide to C is Copyright © 2021 Brian “Beej Jorgensen” Hall.
With specific exceptions for source code and translations, below, this work is licensed under the Creative
Commons Attribution-Noncommercial-No Derivative Works 3.0 License. To view a copy of this license,
visit https://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter to Creative Commons,
171 Second Street, Suite 300, San Francisco, California, 94105, USA.
One specific exception to the “No Derivative Works” portion of the license is as follows: this guide may
be freely translated into any language, provided the translation is accurate, and the guide is reprinted in its
entirety. The same license restrictions apply to the translation as to the original guide. The translation may
also include the name and contact information for the translator.
The C source code presented in this document is hereby granted to the public domain, and is completely free
of any license restriction.
Educators are freely encouraged to recommend or supply copies of this guide to their students.
Contact [email protected] for more information.

1.9 Dedication
The hardest things about writing these guides are:
• Learning the material in enough detail to be able to explain it
• Figuring out the best way to explain it clearly, a seemingly-endless iterative process
• Putting myself out there as a so-called authority, when really I’m just a regular human trying to make
sense of it all, just like everyone else
• Keeping at it when so many other things draw my attention
Chapter 1. Foreword 4

A lot of people have helped me through this process, and I want to acknowledge those who have made this
book possible.
• Everyone on the Internet who decided to help share their knowledge in one form or another. The free
sharing of instructive information is what makes the Internet the great place that it is.
• The volunteers at cppreference.com16 who provide the bridge that leads from the spec to the real world.
• The helpful and knowledgeable folks on comp.lang.c17 and r/C_Programming18 who got me through
the tougher parts of the language.
• Everyone who submitted corrections and pull-requests on everything from misleading instructions to
typos.
Thank you! ♥

16
https://en.cppreference.com/
17
https://groups.google.com/g/comp.lang.c
18
https://www.reddit.com/r/C_Programming/
Chapter 2

Hello, World!

2.1 What to Expect from C


“Where do these stairs go?”
“They go up.”
—Ray Stantz and Peter Venkman, Ghostbusters
C is a low-level language.
It didn’t use to be. Back in the day when people carved punch cards out of granite, C was an incredible way
to be free of the drudgery of lower-level languages like assembly1 .
But now in these modern times, current-generation languages offer all kinds of features that didn’t exist in
1972 when C was invented. This means C is a pretty basic language with not a lot of features. It can do
anything, but it can make you work for it.
So why would we even use it today?
• As a learning tool: not only is C a venerable piece of computing history, but it is connected to the bare
metal2 in a way that present-day languages are not. When you learn C, you learn about how software
interfaces with computer memory at a low level. There are no seatbelts. You’ll write software that
crashes, I assure you. And that’s all part of the fun!
• As a useful tool: C still is used for certain applications, such as building operating systems3 or in
embedded systems4 . (Though the Rust5 programming language is eyeing both these fields!)
If you’re familiar with another language, a lot of things about C are easy. C inspired many other languages,
and you’ll see bits of it in Go, Rust, Swift, Python, JavaScript, Java, and all kinds of other languages. Those
parts will be familiar.
The one thing about C that hangs people up is pointers. Virtually everything else is familiar, but pointers are
the weird one. The concept behind pointers is likely one you already know, but C forces you to be explicit
about it, using operators you’ve likely never seen before.
It’s especially insidious because once you grok6 pointers, they’re suddenly easy. But up until that moment,
they’re slippery eels.
1
https://en.wikipedia.org/wiki/Assembly_language
2
https://en.wikipedia.org/wiki/Bare_machine
3
https://en.wikipedia.org/wiki/Operating_system
4
https://en.wikipedia.org/wiki/Embedded_system
5
https://en.wikipedia.org/wiki/Rust_(programming_language)
6
https://en.wikipedia.org/wiki/Grok

5
Exploring the Variety of Random
Documents with Different Content
sensitive; but now and again he felt tempted to rush off with Priscilla
to some place where they could escape for ever from this horror of
publicity which was besetting them. He did not mind being made the
subject of leading articles, if it was his incapacity as an orator or his
ignorance of the political standpoint that was being assailed; but this
intrusion upon his private life was as distasteful to him as it would be
for anyone to see one’s dressing-room operations made the subject
of a cinematograph display.
How could he feel otherwise, when almost daily he could espy
strangers—men with knapsacks and women with veils (mostly
green), all of them carrying walking sticks—coming halfway up the
avenue and exchanging opinions as to the best point from which the
house could be snapshotted? Such strangers were no more
infrequent than the visits of men on motors—all sorts of motors,
from the obsolete tri-car to the 60 h.p. F.I.A.T. He was obliged to
give orders at the lodge gates that on no pretence was a motor to
be allowed to pass on to the avenue, and that bicycling strangers, as
well as pedestrians with kodaks, were also to be excluded. But in
spite of these orders, scarcely a day went by without bringing a
contingent of outsiders to the park; he believed that excursion trains
were run to Framsby solely to give the curious a chance of catching
a glimpse of the lady who figured as the heroine of “The Curious
Case” column of the great daily paper.
But as far as Framsby itself was concerned, it did not contribute
largely to the material of the nuisance. The truth was that the “sets”
of Framsby, who had for some days made the road to the Manor
suggest a picture of the retreat of the French from Moscow, owing to
their anxiety to leave cards upon the young couple, now stood
aghast at the information conveyed to them by the newspapers that
Mrs. Jack Wingfield was not really Mrs. Jack Wingfield. They stood
aghast, and held up their hands as if they were obeying the
imperative order of a highwayman rather than the righteous impulse
of outraged propriety. Some of them, who, through the strain put
upon the livery stables, had been compelled to postpone their visit
until a more convenient season, now affirmed that they had had
their doubts respecting the marriage all along. There was some
consultation among the “sets” as to the possibility of having their
visits cancelled, as now and again a presentation at Court was
cancelled. Would it not be possible to get back their cards? they
wondered. The baser sort had thoughts of sending in the livery
stables bill to Mr. Jack Wingfield.
But before a fortnight had passed it became plain to Jack and
Priscilla that they were not going to remain without sympathetic
visitors. Priscilla got a letter from Mrs. Bowlby-Sutherst—a vivacious
letter, and a delightfully worldly one into the bargain. The writer
stated her intention of coming to lunch at the Manor House the next
day, and of bringing a fire escape with her to allow of her getting in
by one of the windows if she were refused admission by the door.
And when she came and was admitted without the need for the
display of any ingenuity on her part, she proved a most amusing
visitor, showing no reticence whatever in regard to the “case,” and
ridiculing the claims of Marcus Blaydon to conjugal rights, after the
way he had behaved. Of course everyone with any sense
acknowledged, she affirmed, that the marriage was between Jack
and Priscilla.
When she had gone away Priscilla wondered if there was anything
in what she had said on this point; and Jack replied that he was
afraid that Mrs. Bowlby-Sutherst was too notorious as a patron of
notoriety for her opinion to have much weight. But as things turned
out, that was just where he was wrong, for within the week several
other ladies of considerable importance—county importance—called
at the Manor, and were admitted. These were people who owned
London houses and had a premonition that next season Mrs.
Wingfield—they were sure that she would be Mrs. Wingfield by then
—would be looked on as the most interesting figure in the world of
drawing-rooms; and Priscilla found them very nice indeed, referring
to her “case” as if it were one of the most amusing jests of the
autumn season. They showed no reluctance in talking about its
funniest features—its funniest features were just those which a rigid
disciplinarian would have called its most serious features—and they
promised faithfully that when she should appear in the court they
would be present to offer her their support—their moral support.
They seemed quite downhearted when she explained to them how it
was her hope that the arbitrament of the Divorce Division would be
avoided by a decree of a judge on the question of nullity. They had
quite set their hearts on the Divorce Court, and had in their eye a
toilet scheme which they felt sure would be in sympathy with the
entourage of that apartment, and to which they thought they might
be trusted to do justice.
But as the social position of these visitors was among the highest
in the county, Priscilla began to feel that there was no chance of her
becoming isolated even at the Manor House. The reasonableness of
her attitude appealed, she saw, to some reasonable people. She had
great hopes that it would appeal as well to one or more of His
Majesty’s judges when the time came.
And she was not neglected by her dear friend Rosa Cafifyn; but
this young woman came to her unaccompanied by her mother. The
Caffyn household was divided against itself on this vexed subject of
Priscilla’s attitude. Mrs. Caffyn, who had never encouraged her
daughter’s friendship for Priscilla Wadhurst, was aghast at the
publicity which her daughter’s friend had achieved.
“She was always getting herself talked about,” she remarked.
“First there was that affair with the prince; everyone was talking
about her speaking to him in French—in French, mind—for more
than an hour.” (Mrs. Caffyn seemed to have acquired the impression
that a conversation in French could scarcely fail to possess some of
the elements of the dialogue in a French vaudeville, and she had
heard enough about that form of composition to make her distrustful
of its improving qualities.) “And then,” she went on, “there came all
that horrid business about her marriage—the arrest of the man, you
know, and all that. The next thing was the trial, where her name
was mentioned in the hearing of all the common people—witnesses
and people of that class—in the court. Later on there was the heroic
drowning of the man, and then her marriage to Mr. Wingfield within
a few months, and the electioneering business—I really think that
she should have been more discreet than to get herself talked about
so frequently. As for her present escapade, I can only say that it
seems to me to be the crowning indiscretion of her life.”
But the Reverend Mr. Caffyn, who had been talking to his
patroness, Mrs. Bowlby-Sutherst, about Priscilla, was disposed to
take the view of an easy-going looker-on at the world and its ways
from a lesser altitude than that of his pulpit; and he smiled at
Priscilla’s resolution to remain at the Manor. He did not think that it
mattered much just then. Had she not married young Wingfield in
good faith, and had they not been going about together ever since?
he asked. He had in his mind, though his wife did not know it, the
saying of the wicked witty Frenchwoman who had accepted the
legend of the King’s making quite a promenade when deprived of his
head, on the plea that, after all “c’est le premier pas qui coûte.” And
so his daughter had no hesitation in paying her visit to the Manor.
It was when she was going through the gates that she recollected
how Priscilla had talked to her upon that morning long ago at this
same place. What had she said? Was it not that if she were to love a
man truly she would not allow any considerations of morality or any
other convention to keep her apart from him?
Rosa wondered if there really was anything in the theory which
was held by some people, to the effect that sometimes a judgment
followed hard upon the utterance of a thoughtless phrase. She
wondered if the publicity in which Priscilla was now moving had
been sent to her as a punishment for her impulsive words.
Perhaps it was the atmospheric envelope, so to speak, of this
thought which remained hanging about her in the house and
prevented her visit to her dear friend from being all that she
expected it to be. It was of course a delightful reunion; but
somehow Priscilla did not seem to be just the same as she had been
long ago.
With these variations of visitors and with plenty to occupy her
mind and her hands Priscilla found the weeks to go by rapidly
enough. She took care to be constantly occupied, by undertaking the
reorganization of the dairy in connection with the home farm, and
she had no difficulty in reviving Jack’s interest in the scheme for
introducing electric power for the lighting of the house and for the
lightening of labour in whatever department of the household labour
was employed. An expert on dynamos was summoned from
Manchester, and his opinion bore out all that Priscilla had said to
Jack on this interesting enterprise; and before a fortnight had
passed the details of the scheme had been decided on and estimates
were being prepared for the carrying out of the work.
In addition to her obvious duties Priscilla was making herself
indispensable to Jack’s mother in her long and tedious illness,
reading to her and sitting with her for hours every day. It was,
however, when Jack was alone with his mother one evening that she
laid her hand on his, saying:
“My dear boy, I had my fears at one time for the step you were
taking; but now I can only thank you with all my heart for having
given me a daughter after my own heart. I have, as you know,
always longed for a daughter, and my longing is now fulfilled with a
completeness that I never looked for. She is the best woman in the
world, Jack—the best woman for you.”
“I hope that I shall be able to make her as happy as she has made
me,” said he.
“Ah, that is the very point on which I wished to speak to you,” said
the mother. “I wonder if you have noticed—-if you have thought that
she is quite as happy as we could wish her to be. A shadow—no, not
quite so much as a shadow, but still something—have I been alone
in noticing it?—something like a shadow upon her now and then.”
Jack was slightly startled. He had taken good care that no
newspaper containing an allusion to the “curious case” which was
exciting the attention of all England and calling for immediate
attention on the other side of the Atlantic as well, should get into his
mother’s hands; but now that she was approaching convalescence,
he knew that however vigilant he might be in this respect, an
unlucky chance would make her aware of all that had happened
since the beginning of the attack that had prostrated her. He had
been living in dread of such a catastrophe all the previous week, and
now he perceived that it was imminent. Priscilla had not been able
to play her part so perfectly as to prevent the quick feeling—the
motherly apprehension—of the elder lady from suggesting
something to her.
“It would be the worst day of my life if any cloud were to come
over her path,” he said. “I hope that if anything of the sort were to
happen, it would only be a temporary thing—something that we
should look back upon, wondering that it should ever have disturbed
our peace.”
“What!” she cried. “You have noticed it?—there is something!—you
know what it is?”
“Oh, yes,” he said, with an affectation of carelessness “there has
been something—a trifle really—nothing of the nature of a difference
between Priscilla and myself, but——”
“I am glad you can assure me of that—that it is not the result of
any difference between you,” said she. “I know that the first few
months of married life are usually the most trying to both the man
and the woman; but you can assure me that it is not——”
“I can give you such an assurance,” he replied. “There has not
been so much as a suspicion of difference between us in thought
since she entered this house—in fact, since she became my wife.”
“What is the matter, then? May I not know, Jack? Don’t tell me if it
is anything which concerns Priscilla and you only.”
“Dearest mother, there is nothing that can concern us without
being a matter of concern to you. Still, this one thing—of course you
must know it; but what I am afraid of is that you will attach too
much importance to it—that you will not see how it may be easily
cleared away.”
“You will tell me all about it, Jack, and I promise you not to think
of it except in the way you say I should.”
“It really is quite a simple thing—five minutes should clear it away
for ever; and so far from its standing between Priscilla and myself, it
will, I am sure, only draw us closer to each other.”
He was not an adept in the art of “breaking it gently”; he had
never had need to practise it. He felt that this, his first attempt, was
but an indifferent success; he could see that so far from soothing
her, his preliminary ambling around the subject was exciting her. And
yet he feared to come out with a bare statement of the facts. He
was snipping the end off a cigar; somehow he was clumsy over the
operation; he could not understand why until he found that he was
trying to force into the chamfered cutter the wrong end.
And his mother was noticing his confusion and becoming unduly
excited.
Fortunately at this moment Priscilla entered the room—it was Mrs.
Wingfield’s boudoir, a pretty apartment for an invalid, the windows
overlooking a garden of roses. Never did Jack so welcome her
approach. The moment she passed the door she knew what was
before her.
“Oh, by the way, Priscilla,” said he, “you may as well tell mother
just now all there is to be told about this disagreeable business. I
have said that it is unlikely to take up more than a few minutes of
the judge’s time. You can best do it alone, I know.”
He bolted.
His mother smiled, and Priscilla laughed outright; it was so like a
man—each knew that that was just what the other was thinking
—“so like a man!”
The elder lady’s smile was still on her face when Priscilla said:
“There’s really very little to be told about this disagreeable affair;
but it must be faced. The fact is that we are applying to a judge to
have my first marriage—that shocking mockery of a marriage—
annulled, and everybody says that there will be no difficulty
whatever about it.”
“I don’t suppose that there should be any difficulty, my dear,” said
Mrs. Wingfield. “But what would be the good of it?”
“Something has happened which makes it absolutely necessary,”
replied Priscilla. “But it is really the case that what has happened will
make it very much easier for the judge. The wretch who, with a
charge of fraud hanging over him, did not hesitate to make the
attempt to involve me in his ruin, went straight from the gaol to
America.”
Mrs. Wingfield nodded.
“Don’t trouble yourself, dear,” she said. “I know all the story; it is
not all squalid; you must not forget that he died trying to save the
others.”
“That was his lie,” cried Priscilla. “He managed to get safely to the
shore and he turned up here trying to get money out of us to buy
him off. Jack showed him the door pretty quickly; so now you can
understand how necessary it is that we should have the marriage
nullified. A judge can do it in five minutes. Jack has been to Liscomb
and Liscomb, and they told him so.” (She was not now giving
evidence in a court of law.) “Oh, yes; they had the opinion of Sir
Edward upon it—five minutes! But in the meantime——”
“That’s it—in the meantime,” said Mrs. Wingfield slowly. She
seemed trying to think out some point of great difficulty which had
presented itself to her mind.
“In the meantime,” she repeated. “Am I right, Priscilla, in the
meantime you—you——”
“In the meantime, my dearest mother, if Jack were to die, and in
his will refer to me as his wife, the judge of the Probate Court would
decide that I should get whatever that will left to me. Is there
anyone who will say that I am not Jack’s wife? You will not say it,
and you are Jack’s mother.”
“I certainly will not say it, Priscilla; but still—there are some who
would say it, and—in the meantime—oh, it is terrible! my poor child;
it is no wonder that there was a shadow cast upon your life. What
you must have suffered—what you must still suffer! and how bravely
you bore your burden in front of me!”
Priscilla had flung herself on her knees beside the sofa, and put
her face down to the cushion on which the mother’s head was
resting; but her tears were not bitter, and her sobs were soft.
So she lay, her right arm about the shoulders of the other, for a
long time, in complete silence.
At last she raised her head from the cushion, and then bowed it
down to the pale face that was there until their tears mingled.
“I know what you are thinking, dearest,” she whispered. “You are
thinking that in the meantime I should not be in this house. Is not
that so? Oh, I knew that that was your thought; but it will not be
your thought when I tell you that....”
Her whisper dwindled away into nothing—it was not louder than
the breathing of a baby when asleep.
But the elder woman caught every word. She gave a little cry of
happiness, and held Jack’s wife close to her, kissing her again and
again.
“Dearest,” she said, “you are right; your place is here—here—in
the meantime.”
CHAPTER XXXIII

I
n spite of the very good case which Priscilla had made out for
herself to Jack’s mother, without deviating from strict accuracy
more widely than could easily be pardoned by even the severest
moralist, and in spite also of the still better case which was made
out for her by some of the contributors to that holiday page of the
newspapers, she felt that she had considerable cause for uneasiness
as the weeks went by and Messrs. Liscomb and Liscomb, having
returned from Scotland or Homburg, were busying themselves about
the nullity suit. Incidentally, they were concerned in two very dainty
divorce suits and three libel actions which they hoped to get on the
list before Christmas. They let Jack know that a defence had been
entered to the nullity suit by Mr. Marcus Blaydon, so that the
petitioner should not have a walk-over, whatever might happen; and
they urged on Mr. Wingfield the necessity for finding out whether all
that Captain Lyman knew would be in favour of Priscilla or of Marcus
Blaydon.
It was apparent that what Captain Lyman knew would be an
important factor in the case; but what he did know he had no
chance of revealing, for it seemed as if Captain Lyman was lost. His
name was in the registry of certificated mariners, but it was there as
the master of the barque Kingsdale, and the owners of that ill-fated
craft, on being communicated with by Messrs. Liscomb and Liscomb,
stated that he was no longer in their service, nor did they know
whose employment he had entered after the loss of his vessel.
During the whole of the month of August the solicitors had, through
their agents, been endeavouring to trace Lyman, but they had met
with no success. The barque Kingsdale had been owned in Quebec,
and he had been seen in that city in the month of June, but since
then his whereabouts had been vague; and the clerk who was ready
to rush off at a moment’s notice in search of him, and to fathom the
mystery of what he knew, began to feel that he stood a very good
chance of being deprived of his excursion.
Messrs. Liscomb and Liscomb were beginning to write rather grave
letters, They reminded Jack that they had absolutely no evidence to
show that Blaydon had gone away from the English gaol to meet
another woman than his wife; and as this was an important fact to
establish both in the nullity suit and the possible divorce suit, and as,
apparently, no one but Captain Lyman could give evidence on this
point—a question which had not yet been answered—they thought
no stone should be left unturned in order to find him and learn from
his own lips what it was that he knew, and how much of it he did
actually know, and whether his knowledge should take the form of
an affidavit, or be carefully suppressed.
As a matter of fact Messrs. Liscomb and Liscomb gave Mr.
Wingfield to understand that the success of their case would be
seriously jeopardized unless they could place some evidence before
the judge bearing upon the object of that trip made by Marcus
Blaydon across the Atlantic.
Jack did not question the accuracy of their opinion in this matter;
but what was he to do to provide them with the evidence they
required? It was all very well for them to write about the necessity
for leaving no stone unturned in order to find the extent of Captain
Lyman’s knowledge; but how could he, Jack Wingfield, travel
through the world during the next couple of months, turning over
stones to see if Captain Lyman was concealed beneath one of them?
He felt greatly disappointed, but he took good care that Priscilla
remained in ignorance of the purport of Messrs. Liscomb and
Liscomb’s letters, and every day made it harder for him to keep her
in this condition.
One afternoon he drove with her into Framsby, and their carriage
stopped at a shop almost exactly opposite to the Corn Exchange,
just when the frequenters of that institution were standing in groups
along the pavement on the one day of the week when the Exchange
was open. Business had been exceptionally good that day, and most
of the farmers and millers were in a good humour. As soon as the
rumour went round that the handsome lady in the carriage was the
daughter of Farmer Wadhurst who was “standing up for her rights”—
that was the precis that reached them of the “curious case” of the
newspaper page—they took off their hats and gave her a hearty
cheer.
This was not the first time that Priscilla had been so greeted in
Framsby; but such proofs of the position she occupied in the hearts
of the people, though gratifying, when considered from one
standpoint, did not throw the light that was needed upon the
question of what stone would, when turned, reveal the form of
Captain Lyman ready to make an affidavit that should have weight
with a judge. So while Priscilla drove home gratified by the kindly
spirit shown by her sympathisers, Jack could not help feeling that he
would gladly have exchanged it all for a single statement, made in
the presence of a commissioner for taking oaths, bearing out the
admission of Marcus Blaydon in regard to that woman on the other
side of the Atlantic.
Of course Priscilla quickly perceived that he was becoming uneasy,
and equally as a matter of course she found out the cause of the
uneasiness. He told her something of what Messrs. Liscomb and
Liscomb had communicated to him, though he did not go so far as
to let her know that they considered the absent evidence to be vital
to the success of the petition.
She took his explanation without saying more than a word or two.
“If Captain Lyman is not to be found we cannot have his evidence,
whether for us or against us,” she said. “And that being so, we shall
have to do our best without it. I have great faith in Sir Edward’s
power of cross-examining. If he puts that man in the witness-box he
should be able to get him to confess as much as he did to you.”
Jack did not tell her that Messrs. Liscomb and Liscomb had
explained to him that perhaps Marcus Blaydon might be prevented
from going into the box by his own advisers, who might think it
advisable to let the judge say whether or not she had succeeded in
establishing her petition when she had been examined before him. It
was well known that a very strong case indeed required to be made
out in favour of pronouncing a marriage null and void before a judge
would make such a pronouncement. So Messrs. Liscomb and
Liscomb had told him; but he kept this information to himself.
It was with that phrase about leaving no stone unturned ringing in
his mind, as if it were Messrs. Liscomb and Liscomb’s telephone bell,
that he sent off to the governor of the prison where Marcus Blaydon
had been incarcerated the postcard which contained upon its gummy
surface the imprint of the finger-tips of the man who had visited the
Manor claiming Priscilla as his wife. In spite of the absolute certainty
of Priscilla that he was Marcus Blaydon, Jack thought that there was
just a chance that he was an impostor. Even within his experience
there had been cases of men impersonating others with a view to
blackmail or to an inheritance. There was just a chance that this
man was not the real Marcus Blaydon, but a scoundrel of a slightly
different pattern.
He sent the card in a small box, enclosing with it a letter asking
the governor to be good enough to let him know if the finger-prints
that it bore were those of Marcus Blaydon, who had been
incarcerated in the prison for over a year.
With the lapse of only a few posts he received a communication
from the acting-governor of the prison stating that he had sent on
the card to the Criminal Investigation Department, and that the reply
had been that the prints were those of Marcus Blaydon.
He told Priscilla what he had done, and what was the result, and
she shook her head and smiled.
“It was very clever of you to get the finger-prints as you did,” she
said. “But I knew that I could not be mistaken in the man.”
“There was only the ghost of a chance that the man was an
impostor,” said Jack; “but I felt bound to leave no stone—oh, there’s
that phrase buzzing about me again!”
“You were quite right, dear Jack,” she said. “No stone should be
left unturned in digging the foundation for our case.”
Nothing further passed between them on this point; but two days
later Jack received a private letter from the governor of the prison,
stating that he had just resumed his duty after taking his annual
leave, and that he had seen the letter which his deputy had
answered.
“I can easily understand that you should be interested in an
enquiry of the nature of that suggested by your communication,” he
added; “and though the reply which was sent to you may not have
been just the one for which you hoped, yet I think it possible that it
may be in my power to give you some assistance in any
investigation you or your lawyers may be making in regard to Marcus
Blaydon. It would not be regular to do so by letter, but if you could
make it convenient to pay me a visit I might be able to place you in
possession of one or two interesting—perhaps they may even turn
out to be important—facts which came to my knowledge respecting
the man when he was in my charge.
“When I read in the English newspapers, which I received in
Switzerland, the particulars of the case in which Marcus Blaydon
played so sinister a part, I made up my mind to place myself in
communication with you; and I would have done so even if your
letters had not been put into my hands on my return.”
“It may mean a great deal or it may mean nothing,” remarked
Jack, passing this communication on to Priscilla.
Of course, Priscilla felt inclined, on a first reading of the note, to
attribute a great deal of importance to it. “Why should the prison
official take the trouble to write asking you to meet him if he was
not sure that what he had to say was vital?” she asked Jack. But a
second reading caused her to be less sanguine.
“It is just as you say it is: the man is guarded in his words; they
may mean a great deal or they may mean very little,” she said. “But
he is in an official position, and no doubt he has had experience of
curious cases and of everything that has a bearing upon them; and I
can’t think that he would have taken the trouble to write to you or to
ask you to visit him unless he had something important to tell you.”
“He says it may turn out to be important,” said Jack; “but just now
he thinks that it is only interesting. I am inclined to believe that it
will never get beyond that qualification. You see, if he himself had
thought that what he knew was vital to our interests he would have
telegraphed to us the moment the first newspapers came into his
hands.”
“Yes, that is so, I can see plainly; but anyhow, you’ll go, will you
not?” said Priscilla. She could see plainly that J ack was a little
annoyed because nothing had come of his cleverly-contrived trap in
obtaining the man’s finger-prints. He was not disposed to have any
extravagant hopes of important information coming from a quarter
that had failed him before. She knew that he was unreasonable; but
she also knew that it was quite natural for him to be affected as he
was by the failure of the authorities to say that the finger-prints
were those of some man other than Marcus Blaydon.
“Great Gloriana! Of course I shall go to see him, and you will
come with me,” cried Jack. “No matter what he has to say to us, I
feel that no stone——”
Priscilla clapt her hands upon her ears and rushed out of the
room.
The county gaol to which Marcus Blaydon had been committed
was a long way from Framsby. To reach it necessitated a journey to
London, and thence into the heart of the Midlands. Passing through
London they called upon Messrs. Liscomb and Liscomb to tell them
of their mission, and the junior partner, who was acquainted with
Major Crosbie, the governor of the prison, became greatly interested
in the letter which he had written to Jack—so interested, indeed,
that if the duty had not been laid upon him of receiving professional
visits from two most promising prospective co-respondents and
three defendants of newspaper libel actions, to say nothing of
sundry uncompromising plaintiffs, he would, he declared, accompany
his clients into the very presence of Major Crosbie.
“Whatever he may have to communicate, you may be sure that it
will have a bearing upon the case,” he said. “He will put you on the
track of evidence—real evidence—not merely what somebody said
that somebody told somebody else. You know where we are
deficient in this particular.”
“Yes,” said Jack quickly, being afraid that he might go on to
express himself strongly in Priscilla’s presence regarding the need for
evidence on the object of Blaydon’s trip across the Atlantic. “Yes, we
know pretty well how we stand. Any proof that Blaydon was a
blackguard will be received with gratitude.”
“That’s it,” said Mr. Liscomb.
“I thought Sir Edward’s cross-examination might be expected to
do great things for us in this way,” said Priscilla.
“It may do something, but not a great deal,” said Liscomb.
“Judges are fond of facts; they don’t care much about cross-
examinations, however brilliant the newspapers may call them. You
can easily see how the fellow, now that he has been put on his
guard by your hint that you mean to try to connect his voyage with a
woman, will be careful to have a story ready to account for all his
movements, and he has only to stick to it to pull through, however
Sir Edward may browbeat him. If you can bring the woman into
court we shall have him in the cart.”
That was all that Mr. Liscomb had to say to them, and they began
to feel that they might as well have gone on direct to the gaol
instead of calling upon him. And that was exactly what Mr. Liscomb
himself thought. The honour and glory of being associated with the
“curious case” were not inordinately estimated by him; the firm had
been so closely connected with such a number of other curious
cases ever since he had become a partner.
They found Major Crosbie waiting for them in a private room at
the governor’s house. As he was somewhat irregular in offering them
the information of which he was possessed, he was too strict a
disciplinarian to receive them in an official apartment. Within the
precincts of his private residence he felt himself at liberty to talk as
he pleased. A conscience capable of such reasonable differentiation
is most valuable in an official.
He waved aside in a graceful way Mr. Wingfield’s expression of
gratitude for the invitation to this interview.
“There is no need to say a word on this point, Mr. Wingfield,” he
said. “Your case is a most curious one.”
Jack confessed that he had heard it so described.
“A very curious one. It had been for nearly a week in the papers
before I had a chance of hearing anything about it; but when I
heard the name Marcus Blaydon I at once recollected some
particulars which had come under my notice officially in connection
with that man Blaydon. You are aware that it is part of my duty to
read not only those letters which the prisoners in my charge write to
persons outside, but also those which are received for themselves.
Now, Blaydon received while in this prison four letters, all of which
had been addressed to him at Prangborough, where, as you
doubtless know, he lived.”
Priscilla assented. Prangborough was the town in which her Aunt
Emily lived.
“They had been addressed to him at Prangborough, and from
there were forwarded to the prison. I find by reference to my official
diary that three of them came from apparently the same
correspondent and were posted at the same place—London in
Canada; they were signed ‘Lucy.’ The fourth was from a man,
evidently a captain in the merchant service, named Horace Lyman. It
had been posted at Sunderland, and was received by me a short
time before the expiration of the man’s term of imprisonment.”
“That is the letter which would be of importance to us if it told us
what is the present address of Captain Lyman,” said Jack.
Major Crosbie shook his head.
“You cannot expect a letter written nearly seven months ago to
state positively what is the writer’s address to-day,” said he with a
laugh. “But the contents of that letter made it clear that the writer
and his correspondent were not on the best terms; and that the
reason of this was the ill-treatment by Blaydon of the writer’s sister,
whose name was Lucy.”
“And the woman’s letters—did they make anything clear?”
“The woman’s three letters made a good deal clear. The one of
the earliest date suggested very clearly that she was the man’s wife.”
“What, Blaydon’s wife!” cried Jack. “That would be the best
possible news for us.”
“So it occurred to me,” said Major Crosbie. “If the man had been
married—as the letters suggested he was—some years before he
came under my notice—under our notice, I should say—and if his
wife was alive, as she must have been when those letters of hers
were written, the curious case becomes a very simple case indeed.”
“And the letters suggested marriage?” said Priscilla, interrogatively.
“They undoubtedly suggested marriage—at least, they would have
done so to someone with a smaller experience than I have had of
such correspondence. But from what I know I should say that to
assume that because a woman addresses a man as ‘My own
husband,’ she is that man’s lawful wife, would be a very unwise thing
to do. Such a form of address, I have learnt by experience, comes
quite naturally to the woman who is not married to the man but who
should be on the grounds of the most elementary morality. It is the
form used by the woman who has been deserted by the man, but
who hopes to get back to her former place in his affection. She
seems to think, poor thing, that if she assumes the title of wife
whenever she has the chance, she will in time come to feel that she
is his wife. I am not sure if you recognize the—the—what shall I call
it?—the naturalness of all this.”
He glanced first at Jack and then at Priscilla, and paused as if for
their acquiescence in his suggestion.
They acquiesced. Jack nodded and muttered “Quite so.” Priscilla
said:
“I am sure it is natural—it is quite plausible. But it might be
possible, might it not? to gather from the rest of the letters whether
the woman was trying to bring back a husband or a lover.”
“It is sometimes a good deal more difficult to do so than you could
imagine,” replied the Governor. “I used to think that I could
determine this point by the character of the letters; the most earnest
letters—those that were the most loving—the most full of
endearment—were written by the woman to her lover; the tamest—
the most formal, with a touch of nasty upbraiding, came from the
legal wife to her legal husband. That was the general principle on
which I drew my conclusions; but I soon found out how easy it was
to make a mistake by building on such foundations only. You see,
women differ so amazingly in temper and in temperament, leaving
education and ‘the complete letter-writer’ out of the question
altogether, that a wife who is not quite a wife may be carried away
by her feelings of the moment, and say something so bitter that you
could only believe it to come from a true wife, and the true wife may
be really in love with her husband, and ready to condone his lapses
without a word of reproach. That is how it is quite easy for one to
make a mistake in trying to differentiate on the basis of
correspondence only.”
“Quite so,” muttered Jack.
“I can quite believe that,” said Priscilla. “But about these particular
letters?”
She thought it quite as well to bring back Major Crosbie from his
consideration of the abstract to that of the concrete. She could see
that Jack was becoming slightly impatient at the somewhat cynical
expression of the Governor’s experiences.
“I was just returning to the letters written to Marcus Blaydon,” said
he. “It was necessary for me to state to you the difficulty which I
find in the way of coming to any legitimate conclusion on the point
which concerns you most, in order to prevent you from falling into
the mistake of believing that you are quite safe, when investigation
may prove that you have assumed too much.”
“Of course—quite right,” said Jack. “But you believe that the
woman was his wife?”
The Governor caressed his chin with a neat forefinger.
“I think, after going very carefully once more over the copy of the
letters, that there would be sufficient in any one of them to allow a
Grand Jury to bring in a true bill,” he replied.
Jack saw that the man described very neatly what was in his
mind. But Priscilla had never served on a Grand Jury. She required
further explanation.
“What I mean to say,” resumed Major Crosbie, “is that the letters
suggest a relationship which may prove on investigation to be a legal
union contracted three years ago in Canada. You observe how
cautious I am?”
“I do indeed,” replied Priscilla, and she did not acquiesce merely
out of politeness.
“I should be reluctant to say one word that might lead you to
expect too much,” said he. “My experience leads me to look for the
worst and not the best in men; but I should be reluctant to say that
the letters signed ‘Lucy’ did not come from a woman who was the
legal wife of Marcus Blaydon.”
“That is so much, at any rate,” said Jack; “and now if you can give
us any clue as to how it would be possible to be brought in touch
with Horace Lyman, we will be evermore indebted to you.”
“The woman is his sister—so much I gathered,” said the Governor.
“And I learned that he was waiting for Blaydon at the prison gate
when Blaydon was released. That is all I know. But the sister’s
address is, as I mentioned just now, London, in Canada—at least,
that was her address when she was in communication with Blaydon.
Her letters were not illiterate, though of course they were not
carefully written. They showed what critics would possibly call an ill-
balanced mind—extremes of blandishments on one page, and
threats of the wildest nature on the next. I can give you copies if
you would care to see them.”
Priscilla shook her head. She could not see herself sitting down to
read the confidential letters of the poor woman.
“I am quite willing to accept your judgment on them, Major
Crosbie,” she said.
“I think that you are right to do so,” said he. “If you were to, read
them they would certainly convey more to you, who have fortunately

You might also like