Statistics
312
Views
124
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-08-16

AuthorKamthane, Ashok N

No description

Tags
No tags
ISBN: 9332558329
Publisher: Pearson India Education Services
Publish Year: 2016
Language: 英文
File Format: PDF
File Size: 14.5 MB
Support Statistics
¥.00 · 0times
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.

(This page has no text content)
PROGRAMMING IN C third Edition Ashok N. Kamthane Associate Professor Shri Guru Gobind Singhji Institute of Engineering and Technology, Nanded, Maharashtra, India A01_KAMT3553_02_SE_FM.indd 1 5/17/2015 8:59:35 AM
DEDICATION To My Beloved Late Grandfather Jagganath Kamthane Associate Editor—Acquisitions: Neha Goomer Editor—Production: G Sharmilee Copyright © 2015 Pearson India Education Services Pvt. Ltd Copyright © 2006, 2012 Dorling Kindersley (India) Pvt. Ltd This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, resold, hired out, or otherwise circulated without the publisher’s prior written consent in any form of binding or cover other than that in which it is published and without a similar condition including this condition being imposed on the subsequent purchaser and without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of both the copyright owner and the publisher of this book. ISBN 978-93-325-4355-3 First Impression Published by Pearson India Education Services Pvt. Ltd, CIN: U72200TN2005PTC057128, formerly known as TutorVista Global Pvt. Ltd, licensee of Pearson Education in South Asia. Head Office: A-8(A), 7th Floor, Knowledge Boulevard, Sector 62, Noida 201 309, Uttar Pradesh, India. Registered Office: Module G4, Ground Floor, Elnet Software City, TS-140, Block 2 & 9, Rajiv Gandhi Salai, Taramani, Chennai 600 113, Tamil Nadu, India. Fax: 080-30461003, Phone: 080-30461060 www.pearson.co.in, Email: companysecretary.india@pearson.com Compositor: Mukesh Technologies Pvt. Ltd Printed in India A01_KAMT3553_02_SE_FM.indd 2 5/17/2015 8:59:36 AM
Preface xi Acknowledgements xiv About the Author xv 1 Basics and introduction to C .............................................................. 2 1.1 Why to Use Computers? 3 1.2 Basics of a Computer 3 1.3 Latest Computers 5 1.4 Introduction to C 6 1.5 About ANSI C Standard 7 1.6 Machine, Assembly and High-Level Language 7 1.6.1   Assembly Language  8 1.6.2   High-Level Language  8 1.7 Assembler, Compiler and Interpreter 8 1.8 Structure of a C Program 10 1.9 Programming Rules 12 1.10 Executing the C Program 12 1.11 Standard Directories 16 1.12 The First C Program 16 1.13 Advantages of C 17 1.14 Header Files 17 1.15 Algorithm 18 1.15.1   Program Design  20 1.16 Classification of Algorithms 21 1.17 Flowcharts 22 1.18 Pseudocode 26 Summary  28 Exercises  28 2 the C declarations ............................................................................ 32 2.1 Introduction 33 2.2 The C Character Set 33 2.3 Delimiters 34 CONteNts A01_KAMT3553_02_SE_FM.indd 3 5/17/2015 8:59:36 AM
iv Contents 2.4 Types of Tokens 35 2.5 The C Keywords 35 2.6 Identifiers 35 2.7 Constants 36 2.7.1   Numerical Constants  37 2.7.2   Character Constant  38 2.8 Variables 39 2.9 Rules for Defining Variables 39 2.10 Data Types 40 2.11 C Data Types 41 2.12 Integer and Float Number Representations 43 2.12.1   Integer Representation  43 2.12.2   Floating-Point Representation  44 2.13 Declaring Variables 44 2.14 Initializing Variables 45 2.15 Dynamic Initialization 46 2.16 Type Modifiers 46 2.17 Type Conversion 47 2.18 Wrapping Around 49 2.19 Constant and Volatile Variables 50 2.19.1   Constant Variable  50 2.19.2   Volatile Variable  50 Summary  51 Exercises  52 3 operators and Expressions .............................................................. 58 3.1 Introduction 59 3.2 Operator Precedence 59 3.3 Associativity 62 3.4 Comma and Conditional Operator 63 3.5 Arithmetic Operators 64 3.6 Relational Operators 67 3.7 Assignment Operators and Expressions 68 3.8 Logical Operators 71 3.9 Bitwise Operators 74 Summary  80 Exercises  80 A01_KAMT3553_02_SE_FM.indd 4 5/17/2015 8:59:36 AM
Contents v 4 input and output in C ........................................................................ 84 4.1 Introduction 85 4.2 Formatted Functions 86 4.3 Flags, Widths and Precision with Format String 88 4.4 Unformatted Functions 99 4.5 Commonly Used Library Functions 104 4.6 Strong Points for Understandability 105 Summary  106 Exercises  106 5 decision Statements ....................................................................... 112 5.1 Introduction 113 5.2 The if Statement 114 5.3 The if–else Statement 116 5.4 Nested if–else Statements 120 5.5 The if-else-if Ladder Statement 121 5.6 The break Statement 129 5.7 The continue Statement 130 5.8 The goto Statement 130 5.9 The switch Statement 132 5.10 Nested switch case 144 5.11 The switch case and nested ifs 146 Summary  147 Exercises  148 6 Loop Control .................................................................................... 154 6.1 Introduction 155 6.1.1   What is a Loop?  155 6.2 The for Loop 156 6.3 Nested for Loops 172 6.4 The while Loop 194 6.5 The do-while Loop 207 6.6 The while Loop within the do-while Loop 212 6.7 Bohm and Jacopini’s Theory 213 Summary  213 Exercises  213 A01_KAMT3553_02_SE_FM.indd 5 5/17/2015 8:59:36 AM
vi Contents 7 data Structure: Array ...................................................................... 220 7.1 Introduction 221 7.2 Array Declaration 221 7.3 Array Initialization 222 7.4 Array Terminology 223 7.5 Characteristics of an Array 224 7.6 One-Dimensional Array 226 7.7 One-Dimensional Array and Operations 238 7.8 Operations with Arrays 240 7.9 Predefined Streams 246 7.10 Two-Dimensional Array and Operations 251 7.10.1   Insert Operation with Two-Dimensional Array  253 7.10.2   Delete Operation with Two-Dimensional Array  255 7.11 Three- or Multi-Dimensional Arrays 269 7.12 The sscanf() and sprintf() Functions 274 7.13 Drawbacks of Linear Arrays 276 Summary  276 Exercises  277 8 Strings and Standard Functions ........................................................ 286 8.1 Introduction 287 8.2 Declaration and Initialization of String 287 8.3 Display of Strings with Different Formats 289 8.4 String Standard Functions 291 8.5 String Conversion Functions 315 8.6 Memory Functions 316 8.7 Applications of Strings 318 Summary  328 Exercises  328 9 Pointers ........................................................................................... 336 9.1 Introduction 337 9.2 Features of Pointers 338 9.3 Pointers and Address 338 9.4 Pointer Declaration 339 9.5 The Void Pointers 345 9.6 Wild Pointers 346 9.7 Constant Pointers 347 9.8 Arithmetic Operations with Pointers 348 A01_KAMT3553_02_SE_FM.indd 6 5/17/2015 8:59:36 AM
Contents vii 9.9 Pointers and Arrays 352 9.10 Pointers and Two-Dimensional Arrays 357 9.11 Pointers and Multi-Dimensional Arrays 358 9.12 Array of Pointers 360 9.13 Pointers to Pointers 362 9.14 Pointers and Strings 364 Summary  371 Exercises  371 10 Functions ......................................................................................... 378 10.1 Introduction 379 10.2 Basics of a Function 379 10.2.1   Why Use Functions?  379 10.2.2   How a Function Works?  380 10.3 Function Definition 380 10.4 The return Statement 384 10.5 Types of Functions 386 10.6 Call by Value and Reference 393 10.7 Function Returning More Values 395 10.8 Function as an Argument 396 10.9 Function with Operators 399 10.10 Function and Decision Statements 406 10.11 Function and Loop Statements 409 10.12 Functions with Arrays and Pointers 412 10.13 Passing Array to a Function 418 10.14 Nested Functions 420 10.15 Recursion 422 10.16 Types of Recursion 424 10.17 Rules for Recursive Function 425 10.18 Direct Recursion 427 10.19 Indirect Recursion 428 10.20 Recursion Versus Iterations 430 10.21 The Towers of Hanoi 432 10.22 Advantages and Disadvantages of Recursion 435 10.23 Efficiency of Recursion 436 10.24 Library Functions 437 Summary  438 Exercises  438 A01_KAMT3553_02_SE_FM.indd 7 5/17/2015 8:59:36 AM
viii Contents 11 Storage Classes .............................................................................. 446 11.1 Introduction 447 11.1.1   Lifetime of a Variable  447 11.1.2   Visibility of a Variable  448 11.2 Automatic Variables 448 11.3 External Variables 450 11.4 Static Variables 453 11.5 Static External Variables 454 11.6 Register Variables 455 Summary  456 Exercises  456 12 Preprocessor directives ................................................................. 462 12.1 Introduction 463 12.2 The #define Directive 463 12.3 Undefining a Macro 466 12.4 Token Pasting and Stringizing Operators 467 12.5 The #include Directive 468 12.6 Conditional Compilation 469 12.7 The #ifndef Directive 471 12.8 The #error Directive 472 12.9 The #line Directive 473 12.10 The #pragma inline Directive 473 12.11 The #pragma saveregs 473 12.12 The #pragma Directive 473 12.13 The Predefined Macros in ANSI and TURBO-C 475 12.14 Standard I/O Predefined Streams in stdio.h 478 12.15 The Predefined Marcos in ctype.h 478 12.16 Assertions 480 Summary  481 Exercises  481 13 Structure and Union ........................................................................ 490 13.1 Introduction 491 13.2 Features of Structures 491 13.3 Declaration and Initialization of Structures 492 13.4 Structure within Structure 496 A01_KAMT3553_02_SE_FM.indd 8 5/17/2015 8:59:36 AM
Contents ix 13.5 Array of Structures 498 13.6 Pointer to Structure 501 13.7 Structure and Functions 504 13.8 typedef 506 13.9 Bit Fields 510 13.10 Enumerated Data Type 513 13.11 Union 517 13.12 Calling BIOS and DOS Services 518 13.13 Union of Structures 524 Summary  524 Exercises  525 14 Files ................................................................................................. 532 14.1 Introduction of a File 533 14.2 Definition of File 533 14.3 Streams and File Types 534 14.3.1   File Types  534 14.4 Steps for File Operations 535 14.4.1   Opening of File  536 14.4.2   Reading a File  536 14.4.3   Closing a File  536 14.4.4   Text Modes  537 14.4.5   Binary Modes  543 14.5 File I/O 545 14.6 Structures Read and Write 553 14.7 Other File Function 558 14.8 Searching Errors in Reading/Writing Files 561 14.9 Low-Level Disk I/O 572 14.10 Command Line Arguments 576 14.11 Application of Command Line Arguments 577 14.12 Environment Variables 579 14.13 I/O Redirection 580 Summary  582 Exercises  582 15 Graphics .......................................................................................... 590 15.1 Introduction 591 15.2 Initialization of Graphics 591 15.3 Few Graphics Functions 591 A01_KAMT3553_02_SE_FM.indd 9 5/17/2015 8:59:36 AM
x Contents 15.4 Programs Using Library Functions 593 15.4.1   Program on Moving Moon  596 15.5 Working with Text 598 15.5.1   Stylish Lines  599 15.6 Filling Patterns with Different Colours and Styles 600 15.7 Mouse Programming 604 15.8 Drawing Non-common Figures 607 Summary  608 Exercises  608 16 dynamic Memory Allocation and Linked List ................................ 612 16.1 Dynamic Memory Allocation 613 16.2 Memory Models 613 16.3 Memory Allocation Functions 614 16.4 List 621 16.5 Traversal of a List 621 16.6 Searching and Retrieving an Element 622 16.7 Predecessor and Successor 623 16.8 Insertion 624 16.9 Linked Lists 626 16.10 Linked List with and without Header 627 16.10.1   Linked List with Header 627 Summary  636 Exercises  637 Appendix A ................................................................................. 641 American Standard Code for Information Interchange 641 Appendix B ................................................................................. 649 Priority of Operators and Their Clubbing 649 Appendix C ................................................................................. 651 Header Files and Standard Library Functions 651 Appendix d ................................................................................. 655 ROM-BIOS Services 655 Appendix E ................................................................................. 657 Scan Codes of Keyboard Keys 657 index ....................................................................................... 660 A01_KAMT3553_02_SE_FM.indd 10 5/17/2015 8:59:36 AM
I am indeed very delighted to present the third edition of Programming in C with elaborated concepts supported with more solved and unsolved problems. I have tried to make the book friendly using simple and lucid language. In this edition, a chapter on Graphics with thought provoking questions and programming examples are added. This book is proposed for beginners, intermediate level students, and for all those who are pursing education in computers. It would be extremely useful for the students who enroll for diploma, degree in science and engineering, certificate courses in computer languages in training institutes or those who appear for the C aptitude tests/interviews on C Language conducted by various software companies and enhance their C knowledge. It can be used as a reference book for those who want to learn or enrich their knowledge in C. All the programs given in this book are compiled and run on Turbo C compiler. A few applications are provided in this book which are fully tested and run on Turbo C compiler. The programmer can develop advanced applications based on real-life problems using basics of C language. It contains numerous examples which include solved and unsolved programming exercises that make the book most interesting. Multiple choice questions are also provided at the end of each chapter for testing the skills of a programmer. An attempt has been made to cover the C syllabi of different branches of various universities. Hence this book can be adopted as a text or a reference book in engineering/degree/diploma and other courses. In order to bridge the gap between theory and practical, each concept is explained at length in an easy-to-understand manner supported with numerous worked-out examples and programs. The book contains solved illustrative problems and exercises. The programmer can run the solved programs, can see the output and enjoy the concepts of C. BOOK ORGANIZATION The first chapter describes the fundamental concepts of a computer, components of a computer, an overview of compilers and interpreters, structure of a ‘C’ program, programming rules, how to execute the program, and flowchart for execution of a program. This chapter also presents the techniques of solving a problem using algorithm and flowchart. Chapter 2 explains the fundamentals of ‘C’. These concepts are essential for writing programs and contain character set supported by C language. Various delimiters used with ‘C’ statements, keywords and identifiers are also provided. Different constants, variables and data types supported by C are also given. This chapter covers the rules for defining variables and methods to initialize them. Dynamic initialization is also presented in this chapter. Type conversion of a variable, type modifiers and wrapping around, constant and volatile variables are also explained. Chapter 3 covers various C operators and their priorities. This chapter presents arithmetic, relational and logical operators. It also embodies increment, decrement (unary operators) and assignment operators. Other operators such as comma, conditional operator and bitwise operators are presented with programming examples. PRefACe A01_KAMT3553_02_SE_FM.indd 11 5/17/2015 8:59:36 AM
xii Preface Chapter 4 deals with formatted input and output functions such as scanf()and printf() functions. The unformatted functions such as putchar() , getche() and gets() are described in this chapter. Different data types and conversion symbols to be used in the C programs have also been elaborated. The special symbols such as escape sequences together with their applications are also discussed. Few of the commonly used library functions to be used in the programs such as clrscr() and exit() are also described. Chapter 5 is essential for knowing the decision-making statements in C language. This chapter presents how to transfer the control from one part to the other part of the program. The programmer can make the program powerful by using control statements such as if, if-else, nested if-else statements and switch case. To change the flow of the program, the programmer can use keywords such as break, continue and goto. Chapter 6 is devoted to control loop structures in which how statements are executed several times until a condition is satisfied. In this chapter, the reader follows program loop which is also known as iterative structure or repetitive structure. Three types of loop control statements are illustrated with for, while and do-while programming examples. Syntaxes of these control statements are briefed together with programming examples. The other statements such as the break,continue and goto statements are also narrated. Chapter 7 deals with the array in which the reader can follow how to initialize array in different ways. The theme of this chapter is to understand the array declaration, initialization, accessing array elements and operations on array elements. How to specify the elements of one-, two- and three- or multi-dimensional arrays are explained in detail together with ample examples. The functions such as sscanf() and sprintf()are demonstrated through programming examples. The reader can develop programs after learning this chapter on arrays. This chapter also gives an overview of the string. It covers operations on array such as deletion, insertion and searching an element in the array and how to traverse all the array elements. Chapter 8 is focused on strings. This chapter teaches you how to learn declaration and initialization of a string. It is also very important to identify the end of the string. This is followed by NULL (‘\0’) character. The various formats for display of the strings are demonstrated through numerous examples. String handling has strong impact in real-life string problems such as conversion of lower to upper case, reversing, concatenation, comparing, searching and replacing of string elements. It is also discussed how to perform these operations with and without standard library functions. Memory functions such as memcpy(), memove() and memchr()are also illustrated together with programming examples. Chapter 9 deals with the most important feature of the C language, i.e. pointer, it is important but difficult to understand easily. The reader is made familiar with pointers with numerous examples. The reader is brought to light about declaration and initialization of pointers, and how to access variables using pointers. How pointers are used to allocate memory dynamically at run time is also illustrated using memory allocation functions such as malloc() and calloc() functions. How memory is handled efficiently with pointers is also explained. This chapter consists of arithmetic operations on pointers, pointers and arrays, pointers to pointers and pointers to strings. Memory models are also explained. Chapter 10 is one more important chapter on functions. How a large size program is divided in smaller ones and how a modular program should be developed is learnt in this chapter. Programmer learns the definition and declaration of function. What are the return statements, types of functions and functions with passing arguments are described in detail. What do you mean by “call by value” and “call by reference”? — Their answers are given with many programming examples. This chapter also incorporates functions and loop statements, function and arrays and association of functions and pointers. A01_KAMT3553_02_SE_FM.indd 12 5/17/2015 8:59:37 AM
Preface xiii The reader should know that the function always returns an integer value. Besides a function can also return a non-integer data type but function prototype needs to be initialized at the beginning of the program. The recursive nature of function is also explained with suitable example. Direct and indirect recursive functions have been explained with programming examples. Chapter 11 enlightens on the variables used in C in different situations. It also covers types of variables such as local and global variables. The various storage classes of a variable are also covered in this chapter. Explanations on auto, extern, static and register variables are also presented in this chapter. Chapter 12 narrates how to make use of preprocessor directives and how various macros are to be used. This chapter enlightens preprocessor directives such as #define, #undef, #include, #line, token pasting and stringizing operations and conditional compilation through C are illustrated. It covers #define directive, undef macro, include directive, predefined macros in ANSI and Turbo C. A reader learns how to display programmer’s own error messages using #error directive and making various warnings on/off displayed by compiler using #pragma directive. You are exposed to predefined macors in ctype.h in this chapter. Chapter 13 is on structures and unions. A reader can get derived data type using structures and unions. User can decide the heterogeneous data types to be included in the body of a structure. Use of dot operator (.) and pointer (->) are explained for accessing members of structure. Declaration and initialization of structure and union are also explained. The typedef facility can be used for creating user-defined data types and illustrated with many examples. Enumerated data type and union are the important subtitles of this chapter. Enumerated data type provides user-defined data types. Union is a principal method by which the programmer can derive dissimilar data types. The last but not the least the DOS and ROM-BIOS functions and their applications are also explained. Chapter 14 is on files. This chapter explains the procedure for opening a file, storing information and reading. How to read a file and how to append information are explained in this chapter. Many file handling commands are also discussed. Text and binary files are explained. Command line arguments to accept arguments from command prompt are described. Simulation of various DOS commands with examples is also narrated. A reader is also made familiar with I/O redirections in which MSDOS redirects to send the result to disk instead of seeing information on monitor. Chapter 15 is on graphics. How to draw various figures/images using C library graphics functions are to be studied from graphics chapter. This chapter enlightens the reader about the initialization of graphics with library graphics functions and number of programming examples. Few programs have been provided on mouse programming. Chapter 16 enlightens the reader on dynamic memory allocations, memory models and linked lists. Dynamic memory allocation deals with memory functions such as malloc(), calloc(), coreleft() and realloc() and release the allocated memory using free() function. The linked list is described in brief in this chapter. In the linked list, creation of linked list, traversing, searching, inserting and deleting an element are described with figures and programming examples. Utmost care has been taken to write third edition of the book in order to make it error free. The suggestions and feedback for the improvement of the book are always welcome, the readers can directly mail me at ankamthane@gmail.com. Ashok N. Kamthane A01_KAMT3553_02_SE_FM.indd 13 5/17/2015 8:59:37 AM
I would like to thank all those who have encouraged me, especially Professor B.M. Naik, former principal of Shri Guru Gobind Singhji Institute of Engineering and Technology, who has been always a source of inspiration. Special thanks are due to members of board of governors of SGGS institute who motivated me for writing this book, Baba Kalyani, Chairman and Managing Director of Bharat Forge Ltd Pune, Ram Bhogle, C.Y. Gavhane, Mr Kamlesh Pande, Sanjay Kumar, Dr Nirmal Singh Sehra, and Director of our Institute Dr L.M. Waghmare. My sincere thanks to Professor S.D. Mahajan, Director of Technical Education, Maharashtra State, and ex-Board of Governors of this college, Dr M.B. Kinhalkar, Former Home Minister and ex-Principal, Dr T.R. Sontakke for inspiring me to write this book. I am grateful to all my colleagues, friends and students, who extended morale support, Dr Y.V. Joshi, Dr R.R. Manthalkar, Dr S.S. Gajre, Dr S.V. Bonde, Dr P.G. Jadhav, Professor N.G. Megde, Professor P.S. Nalawade, Dr A.R. Patil, Dr A.B. Gonde, Dr M.B. Kokre, Dr U.V. Kulkarni, Dr P. Pramanik, Dr V.M. Nandedkar, Dr A.V. Nandedkar, Dr B.M. Patre, Dr S.T. Hamde, Dr R.C. Thool, Dr V.R. Thool, Mrs U.R. Kamble, Dr D.D. Doye, Dr V.G. Asutkar, Professor R.K. Chavan, Professor Ghanwat Vijay, Dr V.K. Joshi, Dr S.G. Kahalekar, Dr A. Chakraborty, Dr P. Kar, Dr P.G. Solankar, Dr B.M. Dabde, Dr M.L. Waikar, Dr R.S. Holambe, Dr J.V.L. Venkatesh, Mrs S.S. Kandhare, Professor S.S. Hatkar, Narayan Patil, Dr P.D. Dahe, Dr P.D. Ullagadi, Dr P.B. Londhe, Dr A.S. Sontakke, Professor A.M. Bainwad, Professor Deepak Bacchewar, Professor R.P. Parvekar, Professor N.M. Khandare, Dr V.B.Tungikar, Dr R.N. Joshi, Dr L.G. Patil, Professor A.I. Tamboli, Mr Bhalerao M.V. and Professor S.B. Dethe. I am also thankful to my friends, Professor S.L. Kotgire, Maruti Damkondawar, G.M. Narlawar, Anil Joshi, D.V. Deshpande, Professor Balaji Bacchewar, M.M. Jahagirdar, L.M. Buddhewar, K.M. Buddhewar, S.R. Kokane, Ganpat Shinde, M.G. Yeramwar, S.R. Tumma, S.P. Tokalwad, P.R. Navghare, Somajawar H.S. and Annes for their morale support. I am thankful to the wonderful editorial team of Pearson Education for specific invaluable inputs and bringing this book out in a record time. I also express my thanks to my son Lecturer Amit, students Jadhav Gopal and Wanjare Sainath for their critical review and suggesting improvements. Last but not the least, my thanks are due to my wife Surekha for her patience and support. My son Amol, daughter Sangita and daughter-in-law Swaroopa were of great help and supported me all the times. Ashok N. Kamthane ACkNOwledGeMeNts A01_KAMT3553_02_SE_FM.indd 14 5/17/2015 8:59:37 AM
Ashok N. Kamthane is presently working as an associate professor in Department of Electronics and Telecommunication Engineering at Shri Guru Gobind Singhji Institute of Engineering and Technology, Nanded, Maharashtra. He has over 32 years of teaching experience. He was associated with the development of hardware and software using 8051 on acoustic transceiver system for submarines. Professor Kamthane is also the author of bestselling books Object-Oriented Programming with ANSI and Turbo C++; Introduction to Data Structures in C; and C Programming: Test Your Skills, published by Pearson Education. AbOut the AuthOR A01_KAMT3553_02_SE_FM.indd 15 5/17/2015 8:59:37 AM
This page is intentionally left blank
Basics and Introduction to C1 CHAPTER Chapter Outline 1.1 Why to use Computers? 1.2 Basics of a Computer 1.3 Latest Computers 1.4 Introduction to C 1.5 About ANSI C Standard 1.6 Machine, Assembly and High-Level Language 1.7 Assembler, Compiler and Interpreter 1.8 Structure of a C Program 1.9 Programming Rules 1.10 Executing the C Program 1.11 Standard Directories 1.12 The First C Program 1.13 Advantages of C 1.14 Header Files 1.15 Algorithm 1.16 Classification of Algorithms 1.17 Flowcharts 1.18 Pseudocode M01_KAMT3553_02_SE_C01.indd 2 5/17/2015 9:04:16 AM
Basics and Introduction to C 3 1.1 Why to Use CompUters? Computers play a vital role in the socioeconomic progress of a country. Every nation is paying much importance to computer literacy. Progress of individuals, surrounding region, nation and of the world is ensured only with the introduction and use of computers. In every walk of life, computers are being used increasingly. In every part of the world, computers are employed to increase the overall produc- tivity. Moreover, the quality of the products due to application of computers is substantially improved. The impact of computers is very high on mass education, entertainment and productivity in all fields. With the use of computers the cost of production reduces drastically, a lot of time is saved and the best quality is ensured. With the adoption of the policy of liberalization, privatization and globalization by the govern- ments all over the world, it is necessary to become competitive in the market. Luxurious survival is not easy as was possible in ancient days. With land, labour, muscle power and capital, economy can certainly be boosted to a certain degree, but the intellectual power is highly superior to all these assets. With some know-how (for the common users) and a lot of technical innovations (by the developers), numerical problems, business transactions and scientific applications, can be computed in almost no time. A computer does various tasks on the basis of programs. Intellectual computer is certainly a powerful gadget but human intellectual capability is much higher than that of a computer. Thus, com- puter does not have its own brain. Brainpower of computer is limited. Thus, human intellectual power is further reinforced by the use of computers frequently in the day-to-day life. By learning program- ming languages, developing software packages and using hardware of computers, one can make his/ her life and nation prosperous. 1.2 BasiCs of a CompUter Data to be processed by a computer appears in different forms, such as numeric, alphabetical char- acters either in uppercase or in lowercase, special characters and multimedia. Research applications in universities and colleges frequently use numeric data on large scales, whereas businessmen and people in the corporate sector use both numeric and character data. However, in animation multimedia is used which include text, video and audio. A computer is a programmable electronic machine that accepts instructions and data through input devices, manipulating data according to instructions and finally providing result to the output device. The result can be stored in memo- ry or sent to the output device. Figure 1.1 shows the conventional block diagram of a conventional computer. Figure 1.2 shows a computer system contain- ing a monitor, a keyboard and a rectangular box comprising CPU on the motherboard. In brief, the various blocks of a computer are described as follows. Input Device: Input device is used to accept the data and instructions in to the computer. Through the input device, i.e. keyboard, instructions and data are stored in a computer’s memory. Stored instructions are further read by the computer from its memory and thus a computer manipulates the data according to the instructions. The various input devices that can be used in a computer are Central Processor Unit Output Memory Input Figure 1.1 Block diagram of a conventional computer M01_KAMT3553_02_SE_C01.indd 3 5/17/2015 9:04:17 AM
4 Programming in C keyboards, mouse, analog-to-digital converters, light pen, track ball, optical character reader and scanner. Figure 1.3 shows the input devices of a computer. Floppy, compact disc, etc. can be used as input or output device. Central Processor Unit: CPU is the abbrevia- tion for central processor unit. It is the heart of the computer system. It is a hardware device in the Figure 1.3 Input devices of a computer CD Keyboard Mouse Floppy Microphone Pen Drive computer and quite often it called as microprocessor chip. Since it is a tiny chip hence called as mi- croprocessor chip. This chip is produced from silicon vapor over which millions of transistors are mounted with modern fabrication techniques. The brain of the computer is CPU. This chip is responsible to interpret and execute the instruc- tions. It comprises arithmetic and logical unit, registers and control unit. The arithmetic and logical unit performs various arithmetic and logical operations on the data based upon the instructions. The control unit generates the timing and control signals for carrying out operations within the processor. Registers are used for holding the instructions and storing the results temporarily. Instructions are stored in the memory and they are fetched one by one and executed by the processor. Output Device: The output device is used to display the results on the screen or to send the data to an output device. The processed data is ultimately sent to the output device by the computer. The out- put device can be a monitor, a printer, an LED, seven-segment display, D to A converter, plotter, and so on. Figure 1.4 shows different output devices of a computer. Figure 1.4 Output devices of a computer Printer CD Floppy Monitor Speaker Hard-Disk Figure 1.2 computer system Interface Memory: Memory is used to store the program. There are two types of semi-conductor memories. They are as follows: (i) RAM (Random access memory) (ii) ROM (Read only memory) M01_KAMT3553_02_SE_C01.indd 4 5/17/2015 9:04:18 AM