Extreme C


♡ Coding Demo ♡

                             Content

                                   


Content

About
Preface
ch1 - Essential Features
ch2 - From Source to Binary
... towards ch23 ...
Notes
Links




♡ Listen to New York Jazz Lounge - Bar Jazz Masterpieces ♡♡♡ ♡ ♡♡♡

Extreme C

About

by Kamran Amini - GitHub - Twitter - published on Packt> - Boookauthority's Best C Books of All Time - #9! ♥♡♥ - Amazon $31.19 - Push the limits of what C - and you - can do, with this high-intensity guide to the most advanced capabilities of C!

Key Features:
• Make the most of C's low-level control, flexibility, and high performance
• A comprehensive guide to C's most powerful and challenging features
• A thought-provoking guide packed with hands-on exercises and examples

Description:
There's a lot more to C than knowing the language syntax. The industry looks for developers with a rigorous, scientific understanding of the principles and practices. Extreme C will teach you to use C's advanced low-level power to write effective, efficient systems. This intensive, practical guide will help you become an expert C programmer.
Building on your existing C knowledge, you will master preprocessor directives, macros, conditional compilation, pointers, and much more. You will gain new insight into algorithm design, functions, and structures. You will discover how C helps you squeeze maximum performance out of critical, resource-constrained applications.
C still plays a critical role in 21st-century programming, remaining the core language for precision engineering, aviations, space research, and more. This book shows how C works with Unix, how to implement OO principles in C, and fully covers multi-processing.
In Extreme C, Amini encourages you to think, question, apply, and experiment for yourself. The book is essential for anybody who wants to take their C to the next level.

What you will learn:
• Build advanced C knowledge on strong foundations, rooted in first principles
• Understand memory structures and compilation pipeline and how they work, and how to make most out of them
• Apply object-oriented design principles to your procedural C code
• Write low-level code that's close to the hardware and squeezes maximum performance out of a computer system
• Master concurrency, multithreading, multi-processing, and integration with other languages
• Unit Testing and debugging, build systems, and inter-process communication for C programming

Who this book is for:
Extreme C is for C programmers who want to dig deep into the language and its capabilities. It will help you make the most of the low-level control C gives you.

Table of Contents

I
1 - Essential Features
2 - From Source to Binary
3 - Object Files

II
4 - Process Memory Structure
5 - Stack and Heap

III
6 - OOP and Encapsulation
7 - Composition and Aggregation
8 - Inheritance and Polymorphism
9 - Abstraction and OOP in C++

IV
10 - Unix: History and Architecture
11 - System Calls and Kernels
12 - The Most Recent C

V
13 - Concurrency
14 - Synchronization
15 - Thread Execution
16 - Thread Synchronization

VI
17 - Process Execution
18 - Process Synchronization
19 - Single-Host IPC and Sockets
20 - Socket Programming
21 - Integration with Other Languages

VII
22 - Unit Testing and Debugging
23 - Build Systems




Preface

xi-xix - C one of few core technologies - not dead tiobe.com - Linux -
xii - experience C C++ Go Java Python on various BSD Unix flavors, Linux, and Microsoft Windows - next level, Extreme C not easy journey - examples prepare to deal with the things you will encounter within a real system - topic an honor close to author's heart ♥ - for reader with minimum level of knowledge in C/C++ -
xiii - book VII parts: I build C project - II memory ch4 p133 - III object orientation oo ch6 p201 - IV Unix relationship to c ch10 p309 - V concurrency ch13 p391 - VI inter-process communication ch17 p515 - VII testing and maintenance ch22-23 p717 to end of book p778 + index end p794 - summary ch1-23 -
xvi - minimum requirements: ... - download example code files github.com -
xvii - Conventions used: Code-Box (blue_grey) examples or pseudo-code, bold lines discussed before or after code-box -
xviii - Shell Box (green) shows Terminal output, commands bold starting with $ or # (super user), output normal font, working directory same as in code-bundle: $ gcc ExtremeC_examples_chapter17_5.c -lrt -o ex17_5.out
$ ./ex17_5.out
Bold: Indicates a new term - icon pen: warning or important note - icon light bulb: tipps and tricks -
xix - Get in touch ... errata, piracy, contributing - reviews




