We use cookies to enhance your experience on our website. By continuing to use our website, you are agreeing to our use of cookies. You can change your cookie settings at any time. Find out more
Cover

Programming in C

Second Edition

Reema Thareja

February 2016

ISBN: 9780199456147

468 pages
Paperback
216x138mm

Price: £18.99

Share:

Description

This second edition of Programming in C is designed to serve as a textbook for the undergraduate students of engineering, computer applications, and computer science for a basic course on C programming. It provides a comprehensive coverage of the fundamental concepts of C programming.

  • Employs a very lucid style of presentation which makes the concepts easy to understand
  • Includes a chapter on Developing Efficient Programs, which details steps for developing correct, efficient, and maintainable programs
  • Provides more than 200 programming examples with outputs to illustrate the concepts
  • Includes case studies after selected chapters to help readers develop a practical understanding of the concepts learnt
  • Provides numerous chapter-end exercises to test the understanding of the theory

About the Author(s)

Reema Thareja, Assistant Professor, Department of Computer Science, Shyama Prasad Mukherjee College for Women, University of Delhi

Reema Thareja is Assistant Professor at Department of Computer Science, Shyama Prasad Mukherji College for Women, University of Delhi. She has completed MCA (Software Engineering) and MPhil (Computer Science) and is currently pursuing research in the area of improving data warehouse quality. She has about 10 years of teaching experience and specializes in programming languages, operating systems, microprocessors, DBMS, multimedia, and web technologies. Prof. Thareja has published several research papers in national and international journals of repute. She is the member of Computer Society of India (CSI).

