C++26 for Lazy Programmers Quick, Easy, and Fun C++ for Beginners — Third Edition — Will Briggs
C++26 for Lazy Programmers Quick, Easy, and Fun C++ for Beginners Third Edition Will Briggs
C++26 for Lazy Programmers: Quick, Easy, and Fun C++ for Beginners, Third Edition ISBN-13 (pbk): 979-8-8688-1858-5 ISBN-13 (electronic): 979-8-8688-1859-2 https://doi.org/10.1007/979-8-8688-1859-2 Copyright © 2025 by Will Briggs 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 Coordinating Editor: Gryffin Winkler Cover designed by eStudioCalamar Cover image by Freepik.com Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. 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 Delaware 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 (https://github.com/Apress). For more detailed information, please visit https://www.apress.com/ gp/services/source-code. If disposing of this product, please recycle the paper Will Briggs Lynchburg, VA, USA
To Addie: spinner of tales, both great and small.
v About the Author ���������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Acknowledgments ��������������������������������������������������������������������������������������������������xxi Introduction ����������������������������������������������������������������������������������������������������������xxiii Chapter 1: Getting Started ���������������������������������������������������������������������������������������� 1 Initial Setup ����������������������������������������������������������������������������������������������������������������������������������� 1 …in Microsoft Visual Studio ���������������������������������������������������������������������������������������������������� 2 …with MSys2 �������������������������������������������������������������������������������������������������������������������������� 4 …in Unix ��������������������������������������������������������������������������������������������������������������������������������� 4 A Simple Program ������������������������������������������������������������������������������������������������������������������������� 5 Spacing ������������������������������������������������������������������������������������������������������������������������������������ 8 Creating an SSDL Project ������������������������������������������������������������������������������������������������������������ 10 …with g++ (Unix and MSys2) ����������������������������������������������������������������������������������������������� 10 …in Microsoft Visual Studio �������������������������������������������������������������������������������������������������� 13 How Not to Be Miserable (Whatever Your Platform) �������������������������������������������������������������� 22 Shapes and the Functions That Draw Them ������������������������������������������������������������������������������� 24 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 32 consts and Colors ����������������������������������������������������������������������������������������������������������������������� 33 Text ��������������������������������������������������������������������������������������������������������������������������������������������� 37 sout, Escape Sequences, and Fonts �������������������������������������������������������������������������������������� 37 SSDL_RenderText, SSDL_RenderTextCentered ��������������������������������������������������������������������� 42 Table of Contents
vi Chapter 2: Images and Sound �������������������������������������������������������������������������������� 47 Images and Changing Window Characteristics ��������������������������������������������������������������������������� 47 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 53 Transparency ������������������������������������������������������������������������������������������������������������������������������� 55 Sound ������������������������������������������������������������������������������������������������������������������������������������������ 58 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 60 Chapter 3: Numbers ������������������������������������������������������������������������������������������������ 61 Variables ������������������������������������������������������������������������������������������������������������������������������������� 61 Constants ������������������������������������������������������������������������������������������������������������������������������������ 64 When to Use Constants, Not Literal Values ���������������������������������������������������������������������������� 64 Math Operators ��������������������������������������������������������������������������������������������������������������������������� 65 Integer Division ���������������������������������������������������������������������������������������������������������������������� 65 Assignment (=) Operators ����������������������������������������������������������������������������������������������������� 66 A Diving Board Example �������������������������������������������������������������������������������������������������������� 66 The No-Worries List for Math Operators �������������������������������������������������������������������������������� 70 Built-In Functions and Casting ���������������������������������������������������������������������������������������������������� 71 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 76 Chapter 4: Mouse and if ����������������������������������������������������������������������������������������� 79 Mouse Functions ������������������������������������������������������������������������������������������������������������������������� 79 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 82 if �������������������������������������������������������������������������������������������������������������������������������������������������� 83 Coercion and if Conditions (if’s Dirty Little Secret) ���������������������������������������������������������������� 86 Combining Conditions with &&, ||, and ! �������������������������������������������������������������������������������� 86 Antibugging ��������������������������������������������������������������������������������������������������������������������������� 87 Boolean Values and Variables ����������������������������������������������������������������������������������������������������� 90 A Hidden-Object Game ���������������������������������������������������������������������������������������������������������������� 92 Chapter 5: Loops, Input, and char ��������������������������������������������������������������������������� 99 Keyboard Input ���������������������������������������������������������������������������������������������������������������������������� 99 Antibugging ������������������������������������������������������������������������������������������������������������������������� 101 while and do-while ������������������������������������������������������������������������������������������������������������������� 103 Table of ConTenTs
vii Loops with SSDL������������������������������������������������������������������������������������������������������������������ 104 break and continue �������������������������������������������������������������������������������������������������������������� 106 Antibugging ������������������������������������������������������������������������������������������������������������������������� 106 For Loops ���������������������������������������������������������������������������������������������������������������������������������� 109 Increment Operators ������������������������������������������������������������������������������������������������������������ 110 An Example: Averaging Numbers ���������������������������������������������������������������������������������������� 111 Antibugging ������������������������������������������������������������������������������������������������������������������������� 113 chars and cctype ����������������������������������������������������������������������������������������������������������������������� 115 Antibugging ������������������������������������������������������������������������������������������������������������������������� 118 Chapter 6: Algorithms and the Development Process ������������������������������������������ 121 Adventures in Robotic Cooking ������������������������������������������������������������������������������������������������� 121 Writing a Program, from Start to Finish ������������������������������������������������������������������������������������ 125 Requirements: What Do We Want to Do? ����������������������������������������������������������������������������� 125 Algorithm: How Do We Do It? ����������������������������������������������������������������������������������������������� 126 Trace the Algorithm: Will It Work? ���������������������������������������������������������������������������������������� 128 Coding: Putting It All Into C++ (Plus: Commenting the Lazy Way) ��������������������������������������� 128 Chapter 7: Functions �������������������������������������������������������������������������������������������� 135 Functions That Return Values ��������������������������������������������������������������������������������������������������� 135 Functions That Return Nothing ������������������������������������������������������������������������������������������������� 142 Global Variables ������������������������������������������������������������������������������������������������������������������������ 146 Antibugging ������������������������������������������������������������������������������������������������������������������������� 148 How to Write a Function in Four Easy Steps (and Call It in One) ����������������������������������������������� 150 Antibugging ������������������������������������������������������������������������������������������������������������������������� 153 Why Have Functions, Anyway?�������������������������������������������������������������������������������������������������� 155 Recap ���������������������������������������������������������������������������������������������������������������������������������������� 164 Chapter 8: Functions, continued ��������������������������������������������������������������������������� 167 Random Numbers ��������������������������������������������������������������������������������������������������������������������� 167 Making a Random Number Generator ��������������������������������������������������������������������������������� 167 Using the Built-In Random Number Generator �������������������������������������������������������������������� 170 Antibugging ������������������������������������������������������������������������������������������������������������������������� 173 Table of ConTenTs
viii Boolean Functions �������������������������������������������������������������������������������������������������������������������� 175 & Parameters ���������������������������������������������������������������������������������������������������������������������������� 177 Antibugging ������������������������������������������������������������������������������������������������������������������������� 181 Identifier Scope ������������������������������������������������������������������������������������������������������������������������� 182 A Final Note on Algorithms �������������������������������������������������������������������������������������������������������� 184 Chapter 9: Using the Debugger ����������������������������������������������������������������������������� 185 Breakpoints and Watched Variables ������������������������������������������������������������������������������������������ 190 ddd �������������������������������������������������������������������������������������������������������������������������������������� 190 gdb �������������������������������������������������������������������������������������������������������������������������������������� 191 Visual Studio ������������������������������������������������������������������������������������������������������������������������ 191 Fixing the Stripes ���������������������������������������������������������������������������������������������������������������������� 193 Going Into Functions ����������������������������������������������������������������������������������������������������������������� 193 ddd �������������������������������������������������������������������������������������������������������������������������������������� 193 gdb �������������������������������������������������������������������������������������������������������������������������������������� 194 Visual Studio ������������������������������������������������������������������������������������������������������������������������ 195 Fixing the Stars ������������������������������������������������������������������������������������������������������������������������� 196 Wrap-Up ������������������������������������������������������������������������������������������������������������������������������������ 197 Antibugging ������������������������������������������������������������������������������������������������������������������������� 197 Bottom-Up Testing �������������������������������������������������������������������������������������������������������������������� 198 More on Antibugging ����������������������������������������������������������������������������������������������������������������� 199 Chapter 10: Arrays, Spans, and enum ������������������������������������������������������������������ 203 Arrays ���������������������������������������������������������������������������������������������������������������������������������������� 203 Arrays’ Dirty Little Secret: Using Memory Addresses ���������������������������������������������������������� 206 Antibugging ������������������������������������������������������������������������������������������������������������������������� 207 Arrays as Function Parameters, Spans, and Range-Based For ������������������������������������������������� 208 Making Your Functions Work for Different Array Sizes with Spans (Plus: Easier For Loops) ����������������������������������������������������������������������������������������������������������������� 209 Antibugging ������������������������������������������������������������������������������������������������������������������������� 212 Enumeration Types, and switch ������������������������������������������������������������������������������������������������ 213 Antibugging ������������������������������������������������������������������������������������������������������������������������� 217 Table of ConTenTs
ix Multidimensional Arrays (optional) ������������������������������������������������������������������������������������������� 218 Displaying the Board ����������������������������������������������������������������������������������������������������������� 219 Arrays of More Than Two Dimensions ��������������������������������������������������������������������������������� 224 Antibugging ������������������������������������������������������������������������������������������������������������������������� 224 Chapter 11: Animation with structs and Sprites �������������������������������������������������� 227 structs ��������������������������������������������������������������������������������������������������������������������������������������� 227 Making a Movie with struct and while �������������������������������������������������������������������������������������� 232 Sprites ��������������������������������������������������������������������������������������������������������������������������������������� 240 Antibugging ������������������������������������������������������������������������������������������������������������������������� 245 Chapter 12: Making an Arcade Game: Input, Collisions, and Putting It All Together ���������������������������������������������������������������������������������������������������������������� 247 Determining Input States ���������������������������������������������������������������������������������������������������������� 247 Mouse ���������������������������������������������������������������������������������������������������������������������������������� 247 Keyboard ����������������������������������������������������������������������������������������������������������������������������� 248 Antibugging ������������������������������������������������������������������������������������������������������������������������� 250 Events ��������������������������������������������������������������������������������������������������������������������������������������� 250 Cooldowns and Lifetimes ���������������������������������������������������������������������������������������������������������� 252 Collisions ����������������������������������������������������������������������������������������������������������������������������������� 256 The Big Game ���������������������������������������������������������������������������������������������������������������������������� 257 Antibugging ������������������������������������������������������������������������������������������������������������������������� 271 Chapter 13: Standard I/O and File Operations ������������������������������������������������������ 277 Standard I/O Programs �������������������������������������������������������������������������������������������������������������� 277 Compiling Standard I/O Programs ��������������������������������������������������������������������������������������� 279 Building a Project from Scratch … (optional) ��������������������������������������������������������������������� 279 File I/O (optional) ����������������������������������������������������������������������������������������������������������������������� 284 cin and cout as Files ������������������������������������������������������������������������������������������������������������ 284 Using Filenames ������������������������������������������������������������������������������������������������������������������ 291 Table of ConTenTs
x Chapter 14: Character Arrays and Dynamic Memory ������������������������������������������� 299 Character Arrays ����������������������������������������������������������������������������������������������������������������������� 299 Antibugging ������������������������������������������������������������������������������������������������������������������������� 303 Dynamic Allocation of Arrays ���������������������������������������������������������������������������������������������������� 305 Antibugging ������������������������������������������������������������������������������������������������������������������������� 309 Using the * Notation ������������������������������������������������������������������������������������������������������������������ 312 Antibugging ������������������������������������������������������������������������������������������������������������������������� 315 Chapter 15: Classes ���������������������������������������������������������������������������������������������� 319 Constructors ������������������������������������������������������������������������������������������������������������������������������ 322 Antibugging ������������������������������������������������������������������������������������������������������������������������� 326 const Objects, const Member Functions … ������������������������������������������������������������������������������ 328 Antibugging ������������������������������������������������������������������������������������������������������������������������� 329 …and const Parameters ����������������������������������������������������������������������������������������������������������� 329 Multiple Constructors ���������������������������������������������������������������������������������������������������������������� 330 Copy Constructors ��������������������������������������������������������������������������������������������������������������� 331 Default Constructors ������������������������������������������������������������������������������������������������������������ 332 Conversion Constructors ����������������������������������������������������������������������������������������������������� 332 Summary ����������������������������������������������������������������������������������������������������������������������������� 333 Antibugging ������������������������������������������������������������������������������������������������������������������������� 334 Default Parameters for Code Reuse ������������������������������������������������������������������������������������������ 335 Date Program (So Far)��������������������������������������������������������������������������������������������������������������� 336 Chapter 16: Classes, continued ���������������������������������������������������������������������������� 341 inline Functions for Efficiency ��������������������������������������������������������������������������������������������������� 341 Access Functions ���������������������������������������������������������������������������������������������������������������������� 343 Separate Compilation and Include Files ������������������������������������������������������������������������������������ 344 What Happens in Separate Compilation ������������������������������������������������������������������������������ 345 Writing Your �h File �������������������������������������������������������������������������������������������������������������� 346 Backing Up a Multi-file Project �������������������������������������������������������������������������������������������� 349 Antibugging ������������������������������������������������������������������������������������������������������������������������� 349 Table of ConTenTs
xi Multiple-File Projects in Microsoft Visual Studio ���������������������������������������������������������������������� 351 Multiple-File Projects in g++ ���������������������������������������������������������������������������������������������������� 352 Command Line: More Typing, Less Thinking ������������������������������������������������������������������������ 352 Makefiles: More Thinking, Less Typing (optional) ���������������������������������������������������������������� 353 Antibugging ������������������������������������������������������������������������������������������������������������������������� 357 Final Date Program ������������������������������������������������������������������������������������������������������������������� 357 Chapter 17: Strings, and Operators ���������������������������������������������������������������������� 365 The Basic String Class �������������������������������������������������������������������������������������������������������������� 365 Destructors ������������������������������������������������������������������������������������������������������������������������������� 367 Comparison Operators �������������������������������������������������������������������������������������������������������������� 368 Assignment Operators and *this ����������������������������������������������������������������������������������������������� 369 Unary Operators ������������������������������������������������������������������������������������������������������������������������ 372 Antibugging ������������������������������������������������������������������������������������������������������������������������� 373 Arithmetic Operators ����������������������������������������������������������������������������������������������������������������� 373 ++ and -- ���������������������������������������������������������������������������������������������������������������������������������� 376 operator[] ���������������������������������������������������������������������������������������������������������������������������������� 378 >> and << �������������������������������������������������������������������������������������������������������������������������������� 379 static Members ������������������������������������������������������������������������������������������������������������������������� 381 Temporary Object Creation ������������������������������������������������������������������������������������������������������� 382 Final String Program ����������������������������������������������������������������������������������������������������������������� 384 std::string ���������������������������������������������������������������������������������������������������������������������������������� 389 Chapter 18: String Views, Exceptions, Move Semantics, and O Notation ������������� 391 std::string_view (optional) �������������������������������������������������������������������������������������������������������� 391 Exceptions ��������������������������������������������������������������������������������������������������������������������������������� 393 Move Constructors and Move = ������������������������������������������������������������������������������������������������ 397 Efficiency and O Notation ���������������������������������������������������������������������������������������������������������� 400 Chapter 19: Templates, Including vector �������������������������������������������������������������� 405 Function Templates ������������������������������������������������������������������������������������������������������������������� 405 Antibugging ������������������������������������������������������������������������������������������������������������������������� 407 Table of ConTenTs
xii Concepts (optional) ������������������������������������������������������������������������������������������������������������������� 408 Antibugging ������������������������������������������������������������������������������������������������������������������������� 410 The Vector Class ������������������������������������������������������������������������������������������������������������������������ 411 Efficiency and O Notation ���������������������������������������������������������������������������������������������������� 416 Making Vector a Template ��������������������������������������������������������������������������������������������������������� 418 pair �������������������������������������������������������������������������������������������������������������������������������������������� 422 Antibugging ������������������������������������������������������������������������������������������������������������������������� 422 Making Your Own Concepts (optional) �������������������������������������������������������������������������������������� 423 Non-type Template Arguments �������������������������������������������������������������������������������������������������� 425 #include <vector> �������������������������������������������������������������������������������������������������������������������� 427 Chapter 20: Inheritance ���������������������������������������������������������������������������������������� 429 The Basics of Inheritance���������������������������������������������������������������������������������������������������������� 429 Constructors and Destructors, for Inheritance and Member Variables ������������������������������������� 433 Inheritance as a Concept ���������������������������������������������������������������������������������������������������������� 436 Classes for Card Games ������������������������������������������������������������������������������������������������������������ 438 An Inheritance Hierarchy ����������������������������������������������������������������������������������������������������� 441 private Inheritance �������������������������������������������������������������������������������������������������������������� 445 Hiding an Inherited Member Function ��������������������������������������������������������������������������������� 447 A Game of Montana ������������������������������������������������������������������������������������������������������������� 449 Chapter 21: Virtual Functions and Multiple Inheritance ��������������������������������������� 459 Virtual Functions ����������������������������������������������������������������������������������������������������������������������� 459 Behind the Scenes ��������������������������������������������������������������������������������������������������������������� 461 Pure Virtual Functions and Abstract Base Classes �������������������������������������������������������������� 462 Why Virtual Functions Often Mean Using Pointers �������������������������������������������������������������� 463 Virtual Destructors ��������������������������������������������������������������������������������������������������������������� 467 Antibugging ������������������������������������������������������������������������������������������������������������������������� 470 Inheritance, Class-Type Members, and Move Semantics ���������������������������������������������������������� 472 Multiple Inheritance (optional) �������������������������������������������������������������������������������������������������� 475 Antibugging ������������������������������������������������������������������������������������������������������������������������� 476 Table of ConTenTs
xiii Chapter 22: Linked Lists ��������������������������������������������������������������������������������������� 479 What Lists Are and Why Have Them ������������������������������������������������������������������������������������������ 479 Efficiency and O Notation ���������������������������������������������������������������������������������������������������������� 481 Starting the Linked List Template ��������������������������������������������������������������������������������������������� 482 List<T>::List() ��������������������������������������������������������������������������������������������������������������������������� 484 void List<T>::push_front ���������������������������������������������������������������������������������������������������������� 484 void List<T>::pop_front ������������������������������������������������������������������������������������������������������������ 486 List<T>::~List ��������������������������������������������������������������������������������������������������������������������������� 488 ->: A Bit of Syntactic Sugar ������������������������������������������������������������������������������������������������������ 489 More Friendly Syntax: Pointers as Conditions ��������������������������������������������������������������������������� 489 The Linked List Template ���������������������������������������������������������������������������������������������������������� 490 Antibugging ������������������������������������������������������������������������������������������������������������������������� 493 #include <list>�������������������������������������������������������������������������������������������������������������������������� 495 Chapter 23: The Standard Template Library and Functional-Style Programming �������������������������������������������������������������������������������������������������������� 497 Iterators������������������������������������������������������������������������������������������������������������������������������������� 497 …with vector Too ���������������������������������������������������������������������������������������������������������������� 500 const and reverse Iterators ������������������������������������������������������������������������������������������������� 501 Antibugging ������������������������������������������������������������������������������������������������������������������������� 503 Getting Really Lazy: Ranges and auto ��������������������������������������������������������������������������������������� 503 std::tuple ����������������������������������������������������������������������������������������������������������������������������������� 505 STL Functions Using Ranges and Views ����������������������������������������������������������������������������������� 508 A New Kind of Lazy: Lazy Evaluation and Infinite Ranges ��������������������������������������������������������� 511 Antibugging ������������������������������������������������������������������������������������������������������������������������� 512 Chapter 24: Functional-Style Programming, continued ��������������������������������������� 515 Lambda Functions for One-Time Use ���������������������������������������������������������������������������������������� 516 fold_left, fold_right ������������������������������������������������������������������������������������������������������������������� 519 Image Processing and Functional-Style Programming ������������������������������������������������������������� 521 std::expected and std::optional ������������������������������������������������������������������������������������������������� 528 Table of ConTenTs
xiv Chapter 25: Esoterica (Recommended) ���������������������������������������������������������������� 533 Formatted Output ���������������������������������������������������������������������������������������������������������������������� 533 Command-Line Arguments ������������������������������������������������������������������������������������������������������� 538 Debugging with Command-Line Arguments in Unix ������������������������������������������������������������ 541 Debugging with Command-Line Arguments in Visual Studio ���������������������������������������������� 541 Variadic Functions ��������������������������������������������������������������������������������������������������������������������� 543 Bit Manipulation: &, |, ~, and ����������������������������������������������������������������������������������������������������� 545 One Use of Bit Manipulation: Unicode ��������������������������������������������������������������������������������� 545 Another Use: Flags ��������������������������������������������������������������������������������������������������������������� 548 Antibugging ������������������������������������������������������������������������������������������������������������������������� 549 Chapter 26: Esoterica (Recommended), continued ���������������������������������������������� 551 initializer_list ���������������������������������������������������������������������������������������������������������������������������� 551 Moving Work to Compile Time with constexpr and static_assert ��������������������������������������������� 552 Memory Safety with Smart Pointers ����������������������������������������������������������������������������������������� 556 unique_ptr ��������������������������������������������������������������������������������������������������������������������������� 557 shared_ptr ��������������������������������������������������������������������������������������������������������������������������� 560 Antibugging ������������������������������������������������������������������������������������������������������������������������� 561 Chapter 27: Esoterica (Not So Recommended) ����������������������������������������������������� 563 protected Sections, protected Inheritance �������������������������������������������������������������������������������� 563 friends and Why You Shouldn’t Have Any ���������������������������������������������������������������������������������� 567 User-Defined Conversions (Cast Operators) ������������������������������������������������������������������������������ 572 Coroutines ��������������������������������������������������������������������������������������������������������������������������������� 574 Chapter 28: Building Bigger Projects ������������������������������������������������������������������� 577 Namespaces ����������������������������������������������������������������������������������������������������������������������������� 577 Conditional Compilation ������������������������������������������������������������������������������������������������������������ 578 Libraries ������������������������������������������������������������������������������������������������������������������������������������ 579 g++ ������������������������������������������������������������������������������������������������������������������������������������� 580 Microsoft Visual Studio �������������������������������������������������������������������������������������������������������� 582 Table of ConTenTs
xv Chapter 29: C �������������������������������������������������������������������������������������������������������� 593 Compiling C ������������������������������������������������������������������������������������������������������������������������������� 594 I/O ��������������������������������������������������������������������������������������������������������������������������������������������� 595 printf ������������������������������������������������������������������������������������������������������������������������������������ 595 scanf and the address-of (&) operator �������������������������������������������������������������������������������� 595 fprintf and fscanf; fopen and fclose ������������������������������������������������������������������������������������ 598 sprintf and sscanf; fgets, fputs, and puts ���������������������������������������������������������������������������� 600 Summary of Commands ������������������������������������������������������������������������������������������������������ 603 Antibugging ������������������������������������������������������������������������������������������������������������������������� 604 Parameter Passing with * ��������������������������������������������������������������������������������������������������������� 604 Antibugging ������������������������������������������������������������������������������������������������������������������������� 607 Dynamic Memory ���������������������������������������������������������������������������������������������������������������������� 608 Chapter 30: Moving on with SDL �������������������������������������������������������������������������� 611 Writing Code ����������������������������������������������������������������������������������������������������������������������������� 614 Antibugging ������������������������������������������������������������������������������������������������������������������������������� 618 Compiling ���������������������������������������������������������������������������������������������������������������������������������� 618 Further Resources ��������������������������������������������������������������������������������������������������������������������� 618 Appendix A: Help with Setup �������������������������������������������������������������������������������� 619 Appendix B: Operators ������������������������������������������������������������������������������������������ 625 Appendix C: Fundamental Types ��������������������������������������������������������������������������� 627 Appendix D: Common Escape Sequences ������������������������������������������������������������� 629 Appendix E: Basic C Standard Library ������������������������������������������������������������������ 631 Appendix F: Common Debugger Commands ��������������������������������������������������������� 633 Appendix G: SSDL Reference �������������������������������������������������������������������������������� 635 References ������������������������������������������������������������������������������������������������������������ 647 Index ��������������������������������������������������������������������������������������������������������������������� 649 Table of ConTenTs
xvii About the Author Will Briggs, PhD, is a professor of computer science at the University of Lynchburg in Virginia. He has 30 years' experience teaching C++, 18 of them using earlier drafts of this book, and about as many years teaching other languages including C, JavaScript, LISP, Pascal, PHP, PROLOG, and Python. His primary focus is teaching of late while also being active in research in artificial intelligence.
xix About the Technical Reviewer Marius Iulian Mihailescu is an associate professor at the Faculty of Engineering and Computer Science, specializing in cybersecurity, artificial intelligence, machine learning, cloud computing, Internet of Things (IoT), and blockchain technologies. With a strong multidisciplinary background, he teaches a wide range of undergraduate and postgraduate courses, including functional programming with Haskell and F#, web development applications, information security systems, cloud fundamentals, blockchain technologies, Internet of Things engineering, information security management, software security, software development languages (C#, Python, Java), databases (Microsoft SQL Server, Oracle, and PostgreSQL), and software development methods. He has authored and co-authored numerous books, book chapters, research papers, and conference presentations in the fields of cybersecurity, cryptography, AI-driven cyber threat detection, quantum computing, and secure software development methodologies. His research spans both theoretical foundations and practical applications, with a particular interest in quantum cryptography, cyber threat intelligence, ethical hacking, secure cloud architectures, digital forensics, and AI- powered process optimization. He is actively involved in European research projects under Horizon Europe and European Defence Fund (EDF) frameworks, contributing as both researcher and project coordinator. He collaborates with academia, industry, and governmental institutions to design advanced cybersecurity architectures, AI models for predictive analytics, and innovative IoT solutions for smart cities and critical infrastructures. His current research interests include post-quantum cryptography, AI-driven anomaly detection, blockchain-based identity management, secure IoT frameworks, and digital twin technologies. He also mentors graduate and doctoral students in multidisciplinary projects that combine engineering, artificial intelligence, and applied information security.
xx Through his teaching, research, and innovation projects, he continues to contribute to the advancement of secure, intelligent, and resilient digital ecosystems at both national and international levels. abouT The TeChniCal RevieweR
xxi Acknowledgments Special thanks to • Apress • Microsoft • Google, for Google Fonts • Pixabay.com and contributors, especially 3D Animation Production Company/QuinceCreative (Chapter 1, bullseye), David Mark/12019 (Chapter 2, beach), Free-Photos (Chapter 2, pug), Andi Caswell/ andicaz (Chapter 6, scones), joakant (Chapter 11, tropical fish), Gerhard Janson/Janson_G (Chapter 12, UFO), 13smok (Chapter 12, alien sign), Prawny (Chapter 12, splat), Elliekha (Chapter 12, haunted house), pencil parker (Chapter 12, candy), Robert Davis/rescueram3 (Chapter 12, pumpkin photos), HolgersFotografie (Chapter 12, rubber duck) • Wikipedia Commons, including ASpiegler (Chapter 24, normal distribution)1 • OpenClipArt.org and contributors, especially Firkin (Chapter 2, flamingo), GDJ (Chapter 3, camel) • Flickr, especially Speedy McZoom (Chapter 12, jack-o-lantern art) • FreeSound.org and contributors, especially Razor5 (Chapter 2, techno music), robbo799 (Chapter 2, churChapter bells), alqutis (Chapter 12, hovercar), Berviceps (Chapter 12, splat), mistersherlock (Chapter 12, Halloween graveyard), matypresidente (Chapter 12, water drop), Osiruswaltz (Chapter 12, bump), mrose6 (Chapter 12, echoed scream), robcro6010 (Chapter 12, circus theme) • Chad Savage of Sinister Fonts for Werewolf Moon (Chapter 12) 1 License: creativecommons.org/licenses/by-sa/4.0/deed.en. No changes made.
xxii • Lazy Foo' Productions • StackOverflow.com • Hjörvar Hermannsson and Einar Egilsson of cardgames.io for images of card games and Nicu Buculei (http://nicubunu.ro/cards) for card images • Alumni, colleagues, and students who gave me reviews – you're the best! aCknowledgmenTs
Comments 0
Loading comments...
Reply to Comment
Edit Comment