ch1 - Essential Features

p1-52 - book covers fundamental and advanced knowledge needed to write great software ♥ from simple single process programs to more complex multiple process systems - ch1 extremely useful features for regular programming -
first we consider some key features here before we go deeper with C, including preprocessor directives, variable pointers, function pointers, and structures - without them, no piece of software could continue to work, even if it could get executed - nearly all devices have pieces of software written in C
p2 - therefore we'll now explore the following topics:

• Preprocessor directives, macros, and conditional compilation - can't easily find in other programming languages - lot of advantages, and we'll dive into some of its interesting applications, including macros and conditional directives
• Variable pointers - deep-dive - looking at flaws that could be introduced by misuse
• Functions - deep-dive beyond syntax - functions as the building blocks for writing procedural code - function call mechanism, and how function receives its arguments from the caller function
• Structures - are main building blocks for writing well-organized and more object-oriented code - their importance, together with function pointers, simply cannot be overstated! - last section of chapter, we'll revisit all things that you need to know about structures in C and the tricks around them

essential features of C, and their surrounding concepts, play a key role in the Unix ecosystem, despite its old age and harsh syntax - more also in following chapters

p3 - Already be familiar with following basics in C:
• General knowledge of computer architecture ... how a program interacts with these elements
• General knowledge of programming - what an algorithm is, how its execution can be traced, what a source code is, what binary numbers are, and how their related arithmetic work
• Terminal
• Programming topics: conditional statements, different kinds of loops, structures or classes in at least one programming language, pointers in C or C++, functions, and so on
• oop ...

Preprocessor directives

-> fully covered in ch2 - allows to engineer and modify source code before submitting to compiler - ... - preprocessor can be controlled and influenced using a set of directives - ...

Macros

p4 - rumors ... - grep command ... -

Defining a macro

p5 - using the #define directive - macro has name and possible list of parameters - and has value which gets substituted (ersetzt) by its name through macro expansion - can be undefined with #undef - example ex1.1 ... #define ABC 5 ... - compiled example -
p6 - ex1.2 ... #define ADD(a, b) a + b ... - function-like macro - ... int z = ADD(x, y); ... - ... we get x + y instead of ADD(a, b) ... - ** pointrer to pointer stackoverflow.com
p7 - can mimic C functions ... no need to introduce a new C function - ... macros only exist before the compilation phase, compiler doesn't know anything about the macros! - very important point to remember if using macros instead of functions - so macros allow to generate code before compilation ... - modern C compilers know about directives ... - ex. Code Box 1-5 and Shell Box 1-1 ... - ...
p8 - can use the -E option to dump the code after preprocessing - Shell Box 1-2 ... $ clang -E example.c ... - ... A translation unit (or a compilation unit) is the preprocessed C code that is ready to be passed to the compiler ... all directives are substituted with inclusions or macro expansions ...
p9 - now more difficult examples - will show the power and danger of macros - ... Code Box 1-6 ... compiles to Code Box 1-7 a not C-looking set of instructions to correct C program - this is important application of macros: define new domain specific language (DSL) and write code using it -
p10 - DSL used in testing frameworks such as Google Test framework (gtest) - no C directives in final preprocessed code ex. Code Box 1-7, no #include but its content
ex1.4 with 2 new operators # and ## ... \ to continue line at next line

Variadic macros