Table of Contents

    1 Introduction to Programming
    1.1 Introduction to Computer Software 1
    1.2 Classification of Computer Software 2
    1.2.1 System Software 2
    1.2.2 Application Software 5
    1.3 Programming Languages 5
    1.4 Generation of Programming Languages 6
    1.4.1 First Generation: Machine Language 6
    1.4.2 Second Generation: Assembly Language 7
    1.4.3 Third Generation Programming Languages 7
    1.4.4 Fourth Generation: Very High-level Languages 8
    1.4.5 Fifth Generation Programming Languages 8
    2 Introduction to C
    2.1 Introduction 12
    2.1.1 Background 12
    2.1.2 Characteristics of C 13
    2.1.3 Uses of C 14
    2.2 Structure of a C Program 14
    2.3 Writing the First C Program 15
    2.4 Files Used in a C Program 16
    2.4.1 Source Code Files 16
    2.4.2 Header files 16
    2.4.3 Object Files 17
    2.4.4 Binary Executable Files 17
    2.5 Compiling and Executing C Programs 17
    2.6 Using Comments 18
    2.7 C tokens 19
    2.8 Character Set in C 19
    2.9 Keywords 19
    2.10 Identifiers 20
    2.10.1 Rules for Forming Identifier Names 20
    2.11 Basic Data Types in C 20
    2.11.1 How are Float and Double Stored? 21
    2.12 Variables 22
    2.12.1 Numeric Variables 22
    2.12.2 Character Variables 22
    2.12.3 Declaring Variables 22
    2.12.4 Initializing Variables 22
    2.13 Constants 23
    2.13.1 Integer Constants 23
    2.13.2 Floating Point Constants 23
    2.13.3 Character Constants 24
    2.13.4 String Constants 24
    2.13.5 Declaring Constants 24
    2.14 Input/Output Statements in C 24
    2.14.1 Streams 24
    2.14.2 Formatting input/output 25
    2.14.3 printf() 25
    2.14.4 scanf() 28
    2.14.5 Examples of printf/scanf 30
    2.14.6 Detecting Errors During Data Input
    2.15 Operators in C 32
    2.15.1 Arithmetic Operators 32
    2.15.2 Relational Operators 34
    2.15.3 Equality Operators 35
    2.15.4 Logical Operators 35
    2.15.5 Unary Operators 36
    2.15.6 Conditional Operator 37
    2.15.7 Bitwise Operators 38
    2.15.8 Assignment Operators 39
    2.15.9 Comma Operator 40
    2.15.10 Sizeof Operator 40
    2.15.11 Operator Precedence Chart 40
    2.16 Type Conversion and Typecasting 46
    2.16.1 Type Conversion 46
    2.16.2 Typecasting 47
    Annexure 1 56
    3 Decision Control and Looping Statements
    3.1 Introduction to Decision Control Statements 57
    3.2 Conditional Branching Statements 57
    3.2.1 if Statement 57
    3.2.2 IfElse Statement 59
    3.2.3 IfElseIf Statement 61
    3.2.4 Switch Case 65
    3.3 Iterative Statements 69
    3.3.1 While loop 69
    3.3.2 Do-while Loop 72
    3.3.3 For Loop 75
    3.4 Nested Loops 78
    3.5 The Break and Continue Statements 87
    3.5.1 break Statement 87
    3.5.2 continue Statement 88
    3.6 goto Statement 89
    Case Study 1: Chapters 2 and 3 101
    4 Functions
    4.1 Introduction 105
    4.1.1 Why are functions needed? 105
    4.2 Using Functions 106
    4.3 Function Declaration/Function Prototype 107
    4.4 Function Definition 108
    4.5 Function Call 108
    4.5.1 Points to Remember While Calling Functions 109
    4.6 Return Statement 110
    4.6.1 Using Variable Number of Arguments 110
    4.7 Passing Parameters to Functions 111
    4.7.1 Call by Value 111
    4.7.2 Call by Reference 112
    4.8 Scope of Variables 115
    4.8.1 Block Scope 115
    4.8.2 Function Scope 116
    4.8.3 Program Scope 116
    4.8.4 File Scope 117
    4.9 Storage Classes 117
    4.9.1 auto Storage Class 117
    4.9.2 register Storage Class 118
    4.9.3 extern Storage Class 119
    4.9.4 static Storage Class 119
    4.9.5 Comparison of Storage Classes 120
    4.10 Recursive Functions 120
    4.10.1 Greatest Common Divisor 122
    4.10.2 Finding Exponents 122
    4.10.3 The Fibonacci Series 123
    4.11 Types of Recursion 123
    4.11.1 Direct Recursion 123
    4.11.2 Indirect Recursion 123
    4.11.3 Tail Recursion 123
    4.11.4 Linear and Tree Recursion 124
    4.12 Tower of Hanoi 124
    4.13 Recursion Versus Iteration 126
    Annexure 2 133
    5 Arrays
    5.1 Introduction 134
    5.2 Declaration of Arrays 135
    5.3 Accessing the Elements of an Array 136
    5.3.1 Calculating the Address of Array Elements 136
    5.3.2 Calculating the Length of an Array 137
    5.4 Storing Values in Arrays 137
    5.4.1 Initializing Arrays during Declaration 137
    5.4.2 Inputting Values from the Keyboard 138
    5.4.3 Assigning Values to Individual Elements 138
    5.5 Operations on Arrays 138
    5.5.1 Traversing an Array 139
    5.5.2 Inserting an Element in an Array 144
    5.5.3 Deleting an Element from an Array 146
    5.5.4 Merging Two Arrays 148
    5.5.5 Searching for a Value in an Array 150
    5.6 Passing Arrays to functions 153
    5.7 Two-dimensional Arrays 156
    5.7.1 Declaring Two-dimensional Arrays 156
    5.7.2 Initializing Two-dimensional Arrays 158
    5.7.3 Accessing the Elements of Two-dimensional Arrays 158
    5.8 Operations on Two-dimensional Arrays 161
    5.9 Passing Two-Dimensional Arrays to Functions 164
    5.9.1 Passing a Row 164
    5.9.2 Passing an Entire 2D Array 165
    5.10 Multidimensional Arrays 167
    5.11 Sparse Matrices 168
    5.11.1 Array Representation of Sparse Matrices 169
    5.12 Applications of Arrays 170
    Case Study 2: Chapter 5 175
    6 Strings
    6.1 Introduction 180
    6.1.1 Reading Strings 182
    6.1.2 Writing Strings 182
    6.1.3 Summary of Functions Used to Read and Write Characters 183
    6.2 Suppressing Input 184
    6.2.1 Using a Scanset 184
    6.3 String Taxonomy 185
    6.4 Operations on Strings 186
    6.4.1 Finding the Length of a String 186
    6.4.2 Converting Characters of a String into Upper Case 187
    6.4.3 Converting Characters of a String Into Lower Case 188
    6.4.4 Concatenating Two Strings to Form a New String 188
    6.4.5 Appending a String to Another String 189
    6.4.6 Comparing two strings 189
    6.4.7 Reversing a String 190
    6.4.8 Extracting a Substring from Left 191
    6.4.9 Extracting a Substring from Right of the String 192
    6.4.10 Extracting a Substring from the Middle of a String 192
    6.4.11 Inserting a String in Another String 193
    6.4.12 Indexing 194
    6.4.13 Deleting a String from the Main String 194
    6.4.14 Replacing a Pattern with Another Pattern in a String 195
    6.5 Miscellaneous String and Character Functions 196
    6.5.1 Character Manipulation Functions 196
    6.5.2 String Manipulation Functions 196
    6.6 Arrays of Strings 202
    7 Pointers
    7.1 Understanding the Computers Memory 213
    7.2 Introduction to Pointers 214
    7.3 Declaring Pointer Variables 215
    7.4 Pointer Expressions and Pointer Arithmetic 217
    7.5 Null Pointers 221
    7.6 Generic Pointers 222
    7.7 Passing Arguments to Function Using Pointers 222
    7.8 Pointers and Arrays 223
    7.9 Passing an Array to a Function 227
    7.10 Difference Between Array Name and Pointer 228
    7.11 Pointers and Strings 229
    7.12 Arrays of Pointers 232
    7.13 Pointers and 2D Arrays 234
    7.14 Pointers and 3D Arrays 236
    7.15 Function Pointers 237
    7.15.1 Initializing a Function Pointer 237
    7.15.2 Calling a Function Using a Function Pointer 237
    7.15.3 Comparing Function Pointers 238
    7.15.4 Passing a Function Pointer as an Argument to a Function 238
    7.16 Array of Function Pointers 238
    7.17 Pointers to Pointers 239
    7.18 Memory Allocation in C Programs 240
    7.19 Memory Usage 240
    7.20 Dynamic Memory Allocation 240
    7.20.1 Memory Allocations Process 241
    7.20.2 Allocating a Block of Memory 241
    7.20.3 Releasing the Used Space 242
    7.20.4 To Alter the Size of Allocated Memory 242
    7.21 Drawbacks of Pointers 244
    Annexure 3 253
    Case Study 3: Chapter 6 and 7 256
    8 Structure, Union, and Enumerated Data Types
    8.1 Introduction 259
    8.1.1 Structure Declaration 259
    8.1.2 Typedef Declarations 261
    8.1.3 Initialization of Structures 261
    8.1.4 Accessing the Members of a Structure 262
    8.1.5 Copying and Comparing Structures 262
    8.2 Nested Structures 265
    8.3 Arrays of Structures 266
    8.4 Structures and Functions 268
    8.4.1 Passing Individual Members 268
    8.4.2 Passing the Entire Structure 268
    8.4.3 Passing Structures Through Pointers 271
    8.5 Self-referential Structures 276
    8.6 Unions 276
    8.6.1 Declaring a Union 276
    8.6.2 Accessing a Member of a Union 277
    8.6.3 Initializing Unions 277
    8.7 Arrays of Union Variables 278
    8.8 Unions Inside Structures 278
    8.9 Structures Inside Unions 279
    8.10 Enumerated Data Type 279
    8.10.1 enum Variables 280
    8.10.2 Using the Typedef Keyword 281
    8.10.3 Assigning Values to Enumerated Variables 281
    8.10.4 Enumeration Type Conversion 281
    8.10.5 Comparing Enumerated Types 281
    8.10.6 Input/Output Operations on Enumerated Types 281
    Annexure 4 288
    9 Files
    9.1 Introduction to Files 290
    9.1.1 Streams in C 290
    9.1.2 Buffer Associated with File Stream 291
    9.1.3 Types of Files 291
    9.2 Using Files in C 292
    9.2.1 Declaring a File Pointer Variable 292
    9.2.2 Opening a File 292
    9.2.3 Closing a File Using fclose () 294
    9.3 Read Data From Files 294
    9.3.1 fscanf () 294
    9.3.2 fgets () 295
    9.3.3 fgetc () 296
    9.3.4 fread () 296
    9.4 Writing Data to Files 297
    9.4.1 fprintf () 297
    9.4.2 fputs () 299
    9.4.3 fputc () 299
    9.4.4 fwrite () 299
    9.5 Detecting the End-of-file 300
    9.6 Error Handling During File Operations 301
    9.6.1 clearerr() 301
    9.6.2 perror () 302
    9.7 Accepting Command Line Arguments 302
    9.8 Functions for Selecting a Record Randomly 316
    9.8.1 fseek () 316
    9.8.2 ftell () 318
    9.8.3 rewind () 318
    9.8.4 fgetpos () 319
    9.8.5 fsetpos () 319
    9.9 remove () 320
    9.10 Renaming the File 320
    9.11 Creating a Temporary File 320
    10 Preprocessor Directives
    10.1 Introduction 325
    10.2 Types of Preprocessor Directives 325
    10.3 #define 326
    10.3.1 Object-like Macro 326
    10.3.2 Function-like Macros 327
    10.3.3 Nesting of Macros 328
    10.3.4 Rules for Using Macros 328
    10.3.5 Operators Related to Macros 328
    10.4 #include 329
    10.5 #undef 330
    10.6 #line 330
    10.7 Pragma Directives 331
    10.8 Conditional Directives 333
    10.8.1 #ifdef 333
    10.8.2 #ifndef 333
    10.8.3 #if Directive 334
    10.8.4 #else Directive 334
    10.8.5 #elif Directive 334
    10.8.6 #endif Directive 335
    10.9 Defined Operator 335
    10.10 #error directive 336
    10.11 Predefined Macro Names 336
    Annexure 5 340
    11 Linked Lists
    11.1 Introduction 344
    11.2 Linked Lists Versus Arrays 345
    11.3 Memory Allocation and Deallocation for a Linked List 346
    11.4 Different Types of Linked Lists 347
    11.5 Singly Linked Lists 348
    11.5.1 Traversing a Singly Linked List 348
    11.5.2 Searching for a Value in a Linked List 348
    11.5.3 Inserting a New Node in a Linked List 349
    11.6 Circular Linked Lists 357
    11.7 Doubly Linked Lists 358
    11.8 Circular Doubly Linked Lists 359
    11.9 Header Linked Lists 359
    11.10 Applications of Linked Lists 360
    Case Study 4: Chapter 8, 9, and 11 366
    12 Stacks and Queues
    12.1 Stacks 369
    12.2 Array Representation of Stacks 370
    12.3 Operations on Stacks 370
    12.3.1 Push Operation 371
    12.3.2 Pop Operation 371
    12.3.3 Peep Operation 371
    12.4 Applications of Stacks 373
    12.4.1 Evaluation of Algebraic Expressions 373
    12.5 Queues 380
    12.6 Array Representation of Queues 380
    12.7 Operations on Queues 380
    12.8 Applications of Queues 382
    Case Study 5: Chapters 11 and 12 366
    13.
    13 Trees
    13.1 Binary Trees 391
    13.1.1 Key Terms 392
    13.1.2 Complete Binary Trees 393
    13.1.3 Extended Binary Trees 393
    13.1.4 Representation of Binary Trees in Memory 394
    13.2 Expression Trees 395
    13.3 Traversing a Binary Tree 396
    13.3.1 Pre-order Algorithm 396
    13.3.2 In-order Algorithm 397
    13.3.3 Post-order Algorithm 398
    13.3.4 Level-order Traversal 398
    13.4 Applications of Trees 398
    Case Study 6: Chapter 13 403
    14 Graphs
    14.1 Introduction 407
    14.1.1 Why Graphs are Useful? 407
    14.1.2 Definition 407
    14.1.3 Graph Terminology 408
    14.1.4 Directed Graphs 409
    14.2 Representation of Graphs 409
    14.2.1 Adjacency Matrix Representation 409
    14.2.2 Adjacency List 411
    14.3 Graph Traversal Algorithms 413
    14.3.1 Breadth-first Search 413
    14.3.2 Depth-first Search Algorithm 415
    14.4 Applications of Graphs 417
    Case Study 7: Chapter 14 421
    15 Developing Efficient Programs
    15.1 Modularization 426
    15.2 Design and Implementation of Efficient Programs 427
    15.2.1 Requirements Analysis 428
    15.2.2 Design 428
    15.2.3 Implementation 428
    15.2.4 Testing 428
    15.2.5 Software Deployment, Training, and Support 428
    15.2.6 Maintenance 428
    15.3 Program Design Tools: Algorithms, Flowcharts, Pseudocodes 429
    15.3.1 Algorithms 429
    15.3.2 Flowcharts 430
    15.3.3 Pseudocode 431
    15.4 Types of Errors 431
    15.4.1 Testing and Debugging Approaches 432
    Appendix A: Versions of C 436
    Appendix B: ASCII Chart of Characters 441
    Appendix C: ANSI C Library Functions 443
    Appendix D: Type Qualifiers and Inline Functions 450
    Appendix E: Bit-level Programming and Bitwise Shift Operators 454
    Appendix F: Answers to Objective Questions 457