Pradip Dey, Manas Ghosh - Computer Fundamentals and Programming in C-Oxford (2013)-min.pdf - COMPUTER FUNDAMENTALS AND PROGRAMMING IN C Second Edition

Pradip Dey, Manas Ghosh - Computer Fundamentals and Programming in C-Oxford (2013)-min.pdf

This preview shows page 1 out of 512 pages.

You've reached the end of your free preview.

Want to read all 512 pages?

Unformatted text preview: COMPUTER FUNDAMENTALS AND PROGRAMMING IN C Second Edition Pradip Dey RCC Institute of Information Technology Manas Ghosh RCC Institute of Information Technology 1 3 Published in India by Oxford University Press YMCA Library Building, 1 Jai Singh Road, New Delhi 110001, India © Oxford University Press 2006, 2013 The moral rights of the author/s have been asserted. First Edition published in 2006 Second Edition published in 2013 ISBN-13: 978-0-19-808456-3 ISBN-10: 0-19-808456-0 Preface to the First Edition C stands out among general-purpose programming languages for its unrivaled mix of portability, power, flexibility, and elegance. The language has block structures, standalone functions, a compact set of keywords, and very few restrictions. Like any low-level language, C allows the programmer to manipulate bits, bytes, and memory addresses, among other features. Like any high-level language, C also supports various data types to provide a higher level of abstraction to programmers, thereby making coding easier. C provides features for writing and separately compiling, shorter programs that can be linked together to form a large program. For all these reasons, it is a versatile language suited for projects of various sizes in both systems as well as applications programming. a B This book is intended for a one-semester introductory course on computers and programming in C. The first few chapters of the book impart adequate knowledge of number systems, Boolean logic, hardware, and software of computer systems with particular emphasis on the personal computer. The book assumes no prior programming experience in C or any other language. Once the readers grasp the preliminary topics, it then becomes easier for them to delve into the process of creating algorithms for solving problems and implementing them using C. Throughout the text it has been our endeavor to keep the level of explanations and definitions as lucid as possible. Figures have been included in the text to clarify the discussions on the features of C. Almost all the features of C have been illustrated with examples. Though every attempt has been made to avoid and check errors, we will be grateful to readers if they can point out any that may have crept in inadvertently. ac wl dg We express our gratitude to Dr J. Debroy, Principal, RCC Institute of Information Technology, Kolkata, for encouraging us to write this book. We are also grateful to the staff of Oxford University Press for the cooperation, interest, and assistance extended to us for this book. We thank our colleague Mr Manas Sinharoy for assisting us in preparing the manuscript in time. We are also thankful to Mr Tapas Kumar Tunga and Mr P.N. Pathak for extending their services to us during the preparation of the manuscript. We wish to thank Vijay Kumar R. Zanvar and Jayasimha Ananth for the article on pointers and arrays as also Thomas Jenkins for the article on recursion, both of which have served as a guide during the development of this manuscript. Manas Ghosh Pradi ey Preface to the Second Edition Evolution of ideas is a never-ending process. New technology and changing needs have a profound influence on computing requirements, which in turn lead to continuous enhancements of the power and scope of computers as well as programming languages. One of the latest technologies soon to be launched is the 32-core CPUs from Intel and AMD. Intel has named this 32core processor ‘Kelifer’, which is a combination of 32 brains that can work for the user at one time. These CPUs will be built around a paradigm that will allow them to have plug-in and add-on abilities. Then there are other possibilities such as gesture-based remote controls and smartphone applications to open car doors. Another computing application that has had a profound impact on every aspect of our lives is the Internet. Today, more than 2.4 billion people use the Internet, according to Internet World statistics, and the numbers are still growing. Some industries, such as music and newspapers, have been all but destroyed, even as it has created whole new lines of business, such as search engines and social media. The Boston Consulting Group estimated recently that if the Internet were a country, it would rank as the world’s fifth-largest economy. Naturally, then, programming languages are also being updated to keep pace with these technological leaps. C is one of the most widely used programming languages of all time. Its modern standard C99 (an informal name for ISO/ IEC 9899:1999) extends the previous version (C90) with new language and library features, and helps programmers make better use of available computer hardware and compiler technology. The new features include inline functions, several new data types, and new header files. Hence, with the new features suggested by the C99 committee, C has expanded its scope and range of applications. With the development of embedded systems, the frontiers of C have moved further, to evolve as a very effective programming language for programming embedded systems. aBout the BooK Computer fundamentals and programming in C is an introductory course at most universities offering engineering and science degrees, and aims to introduce the basic computing and programming concepts to students. The general course objectives are to enable the student to learn the major components of a computer system, know the correct and efficient ways of solving problems, and learn to program in C. This second edition of Computer Fundamentals and Programming in C, designed as a textbook for students of engineering (BE/BTech), computer applications (BCA/ MCA), and computer science (BSc), offers an improved coverage of the fundamental concepts of computing and programming. It offers several new topics and chapters, programming updates based on the recommendations proposed by the C99 committee in relevant chapters, and many other useful pedagogical features. A special effort has been made to simplify existing treatments and better explain concepts with the help of improved illustrations and examples containing appropriate comments. Further, most chapters now include notes, check your progress sections, key terms with brief definitions, frequently asked questions with answers, and project questions. These will aid the reader in understanding the concepts and their practical implementations. n w s c d edI I C99 features highlighted wherever relevant in the text 2 New chapters: Introduction to Software; Internet and the World Wide Web Extensive reorganization of the computer fundamentals and functions chapters Note, Check Your Progress sections, Key Terms, Frequently Asked Questions, and Project Questions in most of the chapters Improved explanation of algorithms and codes, and new in-text examples New sections such as working with complex numbers, variable length arrays, searching and sorting algorithms, pointer and const qualifier, and applications of linked lists ex d d p rm rI l Chapter 1 Extensively rewritten sections on classification of computers, anatomy of a computer, memory revisited, introduction to operating systems, and operational overview of a CPU iv Preface to the Second Edition Chapter 8 New sections on • Compilation model of a C program • Philosophy of main() function • The concept of Type qualifiers • How integers are stored in memory Chapter 10 New sections on different forms of loop and moving out from a nested loop Chapter 12 News sections on • Scope, storage class and linkages • Inline function • Different sorting and searching methods along with the analysis of time and space complexity Chapter 13 New sections on • Pointer and const Qualifier • Constant parameter • Returning pointer from a function CoVerage Chapter-wise details of content coverage are as follows: Chapter 1 traces the history of development of computers. The chapter also identifies the different generations and the various categories of computers. It briefly describes the basic hardware units and software modules in a computer, with particular reference to the personal computer. A brief description on the start-up process of a personal computer is also included. Chapter 2 presents the concept of number systems. The rules and methods applied in number system conversions are explained and demonstrated with appropriate examples. It then deals with arithmetic operations of addition, subtraction, multiplication, and division of binary numbers with examples. It also describes the various binary codes used in computers. Chapter 3 introduces Boolean algebra. It defines Boolean variables and the various laws and theorems of Boolean algebra. The formation of Boolean expressions, Boolean functions, and truth tables along with the methods of simplifying Boolean expressions are also demonstrated. It presents the different forms in which Boolean expressions can be expressed and represented. Finally, the chapter discusses logic gates and explains how these are realized using electronic devices. It also demonstrates how Boolean expressions can be realized using logic gates. Chapter 4 identifies the different types of software and the various categories of programming languages available. The roles played by the compiler, linker, and loader in the development of programs are highlighted. Chapter 5 explains the concept of programs and programming. The chapter also defines and explains the key features of algorithms. The significance of an algorithm in developing a program for solving a problem has also been explained. The chapter then discusses some convenient tools and techniques for building and representing algorithms. It also discusses the strategy of problem solving. Chapter 6 begins with the history of evolution of the Internet and the world wide web. It briefly discusses the nature of information transported and the protocols used within the Internet. It introduces the concept of web page, web browser, web server, IP address, and search engines. The chapter concludes by describing the different types of Internet connections followed by the various applications of the Internet. Chapter 7 introduces operating systems. It briefly traces the history of development of operating systems. It explains the functions of an operating system and identifies the component of operating systems in general. The chapter discusses the different types of operating systems that exist. Some popular operating systems such as UNIX and MSDOS have been discussed. Chapter 8 introduces the basic components of C. The keywords and standard data types available in C and the type conversion rules have been discussed. The use of basic operators in C and expressions involving variables and operators has been explained. The basic structure of a standard C program has also been explained. Some common commands used in MSDOS and UNIX to compile and run programs in C have been discussed in this chapter. Chapter 9 discusses the input and output statements in C that are commonly used for the console. It presents the single character non-formatted input/output functions and the formatted input/output functions scanf() and printf(). Chapter 10 presents the decision and loop constructs available in C as also the special constructs that are mostly used with them. It throws light on the reasons behind the choice of control constructs for problem solving. Several examples have been given to illustrate the use of these constructs. Chapter 11 discusses arrays and strings. It explains how arrays of different dimensions are initialized, referred to, used, and printed. The available string arrays and string library functions have been dealt in detail with an adequate number of illustrations. The chapter also illustrates how the input and output functions available in C accept and print strings and arrays. Chapter 12 deals with functions in C. It explains the need of functions in a program. It explains the different components of a function and the method of passing and returning variables Preface to the Second Edition in functions. Scope rules and different storage classes have been discussed with examples. Chapter 13 deals with the concept of pointers. The various features of pointers, including the method of passing pointer variables in functions and other advanced features, have been explained with examples. Multidimensional array handling with pointers has also been discussed. Chapter 14 presents the user-defined data types: structures, unions, enumerators, and bit fields. These have been explained in detail with the help of examples. v been provided for the reader to verify the programs. It also contains tables for ASCII codes, number system conversions, escape sequences, operators, data types and data conversion rules, commonly used conversion characters, and format tags. In addition, it provides an exhaustive listing of library functions of C along with programs that depict their use. There is also a section on common problems encountered while writing programs in C. Chapter 15 discusses the file system used in C. The various functions involving input to and output from a file have been discussed with illustrations. Sequential as well as random access methods adopted in writing to and reading from files have been explained in detail. Chapter 16 highlights some of the advanced features of C such as command-line arguments, bitwise operators, different memory models, and type qualifiers. These features have been discussed with several illustrations. Memory models and special pointers have also been explained. The appendices contain case studies where the problem is first defined and then the algorithm is developed, based on which the C program is coded. Some sample run results have Manas Ghosh Pradi ey Brief Contents Preface to the Second Edition ............iii Preface to the Fisrt Edition ............... vii 1. Computer Fundamentals .................................................................................................................................................1 2. Number Systems and Binary Arithmetic.......................................................................................................................18 3. Boolean Algebra and Logic Gates.................................................................................................................................43 4. Introduction to Software ...............................................................................................................................................59 5. Basic Concepts of Operating Systems ..........................................................................................................................70 6. The Internet .................................................................................................................................................................104 7. Introduction to Algorithms and Programming Concepts ............................................................................................114 8. Basics of C ..................................................................................................................................................................136 9. Input and Output .........................................................................................................................................................183 10. Control Statements ......................................................................................................................................................202 11. Arrays and Strings.......................................................................................................................................................246 12. Functions .....................................................................................................................................................................282 13. Pointers in C................................................................................................................................................................324 14. User-defined Data Types and Variables.......................................................................................................................394 15. Files in C .....................................................................................................................................................................420 16. Advanced C .................................................................................................................................................................445 Bibliography and References ....... 471 Index .............................................. 473 Contents Preface to the Second Edition  iii Preface to the Fisrt Edition  vii 1. p rf d l 1 1.1 1.2 1.3 1.4 1.5 Introduction—What is a Computer? 1 Evolution of Computers—A Brief History 1 Generations of Computers 2 Classification of Computers 3 Anatomy of a Computer  3 1.5.1 Hardware 3 1.5.2 Software 9 1.6 Memory Revisited 10 1.6.1 Primary Memory   10 1.6.2 Secondary Memory  12 1.7 Introduction to Operating Systems 14 1.7.1 Loading an Operating System  14 1.8 Operational Overview of a CPU 15 2.  n arI 2.1 2.2 2.3 2.4 rs Ic d BI r 18 Introduction to Number Systems 18 Base of a Number System 19 Weighting Factor 19 Types of Number Systems 19 2.4.1 Decimal Number System [Base-10]  19 2.4.2 Binary Number System [Base-2]  20 2.4.3 Octal Number System [Base-8]  20 2.4.4 Hexadecimal Number System [Base-16]  20 2.4.5 Common Rules of Number Systems  20 2.5 Number System Conversions 21 2.5.1 Working with Integer Numbers  21 2.5.2 Working with Fractional Numbers  25 2.6 Binary Arithmetic 28 2.6.1 Addition 28 2.6.2 Subtraction 29 2.6.3 Multiplication 33 2.6.4 Division 34 2.7 Binary Codes 36 2.7.1 Numeric Codes   36 2.7.2 Alphanumeric Codes  37 3.  B l alg r d gIc 43 3.1 Introduction to Boolean Algebra 43 3.2 Theorems 44 3.2.1 Some Applications of Boolean Laws and Theorems 45 3.3 Boolean Expression 45 3.4 Simplification of Boolean Expressions 45 3.5 Boolean Functions and Truth Tables 47 3.6 Constructing Boolean Functions from Truth Tables 47 3.7 Canonical and Standard Forms 47 3.8 Numerical Representation of Boolean Functions in Canonical Form 48 3.9 Logic Gates 49 3.9.1 AND Gate  49 3.9.2 OR Gate  50 3.9.3 NOT Gate or Inverter  50 3.9.4 NAND Gate  51 3.9.5 NOR Gate  51 3.9.6 Exclusive-OR or XOR Gate  52 3.9.7 Exclusive-NOR or XNOR Gate  52 3.10 Describing Logic Circuits Algebraically 53 3.11 Realization of Logic Circuits from Boolean Expressions 53 3.12 Universality of NAND and NOR Gates 54 4.  In rodu on o So w re 59 4.1 Introduction 59 4.2 Programming Languages 60 4.2.1 Generation of Programming Languages  60 4.2.2 Classification of Programming Languages  62 4.3 Compiling, Linking, and Loading a Program 64 4.4 Translator, Loader, and Linker Revisited 65 4.4.1 Translators 65 4.4.2 Linker 65 4.4.3 Loader 65 4.5 Developing a Program 66 4.6 Software Development 66 4.6.1 Steps in Software Development  66 xiv Contents 5.  BasIc Concepts of operatIng systems 70 5.1 Introduction 70 5.2 Introduction to Operating System 71 5.3 Functions of an Operating System 71 5.4 Components of an Operating System 73 5.5 Interaction with Operating System 74 5.6 History of Operating Systems 74 5.6.1 First Generation (1945–55)  75 5.6.2 Second Generation (1956–63)—Transistors and Batch System  75 5.6.3 Third Generation (1964–80)—Integrated Chips and Multiprogramming  76 5.6.4 Fourth Generation (1980–present)—Personal Computers 77 5.7 Types of Operating Systems 78 5.7.1 Batch Process Operating System  78 5.7.2 Multiprogramming Operating System  78 5.7.3 Time-sharing Operating Systems  79 5.7.4 Real-time Operating Systems  80 5.7.5 Network Operating System  80 5.7.6 Distributed Operating System  81 5.8 An Overview of UNIX Operating System 82 5.8.1 Reasons for Success of UNIX  82 5.8.2 Components of UNIX  82 5...
View Full Document

  • Summer '20
  • Test

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture