Preface....6
C99 Standard....7
C11 Standard....8
C17 Standard....8
C23 Standard....8
Contents....10
Chapter 1: Representation of Numbers and Characters in Computer....16
1.1 Number Bases....16
1.1.1 Decimal Numbers....16
1.1.2 Binary Numbers....16
1.1.3 Octal Numbers....17
1.1.4 Hexadecimal Numbers....17
1.2 Conversion Between Bases....18
1.2.1 Binary to Decimal Conversion....18
1.2.2 Binary to Octal Conversion....18
1.2.3 Binary to Hexadecimal Conversion....19
1.2.4 Decimal to Binary Conversion....20
1.2.5 Octal to Binary Conversion....21
1.2.6 Hexadecimal to Binary Conversion....21
1.2.7 Hexadecimal to Decimal Conversion....22
1.3 Positive Integers....23
1.4 Two´s Complement Form....23
1.5 Negative Integers....25
1.6 Registers....26
1.7 Memory Units....27
1.8 How Are the Integers Stored in Computer Memory, Big-Endian, and Little-Endian?....27
Chapter 2: Data Types and Operators....29
2.1 How to Start Writing a C Program?....29
2.2 Comments in C Programming....31
2.3 The First C Program....31
2.4 Variables and Data Types....33
2.5 Binary Number Representation in Modern C....35
2.6 sizeof Operator in C....36
2.7 Unsigned Char Data Type....37
2.8 Left and Right Shift Operators in C....39
2.9 Integer Data Type....40
2.10 Hexadecimal and Octal Numbers....42
2.11 How Are Integers Stored in Computer Memory?....43
2.11.1 Short Integer Data Type....44
2.12 Why Do We Have Both Integer and Short Integer Data Types?....45
2.13 Long Integer and Long-Long Integer Data Types....46
2.14 Unsigned Integer Data Type....48
2.15 Floating-Point Number in C....53
2.15.1 IEEE 754 Floating-Point Standard (Single Precision)....53
2.16 Keyboard Input Using scanf in C....55
2.17 Operators in C Programming....57
2.17.1 Arithmetic Operators....57
2.17.1.1 Division of Integer Numbers....59
2.17.1.2 Multiplication of Integer Numbers....59
2.17.2 Remainder Operator %....60
2.17.3 Augmented Assignment Operators....61
2.17.4 Logical Operators....61
2.17.5 Bitwise Operators in C....64
2.17.6 Increment and Decrement Operators....74
2.18 Operator Precedence....76
Chapter 3: Type Conversion in C....81
3.1 Type Conversion Methods....81
3.1.1 Implicit Conversion....81
3.1.2 Explicit Conversion....84
3.2 Information Loss When a Higher-Order Data Is Converted to a Lower-Order Data....87
3.3 Information Loss When Conversion Is Performed Between Signed and Unsigned Data Types....88
Chapter 4: Structures....91
4.1 Introduction....91
4.2 Initialization of Structure Elements....92
4.3 Initialization Using Designated Initializer List....93
4.4 Typedef for Structures....97
4.4.1 Alternative Use of for Structures....99
4.5 Nested Structures....100
4.6 Structure Copying....102
4.7 Structures with Self-Referential....103
4.8 Bit Fields....105
4.9 Structures as Function Arguments....106
4.10 Structure Padding and Packing in C Programming....107
4.11 Unions....108
Chapter 5: Conditional Statements....112
5.1 Conditional Structure....112
5.2 Conditional Ladder Structure (-If Ladder)....117
5.3 Multiconditional Structures....120
5.4 Syntax of Nested If-Else....123
5.5 Conditional Operator in C....125
5.6 switch Statement....129
Chapter 6: Loop Statements....137
6.1 The For-Loop....137
6.1.1 Nested For-Loop....150
6.2 The While-Loop....152
6.2.1 Nested While-Loop....156
6.3 The Do-While Loop....159
6.4 Continue Statement....160
6.5 Break Statement....162
Chapter 7: Complex Numbers in Modern C Programming....169
7.1 How to Define a Complex Number....169
7.2 Complex Operations....170
7.3 Calculation of Absolute Value (Norm, Modulus, or Magnitude) of a Complex Number....173
7.4 Complex Number Formation....173
7.5 Calculation of the Conjugate of a Complex Number in C....174
7.6 Calculation of the Argument, That Is, Phase Angle, of a Complex Number in C....176
7.7 Calculation of Complex Exponentials....176
7.8 Computation of the Complex Natural (Base-e) Logarithm of a Complex Number....177
7.9 Complex Power Calculation....178
7.10 Square Root of a Complex Number....179
7.11 Complex Trigonometric Functions....180
7.11.1 The csin Functions....180
7.11.2 The ccos Functions....180
7.11.3 The ctan Functions....181
7.11.4 The cacos Functions....182
7.11.5 The casin Functions....182
7.11.6 The catan Functions....183
7.11.7 Hyperbolic Functions....184
Chapter 8: Arrays....186
8.1 Syntax for Array Declaration....186
8.2 Accessing Array Elements....186
8.3 Array Initialization Without Size....188
8.4 Array Initialization Using Loops....190
8.5 Strings as Array of Characters....195
8.6 Multidimensional Arrays....196
8.7 Passing an Array to a Function in C....200
Chapter 9: Functions....203
9.1 Introduction....203
9.2 Types of Functions....207
9.3 Passing Parameters to Functions....208
9.4 Returning More Than One Value....210
9.5 Recursive Functions....212
9.6 Nested Functions....215
Chapter 10: Pointers....217
10.1 Definition....217
10.2 Address of a Variable....217
10.3 NULL Pointer....225
10.4 Void Pointer....225
10.5 Types of Pointers....231
10.5.1 Pointer to a Constant Value....231
10.5.2 Pointer to a Constant Address (Constant Pointer)....233
10.5.3 Constant Pointer to a Constant Value....234
10.6 Function Pointers....234
10.6.1 Functions Returning Pointers....238
10.7 Pointers and Arrays....240
10.8 Multiple Pointers....247
10.9 Heap Stack and Code Memories....249
10.10 Dynamic Memory Allocation....250
10.10.1 malloc() Function....251
10.10.2 calloc() Function....255
10.10.3 free() Function....256
10.10.4 realloc() Function....257
10.11 Memory Functions....258
10.11.1 Memset Function....258
10.11.2 Memcpy Function....259
10.11.3 Memmove Function....260
10.11.4 Memcmp Function....261
Chapter 11: Directives and Macros in C....268
11.1 Introduction....268
11.2 Preprocessor Directives as Macros....268
11.3 Macros as Functions....270
11.4 Multiline Macros....271
11.5 Directives Used for File Inclusion....274
11.6 Predefined Macros....276
11.7 Conditional Compilation....279
11.8 Concatenation Operator ##....283
Chapter 12: Type Qualifiers, Enumerations, and Storage Classes in C....286
12.1 Type Qualifiers in C....286
12.1.1 Const....286
12.1.2 Restrict....288
12.1.3 Volatile....288
12.2 Storage Classes in C....289
12.2.1 Auto....289
12.2.2 Extern....289
12.2.3 Static....292
12.2.4 Register....293
Chapter 13: Integer with Exactly N Bits....297
13.1 General Form of Fixed Width Integers....297
13.2 Macros for printf and scanf....297
13.3 uintN_t....301
13.4 int_leastN_t....303
13.5 uint_leastN_t....304
13.6 int_fastN_t....304
13.7 uint_fastN_t....305
13.8 Macros for printf....306
13.9 Macros for scanf....306
Chapter 14: Signals in C....310
14.1 Introduction....310
14.2 Signal Handling....311
14.3 SIGINT....311
14.4 SIGQUIT....315
14.5 Artificial Signal Generation....317
14.6 Some of the Most Used Signals....318
Chapter 15: Threads in C....320
15.1 Introduction....320
15.2 Thread Creation....320
15.3 Parallel Processing Using Threads....321
15.4 pthread_exit() Function....328
15.5 pthread_join() Function....330
15.6 pthread_self() Function....336
15.7 pthread_equal() Function....337
15.8 pthread_cancel() Function....338
15.9 pthread_detach() Function....338
15.10 Synchronizing Threads with Mutexes....339
Chapter 16: Atomic Data Types....349
16.1 How to Define an Atomic Data Type?....349
16.2 Atomic Integer Types....350
16.3 Atomic Pointers....352
16.4 Race Prevention by Atomic Variables....353
16.5 Lock-Free Atomic Types....358
16.6 Atomic Assignments, Operators, and Functions....359
16.7 Atomic Functions....360
16.7.1 atomic_is_lock_free() Function....360
16.7.2 atomic_fetch_key() Function....360
16.7.3 atomic_store() Function....363
16.7.4 atomic_load() Function....364
16.7.5 atomic_exchange() Function....365
16.7.6 Comparison Functions....366
16.7.7 atomic_flag Macro....368
16.7.8 atomic_init() Function....369
16.8 Memory Order in C....370
16.8.1 Acquire, Release, and Consume....370
16.8.2 Memory Order....371
16.8.3 Atomic Functions with Memory Order....373
Chapter 17: File Operations in C....375
17.1 File Types....375
17.2 File Operations....375
17.2.1 Opening a File....375
17.2.2 Closing a File....376
17.2.3 Reading and Writing of a Text File....377
17.2.3.1 Writing to a Text File....377
17.2.3.2 Reading a Text File....381
17.2.4 Reading and Writing of a Binary File....385
17.2.4.1 Writing to Binary Files....386
17.2.4.2 Reading Binary Files....387
Bibliography....393
Index....394
This book provides comprehensive detail about modern C programming, including the standards C99, C11, C17, C23, reflecting recent updates. The book features a number of targeted examples, atomic data types, and threads. After covering the standards of C, the author explains data types, operators, loops, conditional statements, functions, pointers, and more. The book is intended primarily for electrical and hardware engineers looking to use or update their knowledge of modern C programming.