The C Programming Language.pdf

(1879 KB) Pobierz
PDF Writer, Job 2
1
1598438.001.png
2
Preface ......................................................................................................................................... 6
Preface to the first edition ........................................................................................................... 8
Chapter 1 - A Tutorial Introduction ............................................................................................ 9
1.1 Getting Started .................................................................................................................. 9
1.2 Variables and Arithmetic Expressions ............................................................................ 11
1.3 The for statement ............................................................................................................ 15
1.4 Symbolic Constants ........................................................................................................ 17
1.5 Character Input and Output ............................................................................................. 17
1.5.1 File Copying ............................................................................................................. 18
1.5.2 Character Counting .................................................................................................. 19
1.5.3 Line Counting .......................................................................................................... 20
1.5.4 Word Counting ........................................................................................................ 21
1.6 Arrays .............................................................................................................................. 23
1.7 Functions ......................................................................................................................... 25
1.8 Arguments - Call by Value ............................................................................................. 28
1.9 Character Arrays ............................................................................................................. 29
1.10 External Variables and Scope ....................................................................................... 31
Chapter 2 - Types, Operators and Expressions ......................................................................... 35
2.1 Variable Names ............................................................................................................... 35
2.2 Data Types and Sizes ...................................................................................................... 35
2.3 Constants ......................................................................................................................... 36
2.4 Declarations .................................................................................................................... 38
2.5 Arithmetic Operators ...................................................................................................... 39
2.6 Relational and Logical Operators ................................................................................... 39
2.7 Type Conversions ........................................................................................................... 40
2.8 Increment and Decrement Operators .............................................................................. 43
2.9 Bitwise Operators ............................................................................................................ 45
2.10 Assignment Operators and Expressions ....................................................................... 46
2.11 Conditional Expressions ............................................................................................... 47
2.12 Precedence and Order of Evaluation ............................................................................ 48
Chapter 3 - Control Flow .......................................................................................................... 50
3.1 Statements and Blocks .................................................................................................... 50
3.2 If-Else .............................................................................................................................. 50
3.3 Else-If .............................................................................................................................. 51
3.4 Switch ............................................................................................................................. 52
3.5 Loops - While and For .................................................................................................... 53
3.6 Loops - Do-While ........................................................................................................... 56
3.7 Break and Continue ........................................................................................................ 57
3.8 Goto and labels ............................................................................................................... 57
Chapter 4 - Functions and Program Structure .......................................................................... 59
4.1 Basics of Functions ......................................................................................................... 59
4.2 Functions Returning Non-integers .................................................................................. 62
4.3 External Variables ........................................................................................................... 63
4.4 Scope Rules ..................................................................................................................... 68
4.5 Header Files .................................................................................................................... 69
4.6 Static Variables ............................................................................................................... 70
4.7 Register Variables ........................................................................................................... 71
4.8 Block Structure ............................................................................................................... 72
4.9 Initialization .................................................................................................................... 72
4.10 Recursion ...................................................................................................................... 73
4.11 The C Preprocessor ....................................................................................................... 74
4.11.1 File Inclusion ......................................................................................................... 75
4.11.2 Macro Substitution ................................................................................................. 75
4.11.3 Conditional Inclusion ............................................................................................. 77
3
Chapter5-PointersandArrays ................................................................................................ 78
5.1PointersandAddresses ................................................................................................... 78
5.2PointersandFunctionArguments ................................................................................... 79
5.3PointersandArrays ......................................................................................................... 81
5.4AddressArithmetic ......................................................................................................... 84
5.5CharacterPointersandFunctions ................................................................................... 87
5.6PointerArrays;PointerstoPointers ................................................................................ 89
5.7Multi-dimensionalArrays ............................................................................................... 92
5.8InitializationofPointerArrays ....................................................................................... 93
5.9Pointersvs.Multi-dimensionalArrays ........................................................................... 94
5.10Command-lineArguments ............................................................................................ 95
5.11PointerstoFunctions .................................................................................................... 98
5.12ComplicatedDeclarations ........................................................................................... 100
Chapter6-Structures ............................................................................................................. 105
6.1BasicsofStructures ...................................................................................................... 105
6.2StructuresandFunctions ............................................................................................... 107
6.3ArraysofStructures ...................................................................................................... 109
6.4PointerstoStructures .................................................................................................... 112
6.5Self-referentialStructures ............................................................................................. 113
6.6TableLookup ................................................................................................................ 117
6.7Typedef ......................................................................................................................... 119
6.8Unions ........................................................................................................................... 120
6.9Bit-fields ....................................................................................................................... 121
Chapter7-InputandOutput .................................................................................................. 124
7.1StandardInputandOutput ............................................................................................ 124
7.2FormattedOutput-printf .............................................................................................. 125
7.3Variable-lengthArgumentLists ................................................................................... 127
7.4FormattedInput-Scanf ................................................................................................ 128
7.5FileAccess .................................................................................................................... 130
7.6ErrorHandling-StderrandExit ................................................................................... 132
7.7LineInputandOutput ................................................................................................... 134
7.8MiscellaneousFunctions ............................................................................................... 135
7.8.1StringOperations ................................................................................................... 135
7.8.2CharacterClassTestingandConversion ............................................................... 135
7.8.3Ungetc .................................................................................................................... 135
7.8.4CommandExecution .............................................................................................. 135
7.8.5StorageManagement ............................................................................................. 136
7.8.6MathematicalFunctions ......................................................................................... 136
7.8.7RandomNumbergeneration .................................................................................. 136
Chapter8-TheUNIXSystemInterface ................................................................................ 138
8.1FileDescriptors ............................................................................................................. 138
8.2LowLevelI/O-ReadandWrite .................................................................................. 139
8.3Open,Creat,Close,Unlink ........................................................................................... 140
8.4RandomAccess-Lseek ................................................................................................ 142
8.5Example-AnimplementationofFopenandGetc ....................................................... 142
8.6Example-ListingDirectories ....................................................................................... 145
8.7Example-AStorageAllocator ..................................................................................... 149
AppendixA-ReferenceManual ............................................................................................ 154
A.1Introduction .................................................................................................................. 154
A.2LexicalConventions .................................................................................................... 154
A.2.1Tokens ................................................................................................................... 154
A.2.2Comments ............................................................................................................. 154
A.2.3Identifiers .............................................................................................................. 154
A.2.4Keywords .............................................................................................................. 154
4
A.2.5Constants ............................................................................................................... 155
A.2.6StringLiterals ........................................................................................................ 156
A.3SyntaxNotation ............................................................................................................ 156
A.4MeaningofIdentifiers .................................................................................................. 157
A.4.1StorageClass ......................................................................................................... 157
A.4.2BasicTypes ........................................................................................................... 157
A.4.3Derivedtypes ........................................................................................................ 158
A.4.4TypeQualifiers ..................................................................................................... 158
A.5ObjectsandLvalues ..................................................................................................... 158
A.6Conversions .................................................................................................................. 159
A.6.1IntegralPromotion ................................................................................................ 159
A.6.2IntegralConversions ............................................................................................. 159
A.6.3IntegerandFloating .............................................................................................. 159
A.6.4FloatingTypes ...................................................................................................... 159
A.6.5ArithmeticConversions ........................................................................................ 159
A.6.6PointersandIntegers ............................................................................................. 160
A.6.7Void ....................................................................................................................... 161
A.6.8PointerstoVoid .................................................................................................... 161
A.7Expressions .................................................................................................................. 161
A.7.1PointerConversion ................................................................................................ 161
A.7.2PrimaryExpressions ............................................................................................. 161
A.7.3PostfixExpressions ............................................................................................... 162
A.7.4UnaryOperators .................................................................................................... 164
A.7.5Casts ...................................................................................................................... 165
A.7.6MultiplicativeOperators ....................................................................................... 165
A.7.7AdditiveOperators ................................................................................................ 166
A.7.8ShiftOperators ...................................................................................................... 166
A.7.9RelationalOperators ............................................................................................. 167
A.7.10EqualityOperators .............................................................................................. 167
A.7.11BitwiseANDOperator ....................................................................................... 167
A.7.12BitwiseExclusiveOROperator .......................................................................... 167
A.7.13BitwiseInclusiveOROperator ........................................................................... 168
A.7.14LogicalANDOperator ....................................................................................... 168
A.7.15LogicalOROperator ........................................................................................... 168
A.7.16ConditionalOperator .......................................................................................... 168
A.7.17AssignmentExpressions ..................................................................................... 169
A.7.18CommaOperator ................................................................................................. 169
A.7.19ConstantExpressions .......................................................................................... 169
A.8Declarations ................................................................................................................. 170
A.8.1StorageClassSpecifiers ........................................................................................ 170
A.8.2TypeSpecifiers ..................................................................................................... 171
A.8.3StructureandUnionDeclarations ......................................................................... 172
A.8.4Enumerations ........................................................................................................ 175
A.8.5Declarators ............................................................................................................ 175
A.8.6MeaningofDeclarators ......................................................................................... 176
A.8.7Initialization .......................................................................................................... 178
A.8.8Typenames ........................................................................................................... 180
A.8.9Typedef ................................................................................................................. 181
A.8.10TypeEquivalence ................................................................................................ 181
A.9Statements .................................................................................................................... 182
A.9.1LabeledStatements ............................................................................................... 182
A.9.2ExpressionStatement ............................................................................................ 182
A.9.3CompoundStatement ............................................................................................ 182
A.9.4SelectionStatements ............................................................................................. 183
5
A.9.5IterationStatements .............................................................................................. 183
A.9.6Jumpstatements .................................................................................................... 184
A.10ExternalDeclarations ................................................................................................. 185
A.10.1FunctionDefinitions ........................................................................................... 185
A.10.2ExternalDeclarations .......................................................................................... 186
A.11ScopeandLinkage ..................................................................................................... 187
A.11.1LexicalScope ...................................................................................................... 187
A.11.2Linkage ................................................................................................................ 187
A.12Preprocessing ............................................................................................................. 188
A.12.1TrigraphSequences ............................................................................................. 188
A.12.2LineSplicing ....................................................................................................... 188
A.12.3MacroDefinitionandExpansion ........................................................................ 189
A.12.4FileInclusion ...................................................................................................... 190
A.12.5ConditionalCompilation ..................................................................................... 191
A.12.6LineControl ........................................................................................................ 192
A.12.7ErrorGeneration ................................................................................................. 192
A.12.8Pragmas ............................................................................................................... 192
A.12.9Nulldirective ...................................................................................................... 192
A.12.10Predefinednames .............................................................................................. 192
A.13Grammar .................................................................................................................... 193
AppendixB-StandardLibrary .............................................................................................. 200
B.1InputandOutput:<stdio.h> ......................................................................................... 200
B.1.1FileOperations ...................................................................................................... 200
B.1.2FormattedOutput .................................................................................................. 201
B.1.3FormattedInput ..................................................................................................... 203
B.1.4CharacterInputandOutputFunctions .................................................................. 204
B.1.5DirectInputandOutputFunctions ........................................................................ 205
B.1.6FilePositioningFunctions ..................................................................................... 205
B.1.7ErrorFunctions ...................................................................................................... 206
B.2CharacterClassTests:<ctype.h> ................................................................................. 206
B.3StringFunctions:<string.h> ........................................................................................ 206
B.4MathematicalFunctions:<math.h> ............................................................................. 207
B.5UtilityFunctions:<stdlib.h> ........................................................................................ 208
B.6Diagnostics:<assert.h> ................................................................................................ 210
B.7VariableArgumentLists:<stdarg.h> ........................................................................... 210
B.8Non-localJumps:<setjmp.h> ...................................................................................... 211
B.9Signals:<signal.h> ....................................................................................................... 211
B.10DateandTimeFunctions:<time.h> .......................................................................... 211
B.11Implementation-definedLimits:<limits.h>and<float.h> ........................................ 213
AppendixC-SummaryofChanges ....................................................................................... 215
Zgłoś jeśli naruszono regulamin