Beginning C From Beginner to Pro (German Gonzalez-Morris, Ivor Horton) (Z-Library)
Author: German Gonzalez-Morris, Ivor Horton
科学
No Description
📄 File Format:
PDF
💾 File Size:
6.0 MB
14
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
Beginning C From Beginner to Pro Seventh Edition German Gonzalez-Morris Ivor Horton
📄 Page
3
German Gonzalez-Morris Santiago, Chile Ivor Horton STRATFORD UPON AVON, UK Beginning C: From Beginner to Pro, Seventh Edition ISBN-13 (pbk): 979-8-8688-0148-8 ISBN-13 (electronic): 979-8-8688-0149-5 https://doi.org/10.1007/979-8-8688-0149-5 Copyright © 2024 by German Gonzalez-Morris and Ivor Horton This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Melissa Duffy Development Editor: James Markham Editorial Assistant: Gryffin Winkler Cover designed by eStudioCalamar Cover image designed by Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit https://www.apress.com/gp/services/ source-code. Paper in this product is recyclable
📄 Page
4
To my family, Patricia, Raimundo, and Gregorio —German Gonzalez-Morris In memory of my beautiful wife, Eve —Ivor Horton
📄 Page
5
v Table of Contents ■ About the Authorsxix ■ About the Technical Reviewer xxi ■ Acknowledgments xxiii ■ Introduction xxv ■ Chapter 1: Programming in C 1 The C Language ���������������������������������������������������������������������������������������������������������������� 1 The Standard Library �������������������������������������������������������������������������������������������������������� 2 Learning C ������������������������������������������������������������������������������������������������������������������������ 3 Creating C Programs �������������������������������������������������������������������������������������������������������� 3 Editing����������������������������������������������������������������������������������������������������������������������������������������������������� 3 Compiling ������������������������������������������������������������������������������������������������������������������������������������������������ 4 Linking ���������������������������������������������������������������������������������������������������������������������������������������������������� 5 Executing ������������������������������������������������������������������������������������������������������������������������������������������������ 5 Creating Your First Program ��������������������������������������������������������������������������������������������� 7 Editing Your First Program ������������������������������������������������������������������������������������������������ 8 Dealing with Errors ����������������������������������������������������������������������������������������������������������� 8 Dissecting a Simple Program ������������������������������������������������������������������������������������������� 9 Comments �������������������������������������������������������������������������������������������������������������������������������������������� 10 Preprocessing Directives ���������������������������������������������������������������������������������������������������������������������� 11 Defining the main() Function ���������������������������������������������������������������������������������������������������������������� 11 Keywords ���������������������������������������������������������������������������������������������������������������������������������������������� 12
📄 Page
6
vi The Body of a Function ������������������������������������������������������������������������������������������������������������������������� 12 Outputting Information ������������������������������������������������������������������������������������������������������������������������� 14 Function Arguments ����������������������������������������������������������������������������������������������������������������������������� 14 Control Characters �������������������������������������������������������������������������������������������������������������������������������� 15 Trigraph Sequences ������������������������������������������������������������������������������������������������������������������������������ 17 The Preprocessor ����������������������������������������������������������������������������������������������������������� 17 Developing Programs in C ���������������������������������������������������������������������������������������������� 18 Understanding the Problem ������������������������������������������������������������������������������������������������������������������ 18 Detailed Design ������������������������������������������������������������������������������������������������������������������������������������ 19 Implementation ������������������������������������������������������������������������������������������������������������������������������������� 19 Testing �������������������������������������������������������������������������������������������������������������������������������������������������� 19 Functions and Modular Programming ���������������������������������������������������������������������������� 20 Common Mistakes ���������������������������������������������������������������������������������������������������������� 24 Points to Remember ������������������������������������������������������������������������������������������������������� 24 Summary ������������������������������������������������������������������������������������������������������������������������ 25 ■ Chapter 2: First Steps in Programming 27 Memory in Your Computer ���������������������������������������������������������������������������������������������� 27 What Is a Variable? ��������������������������������������������������������������������������������������������������������� 30 Naming Variables ���������������������������������������������������������������������������������������������������������������������������������� 31 Variables That Store Integers���������������������������������������������������������������������������������������������������������������� 31 Using Variables ������������������������������������������������������������������������������������������������������������������������������������� 36 Initializing Variables ������������������������������������������������������������������������������������������������������������������������������ 38 More on Division with Integers ������������������������������������������������������������������������������������������������������������� 44 Unary Operators ������������������������������������������������������������������������������������������������������������������������������������ 44 Unary Minus Operator ��������������������������������������������������������������������������������������������������������������������������� 44 Variables and Memory���������������������������������������������������������������������������������������������������� 45 Signed Integer Types ���������������������������������������������������������������������������������������������������������������������������� 46 Unsigned Integer Types ������������������������������������������������������������������������������������������������������������������������ 46 Specifying Integer Constants ���������������������������������������������������������������������������������������������������������������� 47 Binary Integer Constants ���������������������������������������������������������������������������������������������������������������������� 48 ■ Table of ConTenTs
📄 Page
7
vii Working with Floating-Point Numbers ��������������������������������������������������������������������������� 50 Floating-Point Number Representation ������������������������������������������������������������������������������������������������ 50 Floating-Point Variables ������������������������������������������������������������������������������������������������������������������������ 52 Division Using Floating-Point Values ���������������������������������������������������������������������������������������������������� 53 Controlling the Number of Decimal Places in the Output ��������������������������������������������������������������������� 54 Controlling the Output Field Width �������������������������������������������������������������������������������������������������������� 55 More Complicated Expressions �������������������������������������������������������������������������������������� 55 Checked Integer Arithmetic �������������������������������������������������������������������������������������������� 59 constexpr������������������������������������������������������������������������������������������������������������������������ 59 Defining Named Constants ��������������������������������������������������������������������������������������������� 61 Knowing Your Limitations ����������������������������������������������������������������������������������������������� 63 Introducing the sizeof Operator �������������������������������������������������������������������������������������� 66 Choosing the Correct Type for the Job ��������������������������������������������������������������������������� 68 Explicit Type Conversion ������������������������������������������������������������������������������������������������� 71 Automatic Conversions ������������������������������������������������������������������������������������������������������������������������� 72 Rules for Implicit Conversions �������������������������������������������������������������������������������������������������������������� 73 Implicit Conversions in Assignment Statements ���������������������������������������������������������������������������������� 73 Type Inference �������������������������������������������������������������������������������������������������������������������������������������� 75 typeof ����������������������������������������������������������������������������������������������������������������������������� 76 More Numeric Data Types ���������������������������������������������������������������������������������������������� 76 Character Type �������������������������������������������������������������������������������������������������������������������������������������� 76 Character Input and Character Output �������������������������������������������������������������������������������������������������� 77 Enumerations���������������������������������������������������������������������������������������������������������������������������������������� 81 Choosing Enumerator Values ���������������������������������������������������������������������������������������������������������������� 82 Unnamed Enumeration Types ��������������������������������������������������������������������������������������������������������������� 83 Variables That Store Boolean Values ���������������������������������������������������������������������������������������������������� 84 The op= Form of Assignment ����������������������������������������������������������������������������������������� 84 Mathematical Functions ������������������������������������������������������������������������������������������������� 86 ■ Table of ConTenTs
📄 Page
8
viii Designing a Program ������������������������������������������������������������������������������������������������������ 87 The Problem ����������������������������������������������������������������������������������������������������������������������������������������� 87 The Analysis ������������������������������������������������������������������������������������������������������������������������������������������ 87 The Solution ������������������������������������������������������������������������������������������������������������������������������������������ 89 Summary ������������������������������������������������������������������������������������������������������������������������ 93 ■ Chapter 3: Making Decisions 97 The Decision-Making Process ���������������������������������������������������������������������������������������� 97 Arithmetic Comparisons ����������������������������������������������������������������������������������������������������������������������� 98 The Basic if Statement ������������������������������������������������������������������������������������������������������������������������� 99 Extending the if Statement: if-else ����������������������������������������������������������������������������������������������������� 102 Using Blocks of Code in if Statements ������������������������������������������������������������������������������������������������ 105 Nested if Statements �������������������������������������������������������������������������������������������������������������������������� 106 Testing Characters ������������������������������������������������������������������������������������������������������������������������������ 109 Logical Operators ������������������������������������������������������������������������������������������������������������������������������� 113 The Conditional Operator �������������������������������������������������������������������������������������������������������������������� 116 Operator Precedence: Who Goes First? ���������������������������������������������������������������������������������������������� 119 Multiple-Choice Questions ������������������������������������������������������������������������������������������� 123 Using else-if Statements for Multiple Choices ����������������������������������������������������������������������������������� 124 The switch Statement ������������������������������������������������������������������������������������������������������������������������� 124 The goto Statement ���������������������������������������������������������������������������������������������������������������������������� 133 Bitwise Operators ��������������������������������������������������������������������������������������������������������� 134 The op= Use of Bitwise Operators ������������������������������������������������������������������������������������������������������ 137 Using Bitwise Operators ��������������������������������������������������������������������������������������������������������������������� 138 Bit-Precise Integers ������������������������������������������������������������������������������������������������������ 142 Modern Bit Utilities ������������������������������������������������������������������������������������������������������� 143 Designing a Program ���������������������������������������������������������������������������������������������������� 145 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 145 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 145 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 145 Summary ���������������������������������������������������������������������������������������������������������������������� 149 ■ Table of ConTenTs
📄 Page
9
ix ■ Chapter 4: Loops 151 How Loops Work ����������������������������������������������������������������������������������������������������������� 151 Introducing the Increment and Decrement Operators �������������������������������������������������� 152 The for Loop ����������������������������������������������������������������������������������������������������������������� 153 General Form of the for Loop ��������������������������������������������������������������������������������������� 157 More on the Increment and Decrement Operators ������������������������������������������������������� 158 The Increment Operator ���������������������������������������������������������������������������������������������������������������������� 158 The Prefix and Postfix Forms of the Increment Operator ������������������������������������������������������������������� 158 The Decrement Operator �������������������������������������������������������������������������������������������������������������������� 159 The for Loop Revisited �������������������������������������������������������������������������������������������������� 160 Modifying the for Loop Control Variable ��������������������������������������������������������������������������������������������� 162 A for Loop with No Parameters ����������������������������������������������������������������������������������������������������������� 163 The break Statement in a Loop����������������������������������������������������������������������������������������������������������� 163 Limiting Input Using a for Loop ����������������������������������������������������������������������������������������������������������� 166 Generating Pseudo-random Integers �������������������������������������������������������������������������������������������������� 168 More for Loop Control Options ������������������������������������������������������������������������������������������������������������ 171 Floating-Point Loop Control Variables������������������������������������������������������������������������������������������������� 171 Chars loop Control Variables ��������������������������������������������������������������������������������������������������������������� 172 The while Loop ������������������������������������������������������������������������������������������������������������� 172 Nested Loops ���������������������������������������������������������������������������������������������������������������� 175 Nested Loops and the goto Statement ������������������������������������������������������������������������� 180 The do-while Loop �������������������������������������������������������������������������������������������������������� 181 The continue Statement ����������������������������������������������������������������������������������������������� 184 Designing a Program ���������������������������������������������������������������������������������������������������� 185 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 185 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 185 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 186 Summary ���������������������������������������������������������������������������������������������������������������������� 198 ■ Table of ConTenTs
📄 Page
10
x ■ Chapter 5: Arrays 201 An Introduction to Arrays ���������������������������������������������������������������������������������������������� 201 Programming Without Arrays �������������������������������������������������������������������������������������������������������������� 201 What Is an Array? ������������������������������������������������������������������������������������������������������������������������������� 203 Using an Array ������������������������������������������������������������������������������������������������������������������������������������ 204 The Address of Operator ����������������������������������������������������������������������������������������������� 207 Arrays and Addresses ��������������������������������������������������������������������������������������������������� 210 Initializing an Array ������������������������������������������������������������������������������������������������������� 212 Compound Literal ������������������������������������������������������������������������������������������������������������������������������� 212 Finding the Size of an Array ����������������������������������������������������������������������������������������� 214 Multidimensional Arrays ����������������������������������������������������������������������������������������������� 215 Initializing Multidimensional Arrays ����������������������������������������������������������������������������� 217 Constant Arrays ������������������������������������������������������������������������������������������������������������ 223 Variable-Length Arrays ������������������������������������������������������������������������������������������������� 225 Designing a Program ���������������������������������������������������������������������������������������������������� 228 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 228 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 228 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 230 Summary ���������������������������������������������������������������������������������������������������������������������� 235 ■ Chapter 6: Applications with Strings and Text 237 What Is a String? ���������������������������������������������������������������������������������������������������������� 237 Variables That Store Strings ����������������������������������������������������������������������������������������� 239 Arrays of Strings ����������������������������������������������������������������������������������������������������������� 242 Operations with Strings ������������������������������������������������������������������������������������������������ 244 Checking for C11/C23 Support ����������������������������������������������������������������������������������������������������������� 245 Finding the Length of a String ������������������������������������������������������������������������������������������������������������ 246 Copying Strings ���������������������������������������������������������������������������������������������������������������������������������� 247 Concatenating Strings ������������������������������������������������������������������������������������������������������������������������ 249 Comparing Strings ������������������������������������������������������������������������������������������������������������������������������ 253 Searching a String ������������������������������������������������������������������������������������������������������������������������������ 257 ■ Table of ConTenTs
📄 Page
11
xi Tokenizing a String ����������������������������������������������������������������������������������������������������������������������������� 262 Reading Newline Characters into a String ������������������������������������������������������������������������������������������ 267 Analyzing and Transforming Strings ����������������������������������������������������������������������������� 268 Converting Character Case ����������������������������������������������������������������������������������������������������������������� 271 Converting Strings to Numerical Values ��������������������������������������������������������������������������������������������� 273 Designing a Program ���������������������������������������������������������������������������������������������������� 276 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 276 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 276 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 276 Summary ���������������������������������������������������������������������������������������������������������������������� 282 ■ Chapter 7: Pointers 283 A First Look at Pointers ������������������������������������������������������������������������������������������������ 283 Declaring Pointers ������������������������������������������������������������������������������������������������������������������������������ 284 Accessing a Value Through a Pointer ������������������������������������������������������������������������������������������������� 286 Using Pointers ������������������������������������������������������������������������������������������������������������������������������������ 289 Testing for a NULL Pointer ������������������������������������������������������������������������������������������������������������������ 293 Pointers to Constants ������������������������������������������������������������������������������������������������������������������������� 295 Constant Pointers ������������������������������������������������������������������������������������������������������������������������������� 296 Naming Pointers ��������������������������������������������������������������������������������������������������������������������������������� 296 Arrays and Pointers ������������������������������������������������������������������������������������������������������ 297 Multidimensional Arrays ����������������������������������������������������������������������������������������������� 300 Multidimensional Arrays and Pointers ������������������������������������������������������������������������������������������������ 304 Accessing Array Elements ������������������������������������������������������������������������������������������������������������������ 306 Using Memory As You Go ���������������������������������������������������������������������������������������������� 309 Dynamic Memory Allocation: The malloc() Function ��������������������������������������������������������������������������� 309 Releasing Dynamically Allocated Memory ������������������������������������������������������������������������������������������ 310 Memory Allocation with the calloc() Function ������������������������������������������������������������������������������������ 315 Extending Dynamically Allocated Memory������������������������������������������������������������������������������������������ 316 Handling Strings Using Pointers ����������������������������������������������������������������������������������� 320 Using Arrays of Pointers ��������������������������������������������������������������������������������������������������������������������� 321 Pointers and Array Notation ���������������������������������������������������������������������������������������������������������������� 327 ■ Table of ConTenTs
📄 Page
12
xii Designing a Program ���������������������������������������������������������������������������������������������������� 332 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 332 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 332 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 333 The Complete Program ����������������������������������������������������������������������������������������������������������������������� 338 Summary ���������������������������������������������������������������������������������������������������������������������� 340 ■ Chapter 8: Structuring Your Programs 343 Program Structure �������������������������������������������������������������������������������������������������������� 343 Variable Scope and Lifetime ��������������������������������������������������������������������������������������������������������������� 344 Variable Scope and Functions ������������������������������������������������������������������������������������������������������������ 347 Storage classes ���������������������������������������������������������������������������������������������������������������������������������� 348 Functions ���������������������������������������������������������������������������������������������������������������������� 349 Defining a Function����������������������������������������������������������������������������������������������������������������������������� 349 The return Statement ������������������������������������������������������������������������������������������������������������������������� 354 The Pass-by-Value Mechanism ������������������������������������������������������������������������������������ 359 Function Prototypes ����������������������������������������������������������������������������������������������������� 360 Pointers As Parameters and Return Types ������������������������������������������������������������������� 362 const Parameters ������������������������������������������������������������������������������������������������������������������������������� 362 Perils of Returning Pointers ���������������������������������������������������������������������������������������������������������������� 369 Summary ���������������������������������������������������������������������������������������������������������������������� 372 ■ Chapter 9: More on Functions 375 Pointers to Functions ��������������������������������������������������������������������������������������������������� 375 Declaring a Pointer to a Function ������������������������������������������������������������������������������������������������������� 375 Calling a Function Through a Function Pointer ����������������������������������������������������������������������������������� 376 Arrays of Pointers to Functions ���������������������������������������������������������������������������������������������������������� 379 Pointers to Functions As Arguments ��������������������������������������������������������������������������������������������������� 382 Variables in Functions �������������������������������������������������������������������������������������������������� 384 Static Variables: Keeping Track Within a Function ������������������������������������������������������������������������������ 385 Sharing Variables Between Functions ������������������������������������������������������������������������������������������������ 387 ■ Table of ConTenTs
📄 Page
13
xiii Functions That Call Themselves: Recursion ����������������������������������������������������������������� 389 Functions with a Variable Number of Arguments ��������������������������������������������������������� 394 Copying a va_list �������������������������������������������������������������������������������������������������������������������������������� 397 Basic Rules for Variable-Length Argument Lists �������������������������������������������������������������������������������� 398 The main( ) Function ����������������������������������������������������������������������������������������������������� 398 Terminating a Program ������������������������������������������������������������������������������������������������� 400 The abort() Function ��������������������������������������������������������������������������������������������������������������������������� 400 The exit() and atexit() Functions ��������������������������������������������������������������������������������������������������������� 401 The _Exit() Function ���������������������������������������������������������������������������������������������������������������������������� 401 The quick_exit() and at_quick_exit() Functions ���������������������������������������������������������������������������������� 401 Enhancing Performance ����������������������������������������������������������������������������������������������� 402 Declaring Functions Inline ������������������������������������������������������������������������������������������������������������������ 402 Using the restrict Keyword ����������������������������������������������������������������������������������������������������������������� 402 The _Noreturn Function Specifier������������������������������������������������������������������������������������������������������� 403 Attributes �������������������������������������������������������������������������������������������������������������������������������������������� 403 Designing a Program ���������������������������������������������������������������������������������������������������� 407 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 407 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 408 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 410 Summary ���������������������������������������������������������������������������������������������������������������������� 423 ■ Chapter 10: Essential Input and Output 425 Input and Output Streams �������������������������������������������������������������������������������������������� 425 Standard Streams �������������������������������������������������������������������������������������������������������� 426 Input from the Keyboard ����������������������������������������������������������������������������������������������� 427 Formatted Keyboard Input ������������������������������������������������������������������������������������������������������������������ 427 Input Format Control Strings �������������������������������������������������������������������������������������������������������������� 428 Characters in the Input Format String ������������������������������������������������������������������������������������������������ 434 Variations on Floating-Point Input ������������������������������������������������������������������������������������������������������ 436 Reading Hexadecimal and Octal Values ��������������������������������������������������������������������������������������������� 437 ■ Table of ConTenTs
📄 Page
14
xiv Reading Characters Using scanf_s() �������������������������������������������������������������������������������������������������� 439 String Input from the Keyboard ���������������������������������������������������������������������������������������������������������� 441 Single-Character Keyboard Input ������������������������������������������������������������������������������������������������������� 442 Output to the Screen ���������������������������������������������������������������������������������������������������� 447 Formatted Output Using printf_s() ������������������������������������������������������������������������������������������������������ 447 Escape Sequences ����������������������������������������������������������������������������������������������������������������������������� 450 Integer Output ������������������������������������������������������������������������������������������������������������������������������������� 451 Outputting Floating-Point Values �������������������������������������������������������������������������������������������������������� 454 Character Output �������������������������������������������������������������������������������������������������������������������������������� 455 Other Output Functions ������������������������������������������������������������������������������������������������ 457 Unformatted Output to the Screen ������������������������������������������������������������������������������������������������������ 457 Formatted Output to an Array ������������������������������������������������������������������������������������������������������������� 458 Formatted Input from an Array ����������������������������������������������������������������������������������������������������������� 459 Summary ���������������������������������������������������������������������������������������������������������������������� 459 ■ Chapter 11: Structuring Data 461 Data Structures: Using struct ��������������������������������������������������������������������������������������� 461 Defining Structure Types and Structure Variables ������������������������������������������������������������������������������ 463 Accessing Structure Members ����������������������������������������������������������������������������������������������������������� 464 Unnamed Structures ��������������������������������������������������������������������������������������������������������������������������� 467 Arrays of Structures ���������������������������������������������������������������������������������������������������������������������������� 468 Structure Members in Expressions����������������������������������������������������������������������������������������������������� 470 Pointers to Structures ������������������������������������������������������������������������������������������������������������������������� 471 Dynamic Memory Allocation for Structures ���������������������������������������������������������������������������������������� 472 More on Structure Members ���������������������������������������������������������������������������������������� 475 Structures As Members of a Structure ����������������������������������������������������������������������������������������������� 475 Declaring a Structure Within a Structure �������������������������������������������������������������������������������������������� 476 Pointers to Structures As Structure Members ������������������������������������������������������������������������������������ 477 Doubly Linked Lists ���������������������������������������������������������������������������������������������������������������������������� 482 Bit Fields in a Structure ���������������������������������������������������������������������������������������������������������������������� 485 ■ Table of ConTenTs
📄 Page
15
xv Structures and Functions ��������������������������������������������������������������������������������������������� 487 Structures As Arguments to Functions ����������������������������������������������������������������������������������������������� 487 Pointers to Structures As Function Arguments ����������������������������������������������������������������������������������� 488 Structure As a Function Return Value ������������������������������������������������������������������������������������������������� 489 Binary Trees ���������������������������������������������������������������������������������������������������������������������������������������� 494 Sharing Memory ����������������������������������������������������������������������������������������������������������� 502 Designing a Program ���������������������������������������������������������������������������������������������������� 507 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 507 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 507 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 508 Summary ���������������������������������������������������������������������������������������������������������������������� 520 ■ Chapter 12: Working with Files 521 The Concept of a File ���������������������������������������������������������������������������������������������������� 521 Positions in a File ������������������������������������������������������������������������������������������������������������������������������� 522 File Streams���������������������������������������������������������������������������������������������������������������������������������������� 522 Accessing Files ������������������������������������������������������������������������������������������������������������ 523 Opening a File ������������������������������������������������������������������������������������������������������������������������������������� 523 Buffering File Operations �������������������������������������������������������������������������������������������������������������������� 526 Renaming a File ���������������������������������������������������������������������������������������������������������������������������������� 527 Closing a File �������������������������������������������������������������������������������������������������������������������������������������� 527 Deleting a File ������������������������������������������������������������������������������������������������������������������������������������� 528 Writing a Text File ��������������������������������������������������������������������������������������������������������� 528 Reading a Text File ������������������������������������������������������������������������������������������������������� 530 Reading and Writing Strings to a Text File �������������������������������������������������������������������� 533 Formatted File Input and Output ���������������������������������������������������������������������������������� 537 Formatted Output to a File ������������������������������������������������������������������������������������������������������������������ 538 Formatted Input from a File ���������������������������������������������������������������������������������������������������������������� 538 Dealing with Errors ������������������������������������������������������������������������������������������������������� 541 More Open Modes for Text Files ����������������������������������������������������������������������������������� 542 ■ Table of ConTenTs
📄 Page
16
xvi The freopen_s( ) Function ��������������������������������������������������������������������������������������������� 543 Binary File Input and Output ����������������������������������������������������������������������������������������� 544 Opening a File in Binary Mode ������������������������������������������������������������������������������������������������������������ 545 Writing a Binary File ��������������������������������������������������������������������������������������������������������������������������� 546 Reading a Binary File �������������������������������������������������������������������������������������������������������������������������� 546 Moving Around in a File ������������������������������������������������������������������������������������������������ 553 File Positioning Operations ����������������������������������������������������������������������������������������������������������������� 553 Finding Out Where You Are ����������������������������������������������������������������������������������������������������������������� 554 Setting a Position in a File ������������������������������������������������������������������������������������������������������������������ 554 Using Temporary Work Files ����������������������������������������������������������������������������������������� 561 Creating a Temporary Work File ���������������������������������������������������������������������������������������������������������� 562 Creating a Unique File Name �������������������������������������������������������������������������������������������������������������� 562 Updating Binary Files ��������������������������������������������������������������������������������������������������� 564 Changing the Contents of a File ���������������������������������������������������������������������������������������������������������� 569 Creating a Record from Keyboard Input ��������������������������������������������������������������������������������������������� 570 Writing a Record to a File ������������������������������������������������������������������������������������������������������������������� 571 Reading a Record from a File ������������������������������������������������������������������������������������������������������������� 572 Writing a File �������������������������������������������������������������������������������������������������������������������������������������� 572 Listing the File Contents ��������������������������������������������������������������������������������������������������������������������� 573 Updating the Existing File Contents ���������������������������������������������������������������������������������������������������� 574 File Open Mode Summary �������������������������������������������������������������������������������������������� 581 Designing a Program ���������������������������������������������������������������������������������������������������� 582 The Problem ��������������������������������������������������������������������������������������������������������������������������������������� 582 The Analysis ���������������������������������������������������������������������������������������������������������������������������������������� 582 The Solution ���������������������������������������������������������������������������������������������������������������������������������������� 582 Summary ���������������������������������������������������������������������������������������������������������������������� 587 ■ Chapter 13: The Preprocessor and Debugging 589 Preprocessing �������������������������������������������������������������������������������������������������������������� 589 Including Header Files ������������������������������������������������������������������������������������������������������������������������ 589 Defining Your Own Header Files ��������������������������������������������������������������������������������������������������������� 590 Managing Multiple Source Files ��������������������������������������������������������������������������������������������������������� 591 ■ Table of ConTenTs
📄 Page
17
xvii External Variables ������������������������������������������������������������������������������������������������������������������������������� 591 Static Functions ���������������������������������������������������������������������������������������������������������������������������������� 592 Substitutions in Your Program Source Code ��������������������������������������������������������������������������������������� 592 Macros ������������������������������������������������������������������������������������������������������������������������� 593 Macros That Look Like Functions ������������������������������������������������������������������������������������������������������� 594 Strings As Macro Arguments �������������������������������������������������������������������������������������������������������������� 595 Joining Two Arguments in a Macro Expansion ����������������������������������������������������������������������������������� 597 Preprocessor Directives on Multiple Lines ������������������������������������������������������������������� 597 Logical Preprocessor Directives ��������������������������������������������������������������������������������������������������������� 597 Conditional Compilation ���������������������������������������������������������������������������������������������������������������������� 598 Testing for Multiple Conditions ����������������������������������������������������������������������������������������������������������� 599 Undefining Identifiers ������������������������������������������������������������������������������������������������������������������������� 599 Testing for Specific Values for Identifiers ������������������������������������������������������������������������������������������� 599 Multiple-Choice Selections ����������������������������������������������������������������������������������������������������������������� 600 Standard Preprocessing Macros �������������������������������������������������������������������������������������������������������� 602 _Generic Macro ���������������������������������������������������������������������������������������������������������������������������������� 603 The Unreachable Macro ����������������������������������������������������������������������������������������������� 604 Debugging Methods ����������������������������������������������������������������������������������������������������� 605 Integrated Debuggers ������������������������������������������������������������������������������������������������������������������������� 605 The Preprocessor in Debugging ��������������������������������������������������������������������������������������������������������� 606 Assertions ������������������������������������������������������������������������������������������������������������������������������������������� 610 Date and Time Functions ���������������������������������������������������������������������������������������������� 613 Getting Time Values ���������������������������������������������������������������������������������������������������������������������������� 613 Getting the Date ���������������������������������������������������������������������������������������������������������������������������������� 616 Getting the Day for a Date ������������������������������������������������������������������������������������������������������������������ 622 Summary ���������������������������������������������������������������������������������������������������������������������� 625 ■ Chapter 14: Advanced and Specialized Topics 627 Working with International Character Sets ������������������������������������������������������������������ 627 Understanding Unicode ���������������������������������������������������������������������������������������������������������������������� 627 Setting the Locale ������������������������������������������������������������������������������������������������������������������������������� 628 The Wide Character Type wchar_t ������������������������������������������������������������������������������������������������������ 629 ■ Table of ConTenTs
📄 Page
18
xviii Operations on Wide Character Strings ������������������������������������������������������������������������������������������������ 632 File Stream Operations with Wide Characters ������������������������������������������������������������������������������������ 636 Fixed Size Types That Store Unicode Characters �������������������������������������������������������������������������������� 637 Specialized Integer Types for Portability ���������������������������������������������������������������������� 641 Fixed-Width Integer Types ������������������������������������������������������������������������������������������������������������������ 641 Minimum-Width Integer Types ������������������������������������������������������������������������������������������������������������ 642 Maximum-Width Integer Types ����������������������������������������������������������������������������������������������������������� 642 The Complex Number Types ����������������������������������������������������������������������������������������� 643 Complex Number Basics��������������������������������������������������������������������������������������������������������������������� 643 Complex Types and Operations ����������������������������������������������������������������������������������������������������������� 644 Programming with Threads ������������������������������������������������������������������������������������������ 648 Creating a Thread ������������������������������������������������������������������������������������������������������������������������������� 648 Exiting a Thread ���������������������������������������������������������������������������������������������������������������������������������� 649 Joining One Thread to Another ����������������������������������������������������������������������������������������������������������� 650 Suspending a Thread �������������������������������������������������������������������������������������������������������������������������� 653 Managing Thread Access to Data ������������������������������������������������������������������������������������������������������� 654 Summary ���������������������������������������������������������������������������������������������������������������������� 661 ■ Appendix A: Computer Arithmetic 663 ■ Appendix B: ASCII Character Code Definitions 673 ■ Appendix C: Reserved Words in C 679 ■ Appendix D: Input and Output Format Specifications 681 ■ Appendix E: Standard Library Header Files 689 ■ Index 693 ■ Table of ConTenTs
📄 Page
19
xix About the Authors German Gonzalez-Morris is a polyglot software architect/engineer with 20+ years in the field, with knowledge in Java, SpringBoot, C/C++, Julia, Python, Haskell, and JavaScript, among others. He works for cloud (architecture), web-distributed applications, and microservices. German loves math puzzles (including reading Knuth, proud of solving some of Don's puzzles), swimming, and table tennis. Also, he has reviewed several books, including those on application containers (WebLogic) and languages (C, Java, Spring, Python, Haskell, TypeScript, WebAssembly, Math for coders, regexp, Julia, data structures, and algorithms). You can see details on his blog (https://devwebcl.blogspot.com/) or X/Twitter account (@devwebcl). Ivor Horton is self-employed in consultancy and writes programming tutorials. He worked for IBM for many years and holds a bachelor's degree, with honors, in mathematics. Ivor’s experience at IBM includes programming in most languages (such as assembler and high-level languages on a variety of machines), real-time programming, and designing and implementing real-time closed-loop industrial control systems. He has extensive experience teaching programming to engineers and scientists (Fortran, PL/1, APL, etc.). Ivor is an expert in mechanical, process, and electronic CAD systems; mechanical CAM systems; and DNC/CNC systems.
📄 Page
20
xxi Megan J. Hirni is currently teaching and pursuing her PhD at the University of Missouri-Columbia with a focus on applied statistical research. In addition to her love for coding, Megan loves meeting new people and exploring new areas in multifaceted fields. About the Technical Reviewer
The above is a preview of the first 20 pages. Register to read the complete e-book.