Modern C Programming Including Standards C99, C11, C17, C23 (Gazi, Orhan) (Z-Library)
Author: Gazi, Orhan
科学
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.
📄 File Format:
PDF
💾 File Size:
2.9 MB
14
Views
0
Downloads
0.00
Total Donations
📄 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.
📄 Page
1
(This page has no text content)
📄 Page
2
Modern C Programming
📄 Page
3
Orhan Gazi Modern C Programming Including Standards C99, C11, C17, C23
📄 Page
4
Orhan Gazi Electrical and Electronics Engineering Ankara Medipol University Altındağ, Ankara, Türkiye ISBN 978-3-031-45360-1 ISBN 978-3-031-45361-8 (eBook) https://doi.org/10.1007/978-3-031-45361-8 © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2024 This work is subject to copyright. All rights are solely and exclusively licensed 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. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland Paper in this product is recyclable.
📄 Page
5
ISO/IEC 9899:1999 ISO/IEC 9899:2011 ISO/IEC 9899:2018 ISO/IEC 9899:2023 Preface C programing language was introduced in 1972. After its introduction, it became the most popular programming language in the world in short time. In every branch of science and engineering, developments occur in time, and every technology needs to update itself in time. Otherwise, it becomes old fashion in short time. Today, the popularity cycle of technologies is becoming shorter. C programming language was revised several times in the last several decades. The recent C standards are tabulated in Table 1. In Table 1, all the formal names start with the same expression IEC 9899, and after symbol “:,” the year of the standard is written, i.e., The year of C17 standard is 2018. Some people may use the informal name as C18. But in literature, it is seen that C17 was adopted by many authorities. Let’s briefly give information about these standards. Table 1 C language standards since 1999 Informal name Introduction year Formal name File name C99 1999 ISO/IEC 9899:1999 N1256.pdf C11 2011 ISO/IEC 9899:2011 N1570.pdf C17 2018 ISO/IEC 9899:2018 N2310.pdf C23 2023 ISO/IEC 9899:2023 N3096.pdf (last draft) v
📄 Page
6
vi Preface C99 Standard Formal name of this standard is ISO/IEC 9899:1999. This standard is published in 1999. The file name of this standard is N1256.pdf. Using internet search engines, all standard document can be found easily. C99 standard made major contribution to the C language; the standard introduced the header files <complex.h> complex number arithmetic <fenv.h> floating-point environment <inttypes.h> formats for fixed-width integer types <stdbool.h> macros for Boolean data type <stdint.h> fixed-width integer data types <tgmath.h> type-generic math With the introduction of these header files, it became much easier to perform complex mathematical calculations, and perform trigonometric operations. And fixed-width integers were useful for efficient use of the resources. New built-in data types introduced in C99 are long long, unsigned long long _Bool, _Complex, _Imaginary inline, restrict Variable length arrays were introduced in C99. Before C99, integer constants can be used for array sizes, and they must be defined before the array declaration. In C99 standard, we can declare an array whose size is known at run time. That is, you can get the array size from the user at run time. In C99, the keyword static can be used inside the square brackets during array declaration, for example: void myFunc(int a[static 20]) { // statements } Before C99, single line comments, //. . ., are not accepted. C99 introduced single line comments. In addition, before C99, variable declarations must be done at the beginning of the code; however, with C99, variable declarations can be made in any place of the code.
📄 Page
7
Preface vii C11 Standard Formal name of this standard is ISO/IEC 9899:2011. The file name where the standard is published is N1570.pdf. Using internet search engines, it is possible to find all the document standards easily. After C99 release, a major contribution to the C language is made by the C11 standard. The header files introduced in C11 standard are <stdalign.h> alignas and alignof macros <stdatomic.h> atomic operations <stdnoreturn.h> noreturn macro <threads.h> thread functions <uchar.h> UTF-16 and UTF-32 encoding With C11, standard parallel processing became possible using built in thread functions, and it was possible to avoid the race problem occurring in parallel processing using the atomic data types. Thread and atomic libraries were the powerful contribution of C11 standard. The keywords added by C11 standard are _Alignas _Alignof _Atomic _Generic _Noreturn _Static_assert _Thread_local C17 Standard The standard C17 does not introduce new language features. In this standard, just some minor defects of the C11 standard are fixed. C23 Standard The major contribution of C23 standard is the introduction of two new header files <stdckdint.h> functions and macros for checked integer arithmetic <stdbit.h> functions and macros for bit manipulation These files are useful to use the hardware as efficient as possible. In this book, starting from the most basic topics of C programming language, we cover new language features introduced in the standards. Chapter 1 focuses on the representation of numbers in computer. This chapter is usually skipped in many C
📄 Page
8
books. However, for a good understating of C programming, good knowledge of number representations in computer is needed. We advise the reader to study the first chapter before processing with C programming topics. Chapters 2, 3, 4, 5, 6, 8, and 9 cover the common topics of all the standards. Chapter 10 explains the pointers in C programming, and for good comprehension of this chapter, some knowledge about digital hardware fundamentals is needed. viii Preface Complex numbers and fixed bit length integers are covered in Chaps. 7 and 13. These concepts are introduced in C99 standard. In Chaps. 15 and 16, threads and atomic data types are explained. These concepts are introduced in standard C11 and they are also covered in C17 and C23. Macros, type qualifiers, storage classes, signals, enumerations included in all standards are covered in Chaps. 11, 12, and 14. This book can be adapted as a text or supplementary book for C programming for a one-semester course, or it can be read for personal development. I dedicate this book to my lovely brother Ilhan Gazi, and to those people who do something useful for humanity. Ankara, Turkiye Orhan Gazi Wednesday, August 2, 2023
📄 Page
9
Contents 1 Representation of Numbers and Characters in Computer . . . . . . . . 1 1.1 Number Bases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Decimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.2 Binary Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.3 Octal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.4 Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Conversion Between Bases . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.1 Binary to Decimal Conversion . . . . . . . . . . . . . . . . . 3 1.2.2 Binary to Octal Conversion . . . . . . . . . . . . . . . . . . . 3 1.2.3 Binary to Hexadecimal Conversion . . . . . . . . . . . . . 4 1.2.4 Decimal to Binary Conversion . . . . . . . . . . . . . . . . . 5 1.2.5 Octal to Binary Conversion . . . . . . . . . . . . . . . . . . . 6 1.2.6 Hexadecimal to Binary Conversion . . . . . . . . . . . . . 6 1.2.7 Hexadecimal to Decimal Conversion . . . . . . . . . . . . 7 1.3 Positive Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4 Two’s Complement Form . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.5 Negative Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.6 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.7 Memory Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.8 How Are the Integers Stored in Computer Memory, Big-Endian, and Little-Endian? . . . . . . . . . . . . . . . . . . . . . . . 12 2 Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1 How to Start Writing a C Program? . . . . . . . . . . . . . . . . . . . . 15 2.2 Comments in C Programming . . . . . . . . . . . . . . . . . . . . . . . . 17 2.3 The First C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.5 Binary Number Representation in Modern C . . . . . . . . . . . . . . 21 2.6 sizeof Operator in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.7 Unsigned Char Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ix
📄 Page
10
x Contents 2.8 Left and Right Shift Operators in C . . . . . . . . . . . . . . . . . . . . 25 2.9 Integer Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.10 Hexadecimal and Octal Numbers . . . . . . . . . . . . . . . . . . . . . . 28 2.11 How Are Integers Stored in Computer Memory? . . . . . . . . . . . 29 2.11.1 Short Integer Data Type . . . . . . . . . . . . . . . . . . . . . 30 2.12 Why Do We Have Both Integer and Short Integer Data Types? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.13 Long Integer and Long-Long Integer Data Types . . . . . . . . . . 32 2.14 Unsigned Integer Data Type . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.15 Floating-Point Number in C . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.15.1 IEEE 754 Floating-Point Standard (Single Precision) . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.16 Keyboard Input Using scanf in C . . . . . . . . . . . . . . . . . . . . . . 41 2.17 Operators in C Programming . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.17.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . 43 2.17.2 Remainder Operator % . . . . . . . . . . . . . . . . . . . . . . 46 2.17.3 Augmented Assignment Operators . . . . . . . . . . . . . . 47 2.17.4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.17.5 Bitwise Operators in C . . . . . . . . . . . . . . . . . . . . . . 50 2.17.6 Increment and Decrement Operators . . . . . . . . . . . . 60 2.18 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3 Type Conversion in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.1 Type Conversion Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.1.1 Implicit Conversion . . . . . . . . . . . . . . . . . . . . . . . . 67 3.1.2 Explicit Conversion . . . . . . . . . . . . . . . . . . . . . . . . 70 3.2 Information Loss When a Higher-Order Data Is Converted to a Lower-Order Data . . . . . . . . . . . . . . . . . . . . . . 73 3.3 Information Loss When Conversion Is Performed Between Signed and Unsigned Data Types . . . . . . . . . . . . . . . 74 4 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.2 Initialization of Structure Elements . . . . . . . . . . . . . . . . . . . . . 78 4.3 Initialization Using Designated Initializer List . . . . . . . . . . . . . 79 4.4 Typedef for Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.4.1 Alternative Use of typedef for Structures . . . . . . . . . 85 4.5 Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4.6 Structure Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.7 Structures with Self-Referential . . . . . . . . . . . . . . . . . . . . . . . 89 4.8 Bit Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 4.9 Structures as Function Arguments . . . . . . . . . . . . . . . . . . . . . 92 4.10 Structure Padding and Packing in C Programming . . . . . . . . . . 93 4.11 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
📄 Page
11
Contents xi 5 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.1 Conditional Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.2 Conditional Ladder Structure (-If Ladder) . . . . . . . . . . . . . . . . 104 5.3 Multiconditional Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5.4 Syntax of Nested If-Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 5.5 Conditional Operator in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.6 switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 6 Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.1 The For-Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.1.1 Nested For-Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.2 The While-Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 6.2.1 Nested While-Loop . . . . . . . . . . . . . . . . . . . . . . . . . 144 6.3 The Do-While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 6.4 Continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 6.5 Break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7 Complex Numbers in Modern C Programming . . . . . . . . . . . . . . . . 157 7.1 How to Define a Complex Number . . . . . . . . . . . . . . . . . . . . . 157 7.2 Complex Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 7.3 Calculation of Absolute Value (Norm, Modulus, or Magnitude) of a Complex Number . . . . . . . . . . . . . . . . . . . 161 7.4 Complex Number Formation . . . . . . . . . . . . . . . . . . . . . . . . . 161 7.5 Calculation of the Conjugate of a Complex Number in C . . . . . 162 7.6 Calculation of the Argument, That Is, Phase Angle, of a Complex Number in C . . . . . . . . . . . . . . . . . . . . . . . . . . 164 7.7 Calculation of Complex Exponentials . . . . . . . . . . . . . . . . . . . 164 7.8 Computation of the Complex Natural (Base-e) Logarithm of a Complex Number . . . . . . . . . . . . . . . . . . . . . . 165 7.9 Complex Power Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . 166 7.10 Square Root of a Complex Number . . . . . . . . . . . . . . . . . . . . 167 7.11 Complex Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . 168 7.11.1 The csin Functions . . . . . . . . . . . . . . . . . . . . . . . . . 168 7.11.2 The ccos Functions . . . . . . . . . . . . . . . . . . . . . . . . . 168 7.11.3 The ctan Functions . . . . . . . . . . . . . . . . . . . . . . . . . 169 7.11.4 The cacos Functions . . . . . . . . . . . . . . . . . . . . . . . . 170 7.11.5 The casin Functions . . . . . . . . . . . . . . . . . . . . . . . . 170 7.11.6 The catan Functions . . . . . . . . . . . . . . . . . . . . . . . . 171 7.11.7 Hyperbolic Functions . . . . . . . . . . . . . . . . . . . . . . . 172 8 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 8.1 Syntax for Array Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 175 8.2 Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 8.3 Array Initialization Without Size . . . . . . . . . . . . . . . . . . . . . . 177 8.4 Array Initialization Using Loops . . . . . . . . . . . . . . . . . . . . . . 179 8.5 Strings as Array of Characters . . . . . . . . . . . . . . . . . . . . . . . . 184
📄 Page
12
xii Contents 8.6 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 8.7 Passing an Array to a Function in C . . . . . . . . . . . . . . . . . . . . 189 9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 9.2 Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 9.3 Passing Parameters to Functions . . . . . . . . . . . . . . . . . . . . . . . 198 9.4 Returning More Than One Value . . . . . . . . . . . . . . . . . . . . . . 200 9.5 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 9.6 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 10 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 10.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 10.2 Address of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 10.3 NULL Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 10.4 Void Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 10.5 Types of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 10.5.1 Pointer to a Constant Value . . . . . . . . . . . . . . . . . . . 221 10.5.2 Pointer to a Constant Address (Constant Pointer) . . . 223 10.5.3 Constant Pointer to a Constant Value . . . . . . . . . . . . 224 10.6 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 10.6.1 Functions Returning Pointers . . . . . . . . . . . . . . . . . . 228 10.7 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 10.8 Multiple Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 10.9 Heap Stack and Code Memories . . . . . . . . . . . . . . . . . . . . . . . 239 10.10 Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . 240 10.10.1 malloc() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 241 10.10.2 calloc() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 245 10.10.3 free() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 10.10.4 realloc() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 247 10.11 Memory Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 10.11.1 Memset Function . . . . . . . . . . . . . . . . . . . . . . . . . . 248 10.11.2 Memcpy Function . . . . . . . . . . . . . . . . . . . . . . . . . 249 10.11.3 Memmove Function . . . . . . . . . . . . . . . . . . . . . . . . 250 10.11.4 Memcmp Function . . . . . . . . . . . . . . . . . . . . . . . . . 251 11 Directives and Macros in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 11.2 Preprocessor Directives as Macros . . . . . . . . . . . . . . . . . . . . . 259 11.3 Macros as Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 11.4 Multiline Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 11.5 Directives Used for File Inclusion . . . . . . . . . . . . . . . . . . . . . 265 11.6 Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 11.7 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 11.8 Concatenation Operator ## . . . . . . . . . . . . . . . . . . . . . . . . . . 274
📄 Page
13
Contents xiii 12 Type Qualifiers, Enumerations, and Storage Classes in C . . . . . . . . 277 12.1 Type Qualifiers in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 12.1.1 Const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 12.1.2 Restrict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 12.1.3 Volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 12.2 Storage Classes in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 12.2.1 Auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 12.2.2 Extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 12.2.3 Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 12.2.4 Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 13 Integer with Exactly N Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 13.1 General Form of Fixed Width Integers . . . . . . . . . . . . . . . . . . 289 13.2 Macros for printf and scanf . . . . . . . . . . . . . . . . . . . . . . . . . . 289 13.3 uintN_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 13.4 int_leastN_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 13.5 uint_leastN_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 13.6 int_fastN_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 13.7 uint_fastN_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 13.8 Macros for printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 13.9 Macros for scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 14 Signals in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 14.2 Signal Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 14.3 SIGINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 14.4 SIGQUIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 14.5 Artificial Signal Generation . . . . . . . . . . . . . . . . . . . . . . . . . . 310 14.6 Some of the Most Used Signals . . . . . . . . . . . . . . . . . . . . . . . 311 15 Threads in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 15.2 Thread Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 15.3 Parallel Processing Using Threads . . . . . . . . . . . . . . . . . . . . . 314 15.4 pthread_exit() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 15.5 pthread_join() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 15.6 pthread_self() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 15.7 pthread_equal() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 15.8 pthread_cancel() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 15.9 pthread_detach() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 15.10 Synchronizing Threads with Mutexes . . . . . . . . . . . . . . . . . . . 332
📄 Page
14
xiv Contents 16 Atomic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 16.1 How to Define an Atomic Data Type? . . . . . . . . . . . . . . . . . . 343 16.2 Atomic Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 16.3 Atomic Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 16.4 Race Prevention by Atomic Variables . . . . . . . . . . . . . . . . . . . 347 16.5 Lock-Free Atomic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 16.6 Atomic Assignments, Operators, and Functions . . . . . . . . . . . . 353 16.7 Atomic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 16.7.1 atomic_is_lock_free() Function . . . . . . . . . . . . . . . . 354 16.7.2 atomic_fetch_key() Function . . . . . . . . . . . . . . . . . . 354 16.7.3 atomic_store() Function . . . . . . . . . . . . . . . . . . . . . 357 16.7.4 atomic_load() Function . . . . . . . . . . . . . . . . . . . . . . 358 16.7.5 atomic_exchange() Function . . . . . . . . . . . . . . . . . . 359 16.7.6 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . 360 16.7.7 atomic_flag Macro . . . . . . . . . . . . . . . . . . . . . . . . . 362 16.7.8 atomic_init() Function . . . . . . . . . . . . . . . . . . . . . . . 363 16.8 Memory Order in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 16.8.1 Acquire, Release, and Consume . . . . . . . . . . . . . . . . 364 16.8.2 Memory Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 16.8.3 Atomic Functions with Memory Order . . . . . . . . . . . 367 17 File Operations in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 17.1 File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 17.2 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 17.2.1 Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 17.2.2 Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 17.2.3 Reading and Writing of a Text File . . . . . . . . . . . . . 371 17.2.4 Reading and Writing of a Binary File . . . . . . . . . . . . 379 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
📄 Page
15
Chapter 1 Representation of Numbers and Characters in Computer 1.1 Number Bases Number base is a positive integer, and a number with base N can contain digits less than N. 1.1.1 Decimal Numbers If base equals to 10, then all the digits forming a number should be less than or equal to 9. The numbers under base 10 are called decimal numbers. Example 1.1 We can write a few decimal numbers as 456 99988 67890 45433 In fact, in our daily life we use decimal numbers. 1.1.2 Binary Numbers If the base equals 2, then the numbers are called binary numbers, and binary numbers can be formed using the digits 0 and 1. Example 1.2 We can write a few binary numbers as 1011 10111101 101101010 11111111 © The Author(s), under exclusive license to Springer Nature Switzerland AG 2024 O. Gazi, Modern C Programming, https://doi.org/10.1007/978-3-031-45361-8_1 1
📄 Page
16
2 1 Representation of Numbers and Characters in Computer 1.1.3 Octal Numbers If the base equals 8, then the numbers formed under this base are called octal numbers, and an octal number can be formed using the digits 0, 1, 2, 3, 4, 5, 6, 7 Example 1.3 We can write a few octal numbers as 76403 22334 54634 1.1.4 Hexadecimal Numbers If the base equals 16, then the numbers formed under this base are called hexadec- imal numbers, and a hexadecimal number can be formed using the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F where the letters A, B, C, D, E, and F denote the numbers 10, 11, 12, 13, 14, 15: Example 1.4 We can write a few hexadecimal numbers as AB04 FF0A 456FEA09 We can use 0x or 0X prefix in front of the hexadecimal numbers. The hexadec- imal number in the example can be written either as 0xAB04 0xFF0A 0x456FEA09 or as 0XAB04 0XFF0A 0X456FEA09 but usually small x, that is, 0x, is preferred.
📄 Page
17
1.2 Conversion Between Bases 3 1.2 Conversion Between Bases Assume that we have a number under a base. The equivalent of this number in another base can be calculated. This procedure is called conversion between bases. 1.2.1 Binary to Decimal Conversion The n-bit binary number bn- 1bn- 2 . . . b1b0 can be converted to decimal as bn- 12 n- 1 þ bn- 22 n- 2 þ . . .þ b121 þ b020 Example 1.5 The decimal equivalent of 10110 can be calculated as 24 þ 0 × 23 þ 1× 22 þ 1× 21 þ 0× 20 which is equal to 22 1.2.2 Binary to Octal Conversion To convert a binary number to octal, starting from the rightmost position we first divide the binary string into groups having 3 bits, and then convert each 3 bits to an octal number. Example 1.6 Convert the binary number 1010001101011 to a number in octal base.
📄 Page
18
where the leftmost bit can be left padded by zeros to make a group of 3 bits as where the left most bit can be left padded by zeros to make a group of 3 bits as 4 1 Representation of Numbers and Characters in Computer Solution 1.6 Starting from the rightmost position, we first divide the binary string into groups having 3 bits as 1 010 001 101 011 1 010 001 101 011 and we convert each group to an octal number as in 001 1 010 2 001 1 101 5 011 3 Thus, the equivalent octal number is 12153 1.2.3 Binary to Hexadecimal Conversion To convert a binary number to octal, starting from the rightmost position, we first divide the binary string into groups each having 4 bits, and then convert each 4 bits to a hexadecimal number. Example 1.7 Convert the binary number 11010001101011 to a number in hexadecimal base. Solution 1.7 Starting from the rightmost position, we first divide the binary string into groups having 3 bits as 11 0100 0110 1011 11 0100 0110 1011 and we convert each group to a hexadecimal number as in
📄 Page
19
1.2 Conversion Between Bases 5 0011 3 0100 4 0110 6 1011 B Thus, the equivalent hexadecimal number is 346B 1.2.4 Decimal to Binary Conversion A decimal number can be converted to a binary number using successive division operation. In this method, the decimal number is divided by 2 and the remainder is recorded, the dividend is again divided by 2 and the remainder is recorded. This procedure is repeated with dividends until no more division operation can be achieved. Example 1.8 Convert the decimal number 351 to binary. Solution 1.8 We divide 351 by 2 and remainder equals 1 and dividend equals 350. We indicate this division operation as on the right-hand side of Fig. 1.1. If we continue division operation in a successive manner, we obtain Fig. 1.2. Finally, we collect the binary numbers from bottom to top as depicted in Fig. 1.3 and obtain 101011111 Exercise Find the decimal equivalent of the binary number 101011111 Fig. 1.1 Decimal to binary conversion for Example 1.8 351 1 175 351 2 175350 1 Fig. 1.2 Successive division for Example 1.8 351 1 175 1 87 1 43 1 21 1 10 0 5 1 2 0 1
📄 Page
20
6 1 Representation of Numbers and Characters in Computer Fig. 1.3 Bits are collected from bottom to top 351 1 175 1 87 1 43 1 21 1 10 0 5 1 2 0 1 1.2.5 Octal to Binary Conversion To convert an octal number to binary, we first convert each octal digit to a binary string each having 3 bits, then concatenate all the bits and obtain the binary equivalent of the octal number. Example 1.9 Convert the octal number 763015 to binary. Solution 1.9 We first express each octal digit by 3 bits as shown in 7 111 6 110 3 011 0 000 1 001 5 101 and we concatenate the bits and obtain the binary equivalent number as 111 110 011 000 001 101 where removing spaces we get 111110011000001101 1.2.6 Hexadecimal to Binary Conversion To convert a hexadecimal number to binary, we first convert each hexadecimal digit to a binary string each having 4 bits, and concatenating all the bits we obtain the binary equivalent of the hexadecimal number. Example 1.10 Convert the hexadecimal number 0x1AF395 to binary. Solution 1.10 We first express each hexadecimal digit by 4 bits as shown in 1 0001 A 1010 F 1111 3 0011 9 1001 5 0101 then we concatenate the bits and obtain the binary equivalent number as
The above is a preview of the first 20 pages. Register to read the complete e-book.