Page 1
An Introduction to the C Programming Language and Software Design Tim Bailey
An introductory textbook for first-year undergraduate software engineering students, combining C programming fundamentals with software design principles. It emphasizes modularity, function interfaces, error handling, readable code, and top-down and bottom-up design, including a Tic-Tac-Toe case study. The contents also list coverage of pointers, arrays, strings, dynamic memory, the preprocessor, structures and unions, bitwise operations, input and output, generic programming, and data structures. The document identifies itself as Draft 0.6, dated July 12, 2005.
The first 5 pages, shown as text so search engines and readers can find this document.
An Introduction to the C Programming Language and Software Design Tim Bailey
Preface This textbook began as a set of lecture notes for a first-year undergraduate software engineering course in 2003. The course was run over a 13-week semester with two lectures a week. The intention of this text is to cover topics on the C programming language and introductory software design in sequence as a 20 lecture course, with the material in Chapters 2, 7, 8, 11, and 13 well served by two lectures apiece. Ample cross-referencing and indexing is provided to make the text a servicable reference, but more complete works are recommended. In particular, for the practicing programmer, the best available tutorial and reference is Kernighan and Ritchie [KR88] and the best in-depth reference is Harbison and Steele [HS95, HS02]. The influence of these two works on this text is readily apparent throughout. What sets this book apart from most introductory C-programming texts is its strong emphasis on software design. Like other texts, it presents the core language syntax and semantics, but it also addresses aspects of program composition, such as function interfaces (Section 4.5), file modularity (Section 5.7), and object-modular coding style (Section 11.6). It also shows how to design for errors using assert() and exit() (Section 4.4). Chapter 6 introduces the basics of the software design process—from the requirements and specification, to top-down and bottom-up design, to writing actual code. Chapter 14 shows how to write generic software (i.e., code designed to work with a variety of different data types). Another aspect that is not common in introductory C texts is an emphasis on bitwise operations. The course for which this textbook was originally written was prerequisite to an embedded systems course, and hence required an introduction to bitwise manipulations suitable for embedded systems programming. Chapter 12 provides a thorough discussion of bitwise programming techniques. The full source code for all significant programs in this text can be found on the web at the address www.acfr.usyd.edu.au/homepages/academic/tbailey/index.html. Given the volatile nature of the web, this link may change in subsequent years. If the link is broken, please email me at tbailey@acfr.usyd.edu.au and I will attempt to rectify the problem. This textbook is a work in progress and will be refined and possibly expanded in the future. No doubt there are errors and inconsistencies—both technical and grammatical—although hopefully nothing too seriously misleading. If you find a mistake or have any constructive comments please feel free to send me an email. Also, any interesting or clever code snippets that might be incorporated in future editions are most welcome. Tim Bailey 2005. Draft 0.6 (July 12, 2005) TODO: - complete Chapter 16 - complete appendices - complete the index i
Contents Preface i Contents ii 1 Introduction 1 1.1 Programming and Programming Languages . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 The C Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 A First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 Variants of Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 A Numerical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 Another Version of the Conversion Table Example . . . . . . . . . . . . . . . . . . . 6 1.7 Organisation of the Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Types, Operators, and Expressions 8 2.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.4 Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.5 printf Conversion Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.6 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.7 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.8 Relational and Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.9 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.10 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.11 Type Conversions and Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3 Branching and Iteration 17 3.1 If-Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.2 ?: Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.3 Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.4 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3.5 Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.6 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.7 Break and Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.8 Goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4 Functions 25 4.1 Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.2 Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.3 Benefits of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 4.4 Designing For Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ii
4.5 Interface Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.6 The Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5 Scope and Extent 33 5.1 Local Scope and Automatic Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.2 External Scope and Static Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 5.3 The static Storage Class Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.4 Scope Resolution and Name Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 5.5 Summary of Scope and Extent Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 5.6 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 5.7 Modular Programming: Multiple File Programs . . . . . . . . . . . . . . . . . . . . . 39 6 Software Design 41 6.1 Requirements and Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.2 Program Flow and Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 6.3 Top-down and Bottom-up Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 6.4 Pseudocode Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6.5 Case Study: A Tic-Tac-Toe Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6.5.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6.5.2 Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6.5.3 Program Flow and Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 45 6.5.4 Bottom-Up Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 6.5.5 Top-Down Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.5.6 Benefits of Modular Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 7 Pointers 49 7.1 What is a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 7.2 Pointer Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 7.3 Pass By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 7.4 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 7.5 Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 7.6 Return Values and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7.7 Pointers to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.8 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 8 Arrays and Strings 59 8.1 Array Initialisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 8.2 Character Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 8.3 Strings and the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 8.4 Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 8.5 Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 9 Dynamic Memory 68 9.1 Different Memory Areas in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 9.2 Standard Memory Allocation Functions . . . . . . . . . . . . . . . . . . . . . . . . . 69 9.3 Dynamic Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 9.4 Example: Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 9.5 Example: An Expandable Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 iii
10 The C Preprocessor 79 10.1 File Inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 10.2 Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 10.3 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 10.3.1 Macro Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 10.3.2 More Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 10.3.3 More Complex Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.4 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 11 Structures and Unions 86 11.1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 11.2 Operations on Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 11.3 Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 11.4 Self-Referential Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 11.5 Typedefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.6 Object-Oriented Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 11.7 Expandable Array Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 11.8 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 12 Bitwise Operations 99 12.1 Binary Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 12.2 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 12.2.1 AND, OR, XOR, and NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 12.2.2 Right Shift and Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 12.2.3 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 12.3 Common Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 12.4 Bit-fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 13 Input and Output 105 13.1 Formatted IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 13.1.1 Formatted Output: printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 13.1.2 Formatted Input: scanf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 13.1.3 String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.2 File IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.2.1 Opening and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 13.2.2 Standard IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 13.2.3 Sequential File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 13.2.4 Random Access File Operations . . . . . . . . . . . . . . . . . . . . . . . . . 112 13.3 Command-Shell Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 13.4 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 14 Generic Programming 115 14.1 Basic Generic Design: Typedefs, Macros, and Unions . . . . . . . . . . . . . . . . . . 115 14.1.1 Typedefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 14.1.2 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 14.1.3 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 14.2 Advanced Generic Design: void * . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 14.2.1 Case Study: Expandable Array . . . . . . . . . . . . . . . . . . . . . . . . . . 117 14.2.2 Type Specific Wrapper Functions . . . . . . . . . . . . . . . . . . . . . . . . . 121 14.2.3 Case Study: qsort() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 iv
Doxxly editorial guide
This guide helps you understand the scope of An Introduction to the C Programming Language and Software Design before or alongside reading the original document. It is based on the catalogue record, credited creator, source, classification and topics shown on this page. It does not replace the original work and does not invent claims about passages that Doxxly cannot verify.
An Introduction to the C Programming Language and Software Design is a 153-page documentcredited to Ivo Q. Whitlock. An introductory textbook for first-year undergraduate software engineering students, combining C programming fundamentals with software design principles. It emphasizes modularity, function interfaces, error handling, readable code, and top-down and bottom-up design, including a Tic-Tac-Toe case study. The contents also list coverage of pointers, arrays, strings, dynamic memory, the preprocessor, structures and unions, bitwise operations, input and output, generic programming, and data structures. The document identifies itself as Draft 0.6, dated July 12, 2005. It appears in the Education & Learning collection, which groups it with other material that readers may use for comparison and further study.
The item can be approached in several ways. A reader looking for a quick orientation can begin with the free five-page preview, inspect the page structure and use the metadata to judge relevance. Someone undertaking deeper research can work through the complete file, search its indexed text, record useful page references and compare its treatment of C programming, Software design, Undergraduate textbook, Modular programming, Pointers, Arrays and strings, Dynamic memory, Bitwise operations, Generic programming, Data structures with the related resources linked below.
Rather than reducing the work to a single claim, this page treats it as a source that should be read in context. The title, creator, publication source, licence and format all help explain what kind of evidence it may offer. Readers should distinguish the original author's statements from Doxxly's catalogue description, and should cite the original edition when using the material in academic, professional or published work.
The catalogue connects this document with C programming, Software design, Undergraduate textbook, Modular programming, Pointers, Arrays and strings, Dynamic memory, Bitwise operations, Generic programming, Data structures. These terms offer practical entry points rather than a substitute for close reading. Start by identifying the definitions, examples, arguments, tables or visual explanations attached to each topic. Then note where the creator draws connections, makes distinctions or moves from background material to a conclusion, recommendation, calculation or narrative development.
A useful reading method is to separate description from interpretation. First record what the item actually presents: names, headings, figures, formulas, quotations or stated propositions. Next consider why those details matter within the whole work. Finally compare them with another source from the same subject area. This process helps turn a long digital file into evidence that can be evaluated rather than merely skimmed.
The following is an editorial content guide based on the verified catalogue topics. It is not represented as the original author's formal table of contents. Use it to organise notes while moving through the 153 pages in the player.
Use this section of the reading guide to identify how C programming appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Software design appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Undergraduate textbook appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Modular programming appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Pointers appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Arrays and strings appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Dynamic memory appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
Use this section of the reading guide to identify how Bitwise operations appears in An Introduction to the C Programming Language and Software Design, note important terms, and connect the subject to the wider purpose of the work.
When a heading in the original differs from this guide, follow the original heading in your citation. Page numbers can also vary between editions. Doxxly's direct page field is useful for navigation, but formal references should include the title, creator, source and edition details available in the file itself.
Doxxly credits Ivo Q. Whitlock as the author or creator of An Introduction to the C Programming Language and Software Design. This page does not add an unverified biography, job title or institutional affiliation. The attribution is preserved so readers can distinguish the creator from the hosting library and follow the original record when an external author link is available.
Author context matters because a work reflects a particular purpose, audience and point of view. Before relying on the material, consider what the creator set out to explain, when the source was produced and whether later research or revised guidance should also be consulted. For literature and philosophy, compare translations and editions; for technical material, check whether a newer version has superseded the one shown here.
The recorded source for this item is www-personal.acfr.usyd.edu.au. Doxxly provides access and discovery tools but does not claim to be the original publisher unless explicitly stated. Source links, creator attribution and the recorded Publicly available file collected for editorial review licence are displayed to help readers assess provenance and permitted use.
Provenance is especially important for reports, standards, presentations and working spreadsheets. Check the original source for amendments, supporting files and current policy. A trustworthy research workflow keeps the source URL with any notes or downloaded copy, verifies important figures against the original, and avoids treating an archived document as automatically current.
Visit www-personal.acfr.usyd.edu.auBegin with the title and opening pages, then use the guide above to decide whether to read sequentially or move to a particular topic. The Doxxly player provides previous and next navigation, a direct page field, zoom steps, fit-to-width and fit-to-page modes, rotation and fullscreen. These controls are useful when a page contains small type, a wide table, an image, a scanned layout or a dense sequence of notes.
The first five pages are open to every visitor. A plan unlocks the complete item and full-document search. Search is best used for distinctive names, technical terms or short phrases; broad words can produce too many possible contexts. When you find a useful passage, read the surrounding pages so that a sentence, chart or formula is not separated from its explanation.
If downloading is enabled, members can obtain the original PDF file. Keep the original format when formulas, slide sequencing, pagination or layout carries meaning. Respect the Publicly available file collected for editorial review terms displayed on this page, and use the rights-report link if you believe the attribution or availability needs review.
As you read An Introduction to the C Programming Language and Software Design, ask which audience the creator addresses, what problem or idea organises the work, what evidence is presented, and which assumptions remain unstated. Note whether important terms are defined consistently and whether examples represent a broader pattern or a narrow case. For quantitative material, record units, time periods, formulas and data sources. For interpretive material, separate the creator's language from your own summary.
A second set of questions concerns relevance today. Which parts remain durable, which depend on their historical or institutional setting, and what would need confirmation from a newer source? Following the tags and the Education & Learning collection can reveal useful comparisons. Agreement between sources can strengthen confidence, while disagreement can point to changing definitions, methods, evidence or priorities.
Finally, decide what you need from the item: background knowledge, a quotation, a worked example, a model, a visual explanation or a starting bibliography. That purpose should guide the depth of reading and the notes you keep. Doxxly helps locate and navigate the source; careful interpretation remains the reader's responsibility.
Practical answers about reading, downloading and understanding this document on Doxxly.
Cleo G. Vance
Lior D. Dunmore
Kaya J. Vance
Yusuf Y. Grayling
Hana W. Cressley
Bram Q. Oakfield