p12 - ex1.5 can accept a variable number of input arguments, 2 or 4 or 7 arguments - handy when not sure about number of arguments in different usages of the same macro
Code Box 1-10 [ExtremeC_examples_chapter1_5.c]: ...
__VA_ARGS__ is indicator that tells preprocessor to replace it with all the remaining input arguments that are not assigned to any parameter yet -
p13 - second usage of LOG_ERROR , according to the macro definition, the arguments argv[1] , 1 , and VERSION are those input arguments not assigned to any parameter - going to be used in place of __VA_ARGS__ while expanding the macro - as a side note, function fprintf writes to a file descriptor, which is stderr , which is the error stream of the process -
progressive usage of variadic macros shows ex1.6, which tries to mimic a loop - before foreach in C++, boost framework was (and still is) offering the foreach behavior using a number of macros -
p14 - watch linked code of BOOST_FOREACH macro, defined as the last thing in the header file, which is used to iterate over a boost collection, and it is actually a function-like macro -
ex1.6 about a simple loop which is not comparable to boost's foreach at all, but yet, it is giving you an idea on how to use variadic macros for repeating a number of instructions -
... loop unrolling ... only capable of handling lists of up to three elements ... ...

Advantages and disadvantages of macros

p16 - ... you start building the expected preprocessed code in your mind even before having any
macros defined and based on that you define your macros - can be overused - ... it is not present in your final binaries - can start to cause design issues - macros try to make everything linear and flat - information regarding them can be lost after the preprocessing phase - therefore rule of thumb about macros:

If a macro can be written as a C function, then you should write a C function instead!

p17 - from the debugging perspective, again, it is said that macros are evil - developer uses compilation errors to find the places where there exist syntax errors - they also use logs and possibly compilation warnings to detect a bug and fix it - ... - old and modern compilers using macros ... - ... trade-off between design and performance - ...
p18 - you could avoid loops and use loop unrolling instead - ... optimization or performance tuning - ... code generation is another common application of macros ... - always share your decisions regarding the usage of the macros in the team and keep yourself aligned with the decisions made within the team

Conditional compilation

p19 - ... preprocessed code that is passed to the compiler can be different based on some specified conditions, evaluated by the preprocessor ... - different directives:

• #ifdef • #ifndef • #else • #elif • #endif

ex1.7 ... - no value is proposed for the CONDITION macro - ... -
p20 - macros can be defined using -D options passed to the compilation command: $ gcc -DCONDITION -E main.c ... helpful when having a single source code but compiling it for different architectures, for example, Linux or macOS ...
...

Variable pointers

p22 - concept of a variable pointer, or for short pointer, is one of the most fundamental concepts in C - you can hardly find any direct sign of them in most high-level programming languages - addresses can be used by hardware; not the case for the higher-level twin concepts like 'references' - deep understanding about pointers and the way they work is crucial to become a skilled C programmer - potential to lead to a disaster when used in a wrong way - will cover memory management-related topics in ch4, Process Memory Structure, and ch5, Stack and Heap - here now recap everything about pointers:

Syntax

idea behind any kind of pointer very simple; just simple variable that keeps a memory address - declaring with asterisk * - ex1.9 includes everything we need to know about the pointer's syntax: int main(int argc, char** argv) {
    int var = 100;
    int* ptr = 0;
    ptr = &var; // & operator, called referencing operator, returns address of
        // variable next to it - cannot initialize pointers with valid addresses
    *ptr = 200;
    return 0;
}
p23 - 1st line in Stack segment is int var - more about Stack segment in ch4, Process Memory Structure -
2nd line declares pointer ptr value 0, called null pointer - important to nullify a pointer if you are not going to store a valid address - no header file because pointers part of C language - C code can have no header files -
all of the following declarations are valid in C: int* ptr = 0; int * ptr = 0; int *ptr = 0; 3rd line intoduces & operator, stores returned address into ptr pointer, and now ptr not a null pointer anymore -
4th line another operator prior to the pointer, called dereferencing operator and denoted by * - allows indirect access to memory cell that ptr pointer is pointing to; in other words, allows to read and modify the var variable through the pointer that is pointing to it - this line is equivalent to var = 200; statement -
null pointer not pointing to valid memory address - therefore, dereferencing a null pointer must be avoided because it is considered as an undefined behavior, which usually leads to a crash -
p24 - good practice to use the macro NULL instead of 0 directly, makes it easier to distinguish between the variables and the pointers; char* ptr = NULL; ... C++11 new keyword nullptr ... - pointers must be initialized upon declaration, if you don't want to store any valid memory address while declaring them - don't leave them uninitialized! Make it null by assigning 0 or NULL! Otherwise you may face a fatal bug! - modern compilers nullify unitialuized pointers, but this is no excuse! Always write code for different architechtures, old and new, or warning in memory profiler, more in ch4 and ch5

Arithmetic on variable pointers

...

p34 - ...

Some details about functions

analogous to ordinary variables that store algorithms instead of values - putting variables and functions together, can store relevant values and algorithms under the same concept - as in object-oriented programming (oop), covered in part III, Object Orientation - here explore functions and discuss their properties in C -

Anatomy of a function

here recap everything about a C function in a single place - function is a box of logic that has a name, a list of input parameters, and a list of output results - return only one value - can't throw exception like C++ or Java - invoked by function call, using name of function (callee) to execute its logic - call should pass all required arguments for execution - functions are always blocking in C; has to wait for function to finish to collect returned result -
p35 - no non-blocking functions, asynchronous or async functions (event-oriented programming) in C, need to implement them using multithreading solutions, explained in more detail in part V, Concurrency - most of written functions are non-blocking - growing interest in using non- blocking functions over blocking functions - function calls happen inside an event loop - frameworks such as libuv and libev promote this way of coding -

Importance in design

functions are fundamental building blocks of procedural programming - huge impact on the way we write code - can store logic in semi-variable entities and summon them whenever and wherever they are needed - can write only once specific logic and use it multiple times in various places - can hide piece of logic from other existing logic - example avg ... - ... -

Stack management

memory layout of a processes share similar layout (see ch4) - now we introduce one of its segments; the Stack segment - default memory location where all local variables, arrays, and structures are allocated from - always happens on top of the Stack segment - ... -

Pass-by-value versus pass-by-reference

p36 - ... -

p41 -

Structures

one of the most fundamental concepts in C ...


p41 -



ch2 - From Source to Binary

p53 - ...




ch10 - Unix – History and Architecture

p309-338 - why 2 chapters about relationship between C and Unix ... - Dennis M. Ritchie – The Development of the C Language - in this ch:
history of Unix and how invention of C happened
development of C based on B and BCPL
• Unix onion architecture designed on the Unix philosophy
user application layer together with shell ring - how programs consume the API exposed by the shell ring - SUS and POSIX standards explained
kernel layer - what features and functionalities should be present in a Unix kernel
Unix devices - how they can be used in a Unix system

Unix onion

p317 - user applications - shell - kernel - hardware - kernel - shell - user applications




ch3 to ch22

p101-754 - ... coming soon ♡ ♥ ♡ ...




ch23 - Build systems

p755-778 - ... when number of source files grows (and believe me, it happens), building a code base frequently becomes a real obstacle to development tasks - therefore, a proper mechanism for building a software project is crucial - ...
End of book :-))) Woooooow!!!!!!! Bravo and congrats Mr. Amini!!! ♡ ♥ ♡




Notes

Machines for world knowledge 300 Exabytes, ship SH and spaceship SS - Computers should do what humans want them to do - input output - keyboard pad touchscreen cam mic controller etc. - based binary - ASM and C needed - terminal - IN HTML CSS JS WASM - Arduino Rapberry PI etc. - Linux - games animation sounddesign graficdesign - art FP MP WR - O MG

Chrome: Ctrl F5 > reload tab




Links




Sessions

Session 1 • Wed 2021-3-10 Coswig (Anhalt) 9:35-16:45 WR ec.html - start ♡♡♡ Preface + ch1 p3 ♡♡♡