Statistics
18
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-12-21
Support
Share

AuthorJens Gustedt

Modern C introduces you to modern day C programming, emphasizing the unique and new features of this powerful language. For new C coders, it starts with fundamentals like structure, grammar, compilation, and execution. From there, you’ll advance to control structures, data types, operators, and functions, as you gain a deeper understanding of what’s happening under the hood. In the final chapters, you’ll explore performance considerations, reentrancy, atomicity, threads, and type-generic programming. You’ll code as you go with concept-reinforcing exercises and skill-honing challenges along the way.

Tags
No tags
Publisher: Manning Publications
Publish Year: 2019
Language: 英文
File Format: PDF
File Size: 9.2 MB
Support Statistics
¥.00 · 0times
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.

(This page has no text content)
About the cover illustration The figure on the cover of Modern C is captioned “Femme Dalmate” or woman from Dalmatia. The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset-St-Sauveur (1757-1810), titled Costumes de Différents Pays, published in France in 1788. Each illustration is finely drawn and colored by hand. The rich variety of Grasset-St-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress. The way we dress has changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life. At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset-St-Sauveur’s pictures.
Modern C Jens Gustedt
Copyright For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact        Special Sales Department        Manning Publications Co.        20 Baldwin Road        PO Box 761        Shelter Island, NY 11964        Email: orders@manning.com © 2020 Jens Gustedt has released the manuscript of this work under a Creative Commons license for non-commercial use (CC BY NC). Jens Gustedt has granted to Manning Publications the exclusive commercial right to publish this work in print and electronic formats throughout the world. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Acquisitions editor: Mike Stephens Development editor: Jennifer Stout Technical development editor: Jon Bergman Review editor: Aleksander Dragosavljevic Technical proofreader: Nitin Gode Cover designer: Marija Tudor ISBN 9781617295812 Printed in the United States of America
Brief Table of Contents Copyright Brief Table of Contents Table of Contents Acknowledgments About this book About the cover illustration Level 0. Encounter Chapter 1. Getting started Chapter 2. The principal structure of a program Level 1. Acquaintance Chapter 3. Everything is about control Chapter 4. Expressing computations Chapter 5. Basic values and data Chapter 6. Derived data types Chapter 7. Functions Chapter 8. C library functions Level 2. Cognition Chapter 9. Style Chapter 10. Organization and documentation
Chapter 11. Pointers Chapter 12. The C memory model Chapter 13. Storage Chapter 14. More involved processing and IO Level 3. Experience Chapter 15. Performance Chapter 16. Function-like macros Chapter 17. Variations in control flow Chapter 18. Threads Chapter 19. Atomic access and memory consistency  Takeaways  Bibliography Index List of Figures List of Tables List of Listings
Table of Contents Copyright Brief Table of Contents Table of Contents Acknowledgments About this book About the cover illustration Level 0. Encounter Chapter 1. Getting started 1.1. Imperative programming 1.2. Compiling and running Summary Chapter 2. The principal structure of a program 2.1. Grammar 2.2. Declarations 2.3. Definitions 2.4. Statements 2.4.1. Iteration 2.4.2. Function calls 2.4.3. Function return
Summary Level 1. Acquaintance Chapter 3. Everything is about control 3.1. Conditional execution 3.2. Iterations 3.3. Multiple selection Summary Chapter 4. Expressing computations 4.1. Arithmetic 4.1.1. +, -, and * 4.1.2. Division and remainder 4.2. Operators that modify objects 4.3. Boolean context 4.3.1. Comparison 4.3.2. Logic 4.4. The ternary or conditional operator 4.5. Evaluation order Summary Chapter 5. Basic values and data 5.1. The abstract state machine 5.1.1. Values
5.1.2. Types 5.1.3. Binary representation and the abstract state machine 5.1.4. Optimization 5.2. Basic types 5.3. Specifying values 5.3.1. Complex constants 5.4. Implicit conversions 5.5. Initializers 5.6. Named constants 5.6.1. Read-only objects 5.6.2. Enumerations 5.6.3. Macros 5.6.4. Compound literals 5.7. Binary representions 5.7.1. Unsigned integers 5.7.2. Bit sets and bitwise operators 5.7.3. Shift operators 5.7.4. Boolean values 5.7.5. Signed integers 5.7.6. Fixed-width integer types
5.7.7. Floating-point data Summary Chapter 6. Derived data types 6.1. Arrays 6.1.1. Array declaration 6.1.2. Array operations 6.1.3. Array length 6.1.4. Arrays as parameters 6.1.5. Strings are special 6.2. Pointers as opaque types 6.3. Structures 6.4. New names for types: type aliases Summary Chapter 7. Functions 7.1. Simple functions 7.2. main is special 7.3. Recursion Summary Chapter 8. C library functions 8.1. General properties of the C library and its functions
8.1.1. Headers 8.1.2. Interfaces 8.1.3. Error checking 8.1.4. Bounds-checking interfaces 8.1.5. Platform preconditions 8.2. Mathematics 8.3. Input, output, and file manipulation 8.3.1. Unformatted text output 8.3.2. Files and streams 8.3.3. Text IO 8.3.4. Formatted output 8.3.5. Unformatted text input 8.4. String processing and conversion 8.5. Time 8.6. Runtime environment settings 8.7. Program termination and assertions Summary Level 2. Cognition Chapter 9. Style 9.1. Formatting 9.2. Naming
Summary Chapter 10. Organization and documentation 10.1. Interface documentation 10.2. Implementation 10.2.1. Macros 10.2.2. Pure functions Summary Chapter 11. Pointers 11.1. Pointer operations 11.1.1. Address-of and object-of operators 11.1.2. Pointer addition 11.1.3. Pointer subtraction and difference 11.1.4. Pointer validity 11.1.5. Null pointers 11.2. Pointers and structures 11.3. Pointers and arrays 11.3.1. Array and pointer access are the same 11.3.2. Array and pointer parameters are the same 11.4. Function pointers Summary
Chapter 12. The C memory model 12.1. A uniform memory model 12.2. Unions 12.3. Memory and state 12.4. Pointers to unspecific objects 12.5. Explicit conversions 12.6. Effective types 12.7. Alignment Summary Chapter 13. Storage 13.1. malloc and friends 13.1.1. A complete example with varying array size 13.1.2. Ensuring consistency of dynamic allocations 13.2. Storage duration, lifetime, and visibility 13.2.1. Static storage duration 13.2.2. Automatic storage duration 13.3. Digression: using objects "before” their definition 13.4. Initialization 13.5. Digression: a machine model
Summary Chapter 14. More involved processing and IO 14.1. Text processing 14.2. Formatted input 14.3. Extended character sets 14.4. Binary streams 14.5. Error checking and cleanup Summary Level 3. Experience Chapter 15. Performance 15.1. Inline functions 15.2. Using restrict qualifiers 15.3. Measurement and inspection Summary Chapter 16. Function-like macros 16.1. How function-like macros work 16.2. Argument checking 16.3. Accessing the calling context 16.4. Default arguments 16.5. Variable-length argument lists 16.5.1. Variadic macros
16.5.2. A detour: variadic functions 16.6. Type-generic programming Summary Chapter 17. Variations in control flow 17.1. A complicated example 17.2. Sequencing 17.3. Short jumps 17.4. Functions 17.5. Long jumps 17.6. Signal handlers Summary Chapter 18. Threads 18.1. Simple inter-thread control 18.2. Race-free initialization and destruction 18.3. Thread-local data 18.4. Critical data and critical sections 18.5. Communicating through condition variables 18.6. More sophisticated thread management Summary Chapter 19. Atomic access and memory consistency 19.1. The “happened before” relation
19.2. C library calls that provide synchronization 19.3. Sequential consistency 19.4. Other consistency models Summary  Takeaways  Bibliography Index List of Figures List of Tables List of Listings
Acknowledgments Special thanks go to the people that encouraged the writing of this book by providing me with constructive feedback, including colleagues and other interested readers, Cédric Bastoul, Lucas Nussbaum, Vincent Loechner, Kliment Yanev, Szabolcs Nagy, Marcin Kowalczuk, Ali Asad Lotia, Richard Palme, Yann Barsamian, Fernando Oleo, Róbert Kohányi, Jean-Michel Gorius ... Manning’s staff Jennifer Stout, Nitin Gode and Tiffany Taylor, ... ... and the impressive number of reviewers provided by Manning: Adam Kalisz, Andrei de Araujo Formiga, Christoph Schubert, Erick Nogueira do Nascimento, Ewelina Sowka, Glen Sirakavit, Hugo Durana, Jean-François Morin, Kent R. Spillner, Louis Aloia, Manu Raghavan Sareena, Nitin Gode, Rafael Aiquel, Sanchir Kartiev, and Xavier Barthel Many others have contributed to the success of this book, my sincerest thanks to all of you.
About this book The C programming language has been around for a long time — the canonical reference for it is the book written by its creators, Kernighan and Ritchie [1978]. Since then, C has been used in an incredible number of applications. Programs and systems written in C are all around us: in personal computers, phones, cameras, set-top boxes, refrigerators, cars, mainframes, satellites ... basically in any modern device that has a programmable interface. In contrast to the ubiquitous presence of C programs and systems, good knowledge of and about C is much more scarce. Even experienced C programmers often appear to be stuck in some degree of self-inflicted ignorance about the modern evolution of the C language. A likely reason for this is that C is seen as an “easy to learn” language, allowing a programmer with little experience to quickly write or copy snippets of code that at least appear to do what it’s supposed to. In a way, C fails to motivate its users to climb to higher levels of knowledge. This book is intended to change that general attitude, so it is organized in levels that reflect familiarity with the C language and programming in general. This structure may go against some habits of the book’s readers; in particular, it splits some difficult subjects (such as pointers) across levels in order to not swamp readers too early with the wrong information. We’ll explain the book’s organization in more detail shortly. Generally, although many universally applicable ideas will be presented, that would also be valid for other programming languages (such as Java, Python, Ruby, C#, or C++) the book primarily addresses concepts and practices that are unique to C or are of particular value when programming in the C language.
C VERSIONS As the title of this book suggests, today’s C is not the same language as the one originally designed by its creators, Kernighan and Ritchie (usually referred to as K&R C). In particular, it has undergone an important standardization and extension process, now driven by ISO, the International Standards Organization. This led to the publication of a series of C standards in 1989, 1999, 2011, and 2018, commonly referred to as C89, C99, C11, and C17. The C standards committee puts a lot of effort into guaranteeing backward compatibility such that code written for earlier versions of the language, say C89, should compile to a semantically equivalent executable with a compiler that implements a newer version. Unfortunately, this backward compatibility has had the unwanted side effect of not motivating projects that could benefit greatly from the new features to update their code base. In this book, we will mainly refer to C17, as defined in JTC1/SC22/WG14 [2018], but at the time of this writing some compilers don’t implement this standard completely. If you want to compile the examples in this book, you will need at least a compiler that implements most of C99. For the changes that C11 added to C99, using an emulation layer such as my macro package P99 might suffice; the package is available at http://p99.gforge.inria.fr. C AND C++ Programming has become a very important cultural and economic activity, and C remains an important element in the programming world. As in all human activities, progress in C is driven by many factors: corporate or individual interest, politics, beauty, logic, luck, ignorance, selfishness, ego, sectarianism (add your primary motivation here). Thus the development of C has not been and cannot be ideal. It has flaws and artifacts that can only be understood with their historical and societal context.
The above is a preview of the first 20 pages. Register to read the complete e-book.