Contents
Acknowledgments
About this book
C revisions
This edition
C and C++
Requirements
Source code
Exercises and challenges
Organization
Author
About the cover illustration
Level 0 Encounter
1 Getting started
1.1 Imperative programming
1.2 Compiling and running
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
Level 1 Acquaintance
Buckle up
3 Everything is about control
3.1 Conditional execution
3.2 Iterations
3.3 Multiple selection
4 Expressing computations
4.1 Operands and operators
4.2 Arithmetic
4.2.1 +, -, and *
4.2.2 Division and remainder
4.3 Operators that modify objects
4.4 Boolean context
4.4.1 Comparison
4.4.2 Logic
4.5 The ternary or conditional operator
4.6 Evaluation order
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.6.5 The [basicstyle=]constexpr construct
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 Bit-precise integer types
5.7.8 Floating-point data
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.3.1 Simple structures to access fields by name
6.3.2 Structures with fields of different types
6.3.3 Nested structures
6.3.4 Coalescing structure fields
6.4 New names for types: type aliases
7 Functions
7.1 Simple functions
7.2 main is special
7.3 Recursion
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 Integer arithmetic
8.3 Numerics
8.4 Input, output, and file manipulation
8.4.1 Unformatted text output
8.4.2 Files and streams
8.4.3 Text IO
8.4.4 Formatted output
8.4.5 Unformatted text input
8.5 String processing and conversion
8.5.1 Portability of string processing
8.6 Time
8.7 Runtime environment settings
8.8 Program termination and assertions
Level 2 Cognition
9 Style
9.1 Formatting
9.2 Naming
9.3 Internationalization, so to speak
10 Organization and documentation
10.1 Interface documentation
10.2 Implementation
10.2.1 Macros
10.2.2 Pure functions
10.2.3 Attributes
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
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
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.1.3 Flexible array members
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
14 More involved processing and IO
14.1 Text processing
14.2 Formatted input
14.3 Extended character sets
14.4 UTF character encodings
14.5 Restartable text conversion
14.6 Binary streams
15 Program failure
15.1 Wrongdoings
15.1.1 Arithmetic violations
15.1.2 Invalid conversions
15.1.3 Value violations
15.1.4 Type violations
15.1.5 Access violations
15.1.6 Value misinterpretation
15.1.7 Explicit invalidation
15.2 Program state degradation
15.2.1 Unbounded recursion
15.2.2 Storage exhaustion
15.2.3 Other scarce resources
15.3 Unfortunate incidents
15.3.1 Escalating state degradation
15.3.2 Collisions and race conditions
15.3.3 Inappropriate library calls and macro invocations
15.3.4 Deadlocks
15.4 Series of unfortunate events
15.5 Dealing with failures
15.6 Error checking and cleanup
Level 3 Experience
16 Performance
16.1 Inline functions
16.2 Using restrict qualifiers
16.3 The unsequenced and reproducible attributes
16.4 Measurement and inspection
17 Function-like macros
17.1 How function-like macros work
17.2 Argument checking
17.3 Accessing the context of invocation
17.4 Variable-length argument lists
17.4.1 Variadic macros
17.4.2 A detour: variadic functions
17.5 Default arguments
18 Type-generic programming
18.1 Inherent type-generic features in C
18.1.1 Operators
18.1.2 Default promotions and conversions
18.1.3 Macros
18.1.4 Variadic functions
18.1.5 function pointers
18.1.6 void pointers
18.1.7 Type-generic C library functions
18.2 Generic selection
18.3 Type inferrence
18.3.1 The auto feature
18.3.2 The typeof feature
18.4 Anonymous functions
19 Variations in control flow
19.1 A detailed example
19.2 Sequencing
19.3 Short jumps
19.4 Functions
19.5 Long jumps
19.6 Signal handlers
20 Threads
20.1 Simple inter-thread control
20.2 Race-free initialization and destruction
20.3 Thread-local data
20.4 Critical data and critical sections
20.5 Communicating through condition variables
20.6 More sophisticated thread management
20.7 Ensure liveness
21 Atomic access and memory consistency
21.1 The ``happened before'' relation
21.2 C library calls that provide synchronization
21.3 Sequential consistency
21.4 Other consistency models
Technical Annex
A Transitional code
B C Compilers
B.1 Attributes
B.2 Missing embed
B.3 Missing constexpr
B.4 Missing 128 bit integer support
C C Libraries
C.1 Functions borrowed from POSIX or similar systems
C.2 Improved UTF-8 support
C.3 Bit utilities
C.4 Checked integer arithmetic
C.5 Formatted IO
C.6 Mathematical functions
C.7 A reference implementation for musl libc
Takeaways
Bibliography
Index
For programs that need to be small, fast, and reliable, C is the gold standard. Whether you’re writing embedded code, low-level system routines, or high-performance applications, C is up to the challenge. This unique book by Jens Gustedt, a member of the ISO C standards committee, gets you up to speed with C23.
In Modern C, Third Edition you’ll learn to harness C’s full potential using the latest tools and techniques. After a quick review of the fundamentals suited for coders who haven’t used C in a while, this book guides you to mastery of C23, the latest ISO standard.
From code running on the smallest embedded devices to the low-level libraries behind popular programming languages like Python and Ruby, the software world depends on C. And even after 50 years, it’s still getting better! The new C23 standard adds improvements for security, reliability, and performance. In this thoroughly-revised new edition, author Jens Gustedt gives you an authoritative inside view.
Modern C, Third Edition is a fast-paced introduction to the C language, with special attention on its most modern features. It starts with a quick review of structure, grammar, and execution and then progresses quickly to control structures, data types, operators, and other core language features. Fully revised for C23, this expanded Third Edition covers compound expressions and lambdas, new insights into approaching program failure, and how to transition smoothly to C23.
For readers with intermediate skills in any programming language.