C Programming for Electronic Engineers Keith Jackson BSc CEng MIEE with acknowledgement to Gavin Eamshaw MEng School of Electronic, Communication and Electrical Engineering University of Plymouth MACMILLAN
Preface xni 1 Introduction 1 1.1 Introduction 1 1.2 Origins of C 1 1.3 Advantages and disadvantages of C 1 1.4 Binary numbers 2 1.5 Self-assessment test - Binary 4 1.6 Binary arithmetic 4 1.7 Hexadecimal notation 6 1.8 Octal notation 8 1.9 Self-assessment test - Number bases 8 1.10 Summary 9 2 Programming Concepts 10 2.1 Introduction 10 2.2 Computer hardware 10 2.3 Operating systems 11 2.4 Memory structure 12 2.5 Machine code 13 2.6 High- and low-level languages 15 2.7 Compilers and linkers 16 2.8 Structure of a C program 18 2.9 Generalised program layout 19 2.10 Preprocessor 20 2.11 ANSI standard 20 2.12 Summary 21 2.13 Exercises 21 3 Representation of Data 23 3.1 Introduction 23 3.2 Basic data types 23 3.3 Integer types 23 3.4 Two's complement 24 3.5 Self-assessment exercise - Integers 25 3.6 Short and long integers 26 3.7 Arithmetic overflow 26 3.8 Floating point representation 27 3.9 Double and long double representation 28 3.10 Character representation 28 vii
Vlll 3.11 3.12 Summary Exercises 29 30 4 Variables and Constants 4.1 Introduction 4.2 Variables 4.3 Variable definition 4.4 Identifiers 4.5 A brief note on printf and scanf 4.6 Case study - Resistor calculation 4.7 Numerical constants 4.8 Character and string constants 4.9 Defining constants with tfdefine 4.10 Self-assessment test - Variables 4.11 Summary 4.12 Exercises 5 Arithmetic Operations 5.1 Introduction 5.2 Integer expressions 5.3 Floating point expressions 5.4 Assignment statements 5.5 Arithmetic operators 5.6 Unary + and - operators 5.7 Operator precedence 5.8 Self-assessment test - Integer expressions 5.9 Mathematical functions 5.10 Case study - Complex numbers 5.11 Arithmetic overflow 5.12 Mixed type expressions 5.13 Type casting 5.14 Self-assessment test - Mixed expressions 5.15 Increment and decrement operators 5.16 Summary 5.17 Exercises 6 Control of Program Flow 6.1 Introduction 6.2 The if statement 6.3 Boolean expressions 6.4 if-else statement 6.5 Self-assessment test - Boolean expressions 6.6 /orlloop 6.7 do-while loop 31 31 31 32 33 34 35 37 38 39 39 40 41 43 43 44 45 46 46 47 49 50 51 52 53 53 54 56 56 56 57 59 60 61 64
6.8 6.9 6.10 6.11 6.12 6.13 while loop Choice of loop type Indentation Case study - Guessing game Summary Exercises ix 65 66 67 67 70 70 7 Input and Output 7.1 Introduction 7.2 Data types 7.3 Output using printf 7.4 printf format codes 7.5 Width and precision 7.6 Escape sequences 7.7 Self-assessment test printf 7.8 Input using scanf 7.9 Self-assessment test - scanf 7.10 Problems with scanf 7.11 Reading characters with getchar 7.12 Detecting key strokes 7.13 Keyboard buffering 7.14 Detecting invalid input 7.15 Case study - Diode table 7.16 Summary 7.17 Exercises 8 Functions 8.1 Introduction 8.2 Simple procedural functions 8.3 Parameterised functions 8.4 Self-assessment test - Functions 8.5 Functions with return values 8.6 Function prototypes 8.7 Case study - Oscillator design table 8.8 Local variables 8.9 Global variables 8.10 The main function 8.11 Self-assessment test - Variable scope 8.12 Summary 8.13 Exercises 74 76 78 79 80 82 83 85 86 87 87 89 94 94 96 96 96 97 99 100 102 103 107 108 109 110 111 112 9 Arrays 114 9.1 Introduction 114 9.2 The concept of arrays 114
X 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 Array definitions Array subscripts Self-assessment test - Arrays Passing arrays to functions Case study - Statistical analysis Algorithm design Sorting arrays Summary Exercises 115 116 118 119 120 124 126 128 129 10 Strings 10.1 Introduction 10.2 String variables 10.3 Input and output of strings 10.4 String assignment 10.5 String comparison 10.6 Case conversion 10.7 String library functions 10.8 String value conversions 10.9 Self-assessment test - Strings 10.10 Case study - Resistor calculator 10.11 Summary 10.12 Exercises 11 File Input and Output 11.1 Introduction 11.2 File structure 11.3 Opening and closing files 11.4 Writing to files 11.5 Reading from files 11.6 Opening user-specified files 11.7 Case study - Data post-processing 11.8 Summary 11.9 Exercises 12 Pointers 12.1 Introduction 12.2 Pointer concepts 12.3 Defining pointers 12.4 The & operator 12.5 Assigning pointers 12.6 Pointer indirection 12.7 Self-assessment test - Pointers 12.8 NULL pointers 131 131 131 132 134 135 136 137 137 139 139 1 143 144 144 144 145 147 148 150 151 155 156 158 159 160 161 162
12.9 Pointers as parameters 12.10 Arrays as parameters 12.11 Pointers as return values 12.12 Absolute addressing 12.13 Self-assessment test - Pointer parameters 12.14 Summary 12.15 Exercises 13 Advanced Data Types 13.1 Introduction 13.2 The concept of structures 13.3 Declaring new types 13.4 Declaring a structure 13.5 Defining structured variables 13.6 Accessing structure members 13.7 Self-assessment test - Structures 13.8 Arrays of structures 13.9 Passing records to functions 13.10 Returning structures from functions 13.11 Case study - Telephone directory 13.12 Summary 13.13 Programming exercises 14 Low-level Operations 14.1 Introduction 14.2 Input and output ports 14.3 Controlling ports 14.4 in and out functions 14.5 The low-level operators 14.6 The NOT operator 14.7 The AND operator 14.8 The OR operator 14.9 The XOR operator 14.10 The shift operators 14.11 Case study - Temperature monitor 14.12 Summary 15 Graphics 15.1 Introduction 15.2 Display hardware 15.3 Physical coordinates 15.4 Graphics library functions 15.5 Coordinate mapping 15.6 Case study - Fourier composition XI 163 165 168 169 1 1 173 175 175 175 176 176 177 178 179 180 183 184 185 190 191 195 197 198 198 199 200 200 201 204 205 205 205 206 208 210 212
Xll 15.7 Summary 15.8 Exercises 16 Software Engineering 16.1 Introduction 16.2 Software life-cycle 16.3 Indentifying requirements 16.4 System design 16.5 Implementation 16.6 Testing 16.7 Documentation and maintenance 16.8 Software engineering using C 16.9 Separate file compilation 16.10 Makefiles 16.11 Summary 218 219 220 220 220 221 221 223 223 224 224 224 228 228 Appendix A Appendix B Appendix C Appendix D Index Glossary ASCII codes Errors in C Keywords 229 232 234 238 239