C for Electronic Engineering with applied software engineering William Buchanan Prentice Hall London New York Toronto Sydney Tokyo Singapore Madrid Mexico City Munich
CONTENTS PREFACE 1 INTRODUCTION 1.1 Hardware, software and firmware 1.2 History of C 1.3 Basic computer architecture 1.4 Compiling, linking and producing an executable program 1.5 Pre-processor 1.6 Structure 1.7 Numbers and representations 1.7.1 Negative numbers 1.7.2 Hexadecimal and octal numbers 1.8 Character constants 1.9 Data types 1.10 Declaration of variables 1.11 C operators 1.11.1 Arithmetic 1.11.2 Relationship 1.11.3 Logical (TRUE or FALSE) 1.11.4 Bitwise 1.12 Precedence
viii Cfor Electronic Engineering 1.13 Data type conversion 1.14 Keywords 1.15 Jargon 1.16 Tutorial 2 INPUT/OUTPUT 2.1 Standard output (printf (), puts () and putchar ()) 2.1.1 Special control characters 2.1.2 Conversion control characters 2.1.3 Examples 2.2 Standard input (scanf (), gets () and getchar ()) 2.2.1 Examples 2.3 Tutorial 3 SELECTION STATEMENTS 3.1 if..else statements 3.1.1 Examples 3.2 switch statement 3.2.1 Examples 3.3 Tutorial 4 REPETITIVE STATEMENTS 4.1 for 4.1.1 Examples 4.2 while () 4.3 do..while () 4.4 break 4.5 continue 4.6 Examples 4.6.1 Boolean equation 4.6.2 CR active filter 4.7 Tutorial 5 FUNCTIONS
5.1 Parameter passing 5.2 Return value 5.3 Function type 5.4 Using the pre-processor to define function macros 5.5 Examples 5.5.1 Combinational logic 5.5.2 Impedance of a series RL circuit 5.5.3 AC response of a series RC circuit 5.5.4 Harmonics of a repetitive pulse waveform 5.5.5 Classification of radio waves 5.5.6 Impedance of a transmission line 5.6 Tutorial 6 POINTERS 6.1 Pointers with functions 6.2 Examples 6.2.1 Quadratic equations 6.2.2 Equivalent parallel resistance 6.2.3 Impedance of an RL circuit 6.2.4 Memory viewer 6.2.5 Accessing PC video text memory 6.3 Tutorial 7 ARRAYS 7.1 Pointers and arrays 7.2 Passing arrays to functions 7.3 Array initialization 7.4 Multidimensional arrays 7.5 Dynamic allocation 7.6 Pointer arithmetic 7.7 Arrray of pointers 7.8 Examples 7.8.1 Boolean circuit 7.8.2 Impedance of an RL circuit 7.8.3 DC circuit analysis 7.8.4 Logic simulation
x Cfor Electronic Engineering 7.9 Tutorial 8 STRINGS 8.1 String input 8.2 String assignment 8.3 Standard string functions 8.4 Impedance of a parallel RC circuit 8.5 Circuit selection 8.6 Setting up an array of strings 8.7 Tutorial 9 STRUCTURES 9.1 Array of structures 9.2 Allocating structures dynamically 9.3 Bit fields 9.4 Time structure 9.5 Examples 9.5.1 Traffic light sequence 9.5.2 Input impedance of an RLC circuit 9.5.3 74-series ICs 9.6 Tutorial 10 FILE INPUT/OUTPUT 10.1 Opening a file (f open ()) 10.2 Closing a file (f close ()) 10.3 Printing text to a file (f printf ()) 10.4 Reading text from a file (f scanf ()) 10.5 Finding the end of a file (f eof ()) 10.6 Getting a string of text from a file (f gets ()) 10.7 Putting a string of text to a file (f puts ())
Contents xi 10.8 Putting a character to a file (f puto ()) 260 10.9 Getting a character from a file (f getc ()) 260 10.10 Binary files 261 10.10.1 Reading binary data from a file (f read ()) 261 10.10.2 Writing binary data to a file (fwrite ()) 261 10.11 Examples 262 10.11.1 Boolean equation 262 10.11.2 Averages program 264 10.11.3 Binary read/write 266 10.12 Tutorial 268 11 SYSTEMS PROGRAMMING 273 11.1 System calls 273 11.2 Passing arguments 276 11.3 Examples 277 11.4 Tutorial 280 12 PROJECTS 283 12.1 Resonant frequency of a series RLC circuit 283 12.2 Current flow in a diode 286 12.3 Boolean circuit 289 12.4 Decimal to binary converter 291 12.5 Resistor colour code program 293 12.6 Tutorial 296 13 APPLIED SOFTWARE ENGINEERING 307 13.1 Software development 307 13.2 RLC circuit program 308 13.2.1 Requirement analysis and specification 308 13.2.2 Requirement analysis 309 13.2.3 Requirements specification 310 13.2.4 Design 315 13.2.5 Module specifications 316 13.2.6 Implementation 318 13.2.7 Version control 323
xii Cfor Electronic Engineering 13.2.8 System testing 13.2.9 Installation 13.3 Linear feedback shift register 13.3.1 Requirements analysis 13.3.2 Requirements specification 13.3.3 Design 13.3.4 Module specifications 13.3.5 Coding 13.3.6 System testing 13.3.7 Installation 323 327 328 328 329 330 331 332 335 336 13.4 Tutorial 337 14 INTRODUCTION TO PC GRAPHICS USING TURBO/BORLAND C 341 14.1 Basic graphics routines 14.1.1 Closing graphics 14.1.2 Initializing graphics 14.1.3 Drawing a pixel 14.1.4 Drawing a line 14.1.5 Drawing a rectangle 14.1.6 Displaying text 14.1.7 Drawing a circle 14.1.8 Bit-mapped graphics 14.2 Tutorial 346 346 346 347 349 349 351 352 353 356 APPENDIX Al Structure charts A2 Header flies and associated functions A2.1 Classification routines A2.2 Conversion routines A2.3 Input/ Output routines A2.4 String manipulation routines A2.5 Math routines A2.6 Time and Date routines A2.7 Other standard routines A3 Things that can go wrong A3.1 Typical compiler errors A3.2 Typical compiler warnings A3.3 Typical run-time errors ICO 360 361 363 364 368 369 372 373 373 373 377 381 INDEX 385