Друзья, мы тут исследуем проблемы, с которыми сталкиваются пользователи электронных книг.
Будем вам благодарны за ваши ответы. Опрос займёт не более 5 минут.
Друзья, мы тут исследуем проблемы, с которыми сталкиваются пользователи электронных книг.
Будем вам благодарны за ваши ответы. Опрос займёт не более 5 минут.
The Prolog
C 11
Machine-Independent Assembler?.................................................11
The History......................................................................................13
Standards.........................................................................................14
Compilers Today..............................................................................15
C++.................................................................................................15
Being True to C................................................................................16
How to Read this Book....................................................................17
Dive 1
All You Need Are Bits 19
States................................................................................................19
Specifying Bit Patterns....................................................................20
Place Value Systems........................................................................21
Binary...............................................................................................24
Binary Arithmetic............................................................................26
Rollover............................................................................................27
Negative Numbers...........................................................................28
Dive 2
These aren’t the types you’re looking for 31
The Origin of Type..........................................................................31
C Types.............................................................................................32
Word Size.........................................................................................32
Data Representation........................................................................33
The Usual Types..............................................................................34
Floats................................................................................................35
The Usual Literals...........................................................................35
Precise Types...................................................................................36
Char Type.........................................................................................37
Printf and Type................................................................................39
Unicode............................................................................................40
Final Thoughts.................................................................................42
Dive 3
Type Casting 43
Casting.............................................................................................43
Negative Problems...........................................................................45
Static or Dynamic Casting...............................................................46
Floating Point...................................................................................47
Final Word.......................................................................................47
Dive 4
Expressions 49
Expressions and Statements...........................................................49
C Expressions..................................................................................51
Compile v Runtime Expressions.....................................................53
Lvalues and Rvalues........................................................................53
Side Effects and Sequence Points...................................................54
Auto Promotion...............................................................................56
Some Oddities.................................................................................58
Dive 5
Bits and More Bits 63
Addressing the Bit...........................................................................63
The Bitwise Operators.....................................................................64
Signed v Unsigned..........................................................................65
Masks...............................................................................................66
A Single Function To Write Bits......................................................68
Reading Bits.....................................................................................69
Shifting Values.................................................................................70
Significant Bits................................................................................72
Endianism........................................................................................72
Rotate...............................................................................................74
Bit Fields..........................................................................................75
Some Examples...............................................................................77
Dive 6
The Brilliant But Evil for 81
Quick Guide To The Loop Zoo........................................................81
Conditional Loops...........................................................................82
The C for Loop.................................................................................82
While and Until...............................................................................84
Break and Continue.........................................................................85
Find It and Fix It..............................................................................86
The Evil Parts...................................................................................88
Final Thoughts.................................................................................88
Dive 7
Into the Void 89
Why Void?........................................................................................89
Void In C..........................................................................................90
Void Expressions.............................................................................91
The Void Type..................................................................................92
Final Word.......................................................................................94
Dive 8
Blocks, Stacks and Locals 95
The Need To Return........................................................................95
Automatic Local Variables..............................................................97
Dead Data.........................................................................................99
Blocks.............................................................................................101
Final Word.....................................................................................103
Dive 9
Static Storage 105
File Level Storage..........................................................................106
Global Variables.............................................................................108
Declaring and Defining.................................................................108
Static..............................................................................................109
Static v Heap..................................................................................111
Dive 10
Pointers 113
The Address Operator...................................................................113
The Pointer....................................................................................115
Dereferencing................................................................................115
Lifetime Problems..........................................................................116
Operator or Type?..........................................................................118
Pointers, Arrays and Heaps...........................................................118
Dive 11
The Array and Pointer Arithmetic 119
The Storage Mapping Function.....................................................119
Index Notation...............................................................................120
Arrays Are Pointers.......................................................................121
Pointer Arithmetic.........................................................................122
Pointer to Array.............................................................................123
Casting Pointers.............................................................................124
More Than One Dimension..........................................................124
Pointers to Pointers........................................................................126
Strings As NULL-Terminated Arrays............................................128
Dive 12
Heap, The Third Memory Allocation 131
The Heap........................................................................................131
Malloc and friends........................................................................132
Casting...........................................................................................134
Dynamic Allocation......................................................................135
Dynamic Multidimensional Arrays in C89..................................136
Reallocating...................................................................................137
The Variable Length Array............................................................138
VLA Type and Dynamic Multidimensional Arrays......................138
Heap or Stack................................................................................139
Dive 13
First Class Functions 141
The C Function..............................................................................141
Partial Evaluation and Currying...................................................142
Functions First...............................................................................143
Function Pointers and Type...........................................................145
Function Parameters and Return Types........................................147
Parameters and Arguments...........................................................149
Calling Conventions......................................................................152
Ignoring Parameters.......................................................................153
Variadic..........................................................................................154
Return............................................................................................157
Functions and Big Data.................................................................158
Returning More Than One Result.................................................159
Inline Functions.............................................................................160
C Functions Considered................................................................161
Dive 14
Structs and Objects 163
The Struct......................................................................................163
Value Semantics............................................................................165
It’s Just a Block of Memory...........................................................166
Padding..........................................................................................166
Type Punning.................................................................................167
Structs As Objects.........................................................................170
The Array At The End Of The Struct...........................................173
Final Thoughts..............................................................................174
Dive 15
The Union 175
Union Basics..................................................................................175
Color...............................................................................................176
Type Punning Unions....................................................................177
When Unions Don’t Hack It..........................................................178
Tagged Union.................................................................................179
Final Thoughts..............................................................................180
Dive 16
Undefined Behavior 181
An Optimization Too Far...............................................................181
Undefined Behavior......................................................................184
UB in Practice................................................................................185
Divide By Zero and Undefined Behavior.....................................186
Signed Overflow............................................................................187
Strict Aliasing................................................................................189
Dive 17
Exceptions and the Long Jump 193
The Idea of an Exception..............................................................193
Setjmp Non-local Jumps...............................................................196
Try Catch Macros..........................................................................197
Handle or Reraise..........................................................................198
Restrictions....................................................................................201
Linux Signals.................................................................................202
goto versus longjmp......................................................................204
Final Word.....................................................................................204
C is a language with a long past and probably a long future. Being one step up from assembler, it keeps the programmer close to the hardware and if you want to understand how your code takes control without all of the abstractions applied in other languages, C is the one to choose. The only problem is that many books and articles on the subject of C are written by programmers who are happier in higher-level languages – they tend to look down on C and try to explain it as if it were a defective Java or some other language.
In Deep C Dives, Mike James provides in-depth exploration of the essence of C, identifying the strengths of its distinctive traits. This reveals that C has a very special place among the programming languages of today as a powerful and versatile option for low-level programming, something that is often overlooked in books written by programmers who would really rather be using a higher-level language. To emphasize the way in which chapters of this book focus on specific topics, they are referred to as “dives”, something that also implies a deep examination of the subject, suitable for programmers who already program in C. The intention is that you will encounter some things that you already know but will come to see them differently. You will also read things that take you into the lesser-understood areas of the C language, which only make sense when you view C as a machine-independent assembly language, which is a good way to find out more about how the machine works and influences the way things are implemented.
Topics delved into include: bit patterns, arithmetic, how C types are different, casting, expressions, the evil for, what’s void, blocks, locals, the stack, heap and static storage, pointers, arrays, multidimensional arrays as pointers, first class functions, structs, type punning, unions, undefined behavior and exceptions.