Uploaded by Jon Hari

Basic computation - daftar isi doang

advertisement
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Basic Computation and Programming with C
Undergraduate students of engineering and computer science will come across detailed
coverage of the fundamentals of computation and programming in C language, in this
textbook. Essential concepts including operators and expressions, input and output
statements, loop statements, arrays, pointers, functions, strings and pre-processor
are described in a lucid manner. For better comprehension of the concepts, the book is
divided into three parts: Fundamentals of Computer, Programming with 8C9 and Technical
Questions.
A unique feature of the book is 8Learn by Quiz 3 Questions/Answers9, which has questions
designed through conodence-based-learning methodology. This helps readers to identify
right answers with adequate explanation and reasoning as to why the other options are
wrong. Plenty of computer programs and review questions are interspersed throughout
the text.
The book can also be used as a self-learning book by beginners in Computer
Programming.
Subrata Saha is Head of the Department of Computer Applications, Techno India Hooghly,
West Bengal. His areas of interest include object oriented languages, image processing and
cryptography.
Subhodip Mukherjee is Head of the Department of Computer Applications, Techno India
College of Technology, Kolkata. His areas of interest include object oriented languages,
software engineering, computer architecture and database management systems.
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Basic Computation and
Programming with C
Subrata Saha
Subhodip Mukherjee
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
4843/24, 2nd Floor, Ansari Road, Daryaganj, Delhi - 110002, India
Cambridge University Press is part of the University of Cambridge.
It furthers the University9s mission by disseminating knowledge in the pursuit of
education, learning and research at the highest international levels of excellence.
www.cambridge.org
Information on this title: www.cambridge.org/9781316601853
© Cambridge University Press 2016
This publication is in copyright. Subject to statutory exception
and to the provisions of relevant collective licensing agreements,
no reproduction of any part may take place without the written
permission of Cambridge University Press.
First published 2016
Printed in India
A catalogue record for this publication is available from the British Library
Library of Congress Cataloging-in-Publication Data
Names: Saha, Subrata, 1973- author. | Mukherjee, Subhodip, author.
Title: Basic computation and programming with C / Subrata Saha, Subhodip Mukherjee.
Description: New York : Cambridge University Press, 2016. | Includes bibliographical references and index.
Identioers: LCCN 2016018907 | ISBN 9781316601853 (pbk.)
Subjects: LCSH: C (Computer program language) | Mathematics--Data processing.
Classiocation: LCC QA76.73.C15 S237 2016 | DDC 004.01/51--dc23 LC record available at
https://lccn.loc.gov/2016018907
ISBN 978-1-316-60185-3 Paperback
Additional resources for this publication at www.cambridge.org/9781316601853
Cambridge University Press has no responsibility for the persistence or accuracy
of URLs for external or third-party internet websites referred to in this publication,
and does not guarantee that any content on such websites is, or will remain,
accurate or appropriate.
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
To my father Late Kamal Krishna Saha and to my mother Geetasree Saha for what I am today.
— Subrata Saha
To my mother who sacrioced all sorts of entertainment for my education during my school life and
to my father Professor S. G. Mukherjee whose addiction to books and simple living inspired me to
be a teacher.
— Subhodip Mukherjee
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Contents
List of Figures
List of Tables
Preface
Acknowledgments
xiii
xv
xvii
xix
PART A: Fundamentals of Computer
1.
Computer-History, Classification and Basic Anatomy
1.1
Generations of Computer
1.2
Classiocation of Computers
1.3
Basic Anatomy of a Computer
1.4
Von Neumann Architecture
1.5
Memory Classiocation and Hierarchy
1.6
Input and Output Devices
1
1
4
8
9
10
10
2.
Introduction to Number System and Logic Gates
2.1
Introduction
2.2
Base of a Number System
2.3
Reason behind Using Binary Number System
2.4
Conversion among Different Bases
2.5
Similarities between Number Systems of Different Bases
2.6
Addition of Two Numbers
2.7
Signed Binary Numbers
2.8
ASCII (American Standard Code for Information Interchange)
2.9
Logic Gates and Boolean Algebra
2.10
Gates (Logic Gates)
2.11
Basic Gates
2.12
Universal Gates
14
14
14
15
16
19
19
21
24
25
25
26
28
3.
Introduction to System Software and Operating Systems
3.1
Introduction to Assembler
3.2
Introduction to Compiler
3.3
Introduction to Operating System
31
31
32
34
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Contents
viii
4.
Algorithms and Flow Chart
4.1
Flow Chart
40
40
PART B: Programming in ‘C’
5.
Introduction to C
5.1
Generations of Programming Languages
5.2
History of C Language
5.3
Why is C so Popular?
5.4
Position of C in the Generations of Languages
49
50
51
52
53
6.
Constants, Variables and Data Types
6.1
C Character Set
6.2
Constants
6.3
Keywords and Identioers
6.4
Variables and Data Types
6.5
Storage Type Qualioer
58
58
58
61
61
64
7.
Operators and Expressions
7.1
Operators in C
7.2
Expressions
7.3
Precedence and Associativity
74
74
82
85
8.
Input
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
9.
Branching Statement
9.1
If Statement
9.2
Conditional Operator
9.3
Switch Statement
9.4
Goto Statement
9.5
Programing Examples
132
132
143
144
148
148
10.
Loop Statements
10.1
While Statement
10.2
Nested Loop
10.3
Test Your Progress
10.4
For Statement
10.5
Test Your Progress
10.6
Do-while Statement
10.7
Break Statement
10.8
Use of Goto
171
172
177
178
179
185
186
187
189
and Output Statement
Formatted I/O Functions
Unformatted I/O Functions
General Structure of a C Program
First C Program
Executing a C Program
Executing from UNIX Environment
Developing a Program
Programming Examples
© in this web service Cambridge University Press & Assessment
98
98
108
109
110
111
113
113
115
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Contents
ix
10.9
10.10
10.11
Continue Statement
Test Your Progress
Programming Examples
190
191
191
Array
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
What is an Array?
Declaration of an Array
Initializing Arrays
Accessing Elements of an Array
Sorting
Searching
Two Dimensional Array
Multidimensional Array
Programming Examples
224
224
224
225
228
232
240
242
245
247
12.
String
12.1
12.2
12.3
12.4
12.5
12.6
12.7
Handling
String
Overcoming the Limitation with the Help of Scanset
sscanf( ) and sprintf( ) Function
Character Handling Functions
String Handling Functions
Two-Dimensional Array of Characters
Programming Examples
268
268
273
278
280
289
294
296
13.
Function
13.1
Why Function
13.2
Calling a Function
13.3
Types of Function Arguments
13.4
Array as Function Argument
13.5
Function Prototype
13.6
Passing Multidimensional Array as Function Argument
13.7
Storage Class
13.8
Multi-ole Programs
13.9
Recursion
13.10 Advantage and Disadvantage of Recursion
13.11 Implementation of Some Standard Library Functions
13.12 Programming Examples
320
320
323
324
326
328
331
332
337
339
343
343
345
14.
Pointer
14.1
Pointer
14.2
Array and Pointer
14.3
Pointer Arithmetic
14.4
Array vs Pointer
14.5
Pointer and Function
14.6
Pointer and String
14.7
Function Returning Pointer
14.8
Pointers and Multidimensional Arrays
14.9
Array of Pointers
14.10 Dynamic Memory Allocation
367
367
371
372
375
376
380
381
383
384
390
11.
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Contents
x
14.11
14.12
14.13
14.14
14.15
14.16
14.17
Function Pointer
Returning a Two Dimensional Array from Function
Dangling Pointer
Implementation of Some Standard Library Functions
Advantages of Pointer
Interpreting Complicated Pointer Declarations
Programming Examples
393
394
396
397
399
400
401
15.
Structure
15.1
Structure
15.2
Structure Initialization
15.3
Arrays of Structures
15.4
Nested Structures
15.5
Structures and Functions
15.6
Function Returning Structure
15.7
Structures and Pointers
15.8
Bit Fields
15.9
Programming Examples
416
416
418
422
425
427
428
431
434
442
16.
File
16.1
16.2
16.3
16.4
16.5
16.6
461
461
462
464
473
477
479
What is a File?
Processing a File
Input-Output Operations on Files
More File Functions
Command Line Arguments
Programming Examples
17.
Bitwise Operators
17.1
Bitwise & Operator
17.2
Bitwise | Operator
17.3
Bitwise ^ Operator
17.4
~ Operator
17.5
Left Shift Operator
17.6
Right Shift Operator
17.7
Programming Examples
507
508
511
512
514
515
517
518
18.
Preprocessors
18.1
#deone Directive
18.2
Macros with Arguments
18.3
Nesting of Macro
18.4
Multiline Macro
18.5
Macro vs. Function
18.6
#undef Directive
18.7
#include Directive
18.8
Conditional Compilation Directive
18.9
Additional Directives
18.10 Predeoned Macros
18.11 Programming Examples
528
528
529
531
531
532
532
532
533
536
537
538
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Contents
19.
Linked List
19.1
Linked List
19.2
Advantages of Linked List
19.3
Types of Linked List
19.4
Implementation of Singly Linked List
19.5
Operations on Singly Linked List
19.6
Inserting a New Element in a Linked List
19.7
Applications of Singly Linked List
19.8
Disadvantages of Linked List
19.9
Programming Examples
xi
546
547
547
547
548
550
553
568
572
572
PART C: Technical Questions for Interview
Model Question Set-1
Answer to Model Question Set-1
581
584
Model Question Set-2
Answer to Model Question Set-2
586
589
Model Question Set-3
Answer to Model Question Set-3
591
594
Model Question Set-4
Answer to Model Question Set-4
596
599
Model Question Set-5
Answer to Model Question Set-5
602
605
Appendix:
I. Common Questions and Answers
II. ASCII Characters
III. Some Useful Library Functions
© in this web service Cambridge University Press & Assessment
607
630
635
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Figures
Fig. 1.1
Fig. 1.2
Fig. 1.3
Fig. 1.4
Fig. 1.5
Fig. 1.6
Fig. 1.7
Fig. 1.8
Fig. 1.9
Fig. 2.1
Fig. 2.2
Fig. 2.3
Fig. 3.1
Fig. 3.2
Fig. 3.3
Fig. 4.1
Fig. 8.1
Fig. 9.1
Fig. 9.2
Fig. 9.3
Fig. 10.1
Fig. 10.2
Fig. 10.3
Fig. 11.1
Fig. 11.2
Fig. 11.3
Fig. 11.4
Fig. 14.1
Fig. 14.2
Fig. 14.3
Fig. 14.4
Fig. 15.1
ENIAC
Transistor
Components of a computer
Common bus architecture
Keyboard.
USB and PS/2 ports
Scanner
Microphone
Printers
Addition process
Sequential circuit
NAND as universal gate
Process for producing executable ole
Parse tree of a sentence
A sample window
Flowchart of onding maximum of three numbers
Steps of compilation and execution
Flowchart of if statement
Flowchart of if3else statement
Flow chart to ond the largest number among three inputted numbers
Entry controlled loop
Exit controlled loop
Flow chart to illustrate while statement
Allocation of memory for character array
Allocation of memory for integer array
Conceptual view of a two dimensional array
Accessing elements in a two dimensional array
Memory map of variable allocation
Address calculation for the elements of 2D array
Memory map of above example
Memory map of array of pointer to string
Memory map of structure variable
© in this web service Cambridge University Press & Assessment
2
3
8
9
11
11
12
12
13
20
25
28
32
33
36
41
112
133
134
137
171
172
173
226
227
242
243
368
383
385
386
417
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
xiv
Fig. 15.2
Fig. 15.3
Fig. 15.4
Fig. 17.1
Fig. 17.2
Fig. 19.1
Fig. 19.2
Fig. 19.3
Fig. 19.4
Fig. 19.5
Fig. 19.6
Fig. 19.7
Fig. 19.8
Fig. 19.9
Fig. 19.10
Figures
Array of structure
Initialized array of structure s
Sharing of memory location by the members of a union
<< operation
>> operation
Different type of linked list
Representation of a singly linked list in memory
Inserting a node at the beginning of a list
Inserting a node at the end of a list
Inserting a node after 2nd node
Inserting a node before 3rd node
Deletion of orst node from a list
Deletion of last node from a list
Deletion of an intermediate node from a list
Representation of a polynomial using linked list
© in this web service Cambridge University Press & Assessment
423
423
437
516
517
548
550
554
556
557
557
559
560
562
568
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Tables
Table 2.1
Table 2.2
Table 2.3
Table 2.4
Table 3.1
Table 3.2
Table 4.1
Table 6.1
Table 6.2
Table 6.3
Table 6.4
Table 7.1
Table 7.2
Table 7.3
Table 7.4
Table 7.5
Table 7.6
Table 7.7
Table 8.1
Table 8.2
Table 8.3
Table 8.4
Table 8.5
Table 8.6
Table 8.7
Table 16.1
Table 16.2
Table 17.1
Table 17.2
Table 17.3
Table 17.4
Table 17.5
Powers of 2
Binary values of decimal numbers
Similarity of different base
ASCII values
Some DOS commands
Some UNIX commands
Flowchart symbols
C character set
Keywords in C
Basic data types in C
Size and range of data types on 16 bit machine
Arithmetic operators and their role
Shorthand assignment operators and their equivalent statement
Relational operators
Logical operators
Bitwise operators
Conversion rules
The precedence of operators and their associativity
Escape sequences
Conversion specioers
Minimum width specioers
Flag specioers
Conversion specioer that are used with nag specioers
Size modioers with their conversion specioer
Examples of size modioers
File opening mode
Reference positions
Bitwise operators
Bitwise & operation on individual bits
Illustration of bitwise & operation showing bit pattern
Bitwise | operation on individual bits
Illustration of bitwise | operation showing bit pattern
© in this web service Cambridge University Press & Assessment
17
17
19
24
35
37
42
58
61
62
62
75
77
79
80
81
83
86
99
100
102
104
104
105
106
462
474
507
508
508
511
511
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
xvi
Table 17.6
Table 17.7
Table 17.8
Table 17.9
Table 17.10
Table 18.1
Tables
Bitwise ^ operation on individual bits
Illustration of bitwise ^ operation showing bit pattern
Bitwise ~ operation on individual bits
Illustration of left shift operation showing bit pattern
Illustration of right shift operation showing bit pattern
Predeoned macros
© in this web service Cambridge University Press & Assessment
512
513
515
516
517
537
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Preface
The C programming language is one of the most, academically as well as industrially,
important programming languages in the world. It was unveiled in 1972 and since then,
with gradual enhancements and enrichments, it has successfully established itself as a
powerful language for programming microcontrollers, operating systems and various
commercially signiocant software packages. Having unique features like block structure,
stand alone functions and a rich set of keywords with very few restrictions, it is aptly
regarded as the 8Mother Language9 among programming languages. Hence it is necessary
for every programmer to learn and, often, use C.
About the Book
Today, in any corner of the world, there is a necessity to learn computer programming
for people to survive in any industry. All students do not possess technical background
and therefore ond learning C programming difocult. Keeping a strong focus on industrial
requirements and the limitations of students from non-technical backgrounds, we have
written this new book on C programming to enable students of non-technical as well as
technical backgrounds learn this marvelous programming language in a completely new
way.
This book is completely different from other popular C programming books available
in the market. It teaches programming to someone who wants to learn how to program in
C for the orst time in his/her life with no programming knowledge at all. In other words,
this book on C programming is for absolute beginners of programming but at the same
time it outclasses several other C programming books in the market with its coverage.
Although targeted at complete beginners, this book covers many advanced concepts in C
programming and enables a non-programmer to develop into a competitive C programmer
ready to face job interviews on C.
Structure of the Book
This book is written presuming that the reader is not strong in English. All the
explanations in each chapter are offered in simplest English to enable any reader
to correctly understand a concept.
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
xviii
Preface
Each chapter contains appropriate examples. It ends with a 8Learn by Quiz 3
Questions/Answers9, having multiple choice questions with their respective answers
(with clariocations) to ensure understandability.
In order to make the presentation visually interactive for students, neat labeled
diagrams are provided wherever necessary. For each topic, the explanations are
clear and concise, avoiding verbosity as much as possible.
Each programming topic is presented with an exercise set consisting of a large
number of solved C programs. Each such solved program is presented with WHYs
and HOWs for each new or next statement of a program.
This book also includes a rich collection of commonly asked as well as most
probable, new C programming interview solved questions and programs with stepby-step explanations to enable a student to be successful in an interview.
Students, we have prepared this book for you using a unique teaching approach based
on our academic learnings so that it looks least intimidating and most interesting. We
are sure that after studying this new book on C programming, those of you for whom C
programming has been a source of sorrow will now ond in it joy and fun. Though every
attempt has been made to avoid errors, we will be grateful to our readers if they bring
to our notice any oversight they ond. If the content design and organization of this book
meets all the expectations and requirements of our students, then only can this book be
considered a worthwhile accomplishment.
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Cambridge University Press & Assessment
978-1-316-60185-3 — Basic Computation and Programming with C
Subrata Saha, Subhodip Mukherjee
Frontmatter
More Information
Acknowledgments
xix
Acknowledgments
I thank my, beloved, students for their constant motivation which made me write this
book despite the availability of many best-selling C programming books in the market. My
students are extremely fond of and fascinated by my 8teaching from the ground up9 style
and their demand inspired me to author this amazingly simple C book which is aimed at
non-programmers or absolute beginners. My heartiest thanks to them.
I would like to thank T. K. Ghosh, Chief Executive Director of Techno India Group for
providing me the platform to teach so many young minds.
I want to thank M. K. Chakraborty for his constant support and inspiration.
I would also like to thank Subir Hazra, my friend and ex-colleague, who helped us in
preparing the 8Learn by Quiz - Questions/Answers9 section.
Most important, heartiest thanks to my family specially my wife Sriparna Saha and my
beloved daughter Shreya for their support, encouragement, quite patient and unconditional
love which helped, conclude the book.
Finally, I would like to thank all the reviewers of this book for their critical comments
and suggestions. I convey my gratitude to Rachna Sehgal and the entire editing team at
Cambridge University Press, India for their great work.
Subrata Saha
Thanks to T. K. Ghosh (Chief Executive Director) who found in me suitability to be the
head of three departments of a Techno India Group college. He gave me the opportunity to
prove myself, to be a good teacher to thousand of students.
Thanks to my teacher Professor Ranjan Dasgupta (Head, Computer Science, NITTTRKolkata) who gave me the conodence to write a book.
Thanks to my ex-colleague Professor Sumana Chakraborty who participated actively in
discussions to freeze the table of contents of this book.
Subhodip Mukherjee
© in this web service Cambridge University Press & Assessment
www.cambridge.org
Download