sample copy. not distribution.(dr.k.sujatha) am grateful to my friends and family. i...

24
Sample Copy. Not Distribution.

Upload: others

Post on 12-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

Sample Copy. Not Distribution.

Page 2: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

i

C Simpler: How to Program

Sample Copy. Not Distribution.

Page 3: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

ii

Publishing-in-support-of,

EDUCREATION PUBLISHING

RZ 94, Sector - 6, Dwarka, New Delhi - 110075

Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001

Website: www.educreation.in

________________________________________________________________

© Copyright, 2018, Authors

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer.

ISBN: 978-93-88719-61-2

Price: ₹ 325.00

The opinions/ contents expressed in this book are solely of the authors and do not represent the opinions/ standings/ thoughts of Educreation or the Editors. The book is released by using the services of self-publishing house.

Printed in India

Sample Copy. Not Distribution.

Page 4: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

iii

C Simpler: How to

Program

Ms. D. Ramya

Dr. K. Sujatha

Dr. S. Chidambaranathan

Dr. S. R. Boselin Prabhu

EDUCREATION PUBLISHING (Since 2011)

www.educreation.in

Sample Copy. Not Distribution.

Page 5: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

iv

Sample Copy. Not Distribution.

Page 6: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

v

Ms. D. Ramya

Assistant Professor, Department of Computer Science and

Engineering, Sri Krishna College of Engineering and Technology,

Kuniamuthur, Coimbatore, India.

Dr. K. Sujatha

Assistant Professor, Department of Computer Science, Wenzhou-

Kean University, China.

Dr. S. Chidambaranathan

Associate Professor & Head of MCA Department, St. Xavier’s

College, Palayamkottai, Tamilnadu, India.

Dr. S. R. Boselin Prabhu

Associate Professor, Department of ECE, VSB College of

Engineering Technical Campus, Coimbatore, Tamilnadu, India.

Sample Copy. Not Distribution.

Page 7: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

vi

PREFACE

This treatise on the subject “C Simpler: How to Program” contains

comprehensive treatment of subject helps to solve the C-

Programming. It covers the syllabus of various Indian universities.

● This book contains five modules which emphasis on an

adaptive and systematic approach from introduction to

mainstream applications

● It will be beneficial for students, and academia’s for a time

bound and effective reading for easy understanding of the

subject.

● This is a foundation basic programming subject in Computer

Science and Engineering and many competitive examinations

like GATE, IES etc. This book will be beneficial for preparing

the subject in depth for such competitive objective and

descriptive examinations.

The author’s thanks are due to his beloved parents for extending all

cooperation during preparation of the manuscript. I (Ms.D.Ramya)

take this opportunity to thank the management of SKI Group of

Institutions for their wholehearted support especially to the

chairperson Smt.S.Malarvizhi for her inspiration and motivation. I

(Dr.K.Sujatha) am grateful to my friends and Family. I

(Dr.S.Chidambaranathan) am grateful to my friends and Family. I

(Dr.S.R.Boselin Prabhu) am grateful to my beloved friend

Dr.E.Gajendran, Associate Professor, Sree Dattha Group of

Institutions, Hyderabad because of whom I was enriched with the

skill of writing books.

Sample Copy. Not Distribution.

Page 8: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

vii

Any suggestions for improvement of this book will be thankfully

acknowledged and incorporated in the next edition

Ms.D.Ramya, Dr.K.Sujatha, Dr.S.Chidambaranathan & Dr.S.R.Boselin Prabhu

(Authors of the Book)

Sample Copy. Not Distribution.

Page 9: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

viii

AUTHOR PROFILES

Ms.D.Ramya is working as an Assistant

Professor in the Department of Computer

Science and Engineering in Sri Krishna

college of Engineering and Technology. She

received his M.E degree in Computer Science

and Engineering from Dr.Mahalingam College

of Engineering and Technology, Pollachi,

Coimbatore. Her research areas include

Digital Image Processing, Big Data analytics and Software

Engineering. She published various domain papers in Scopus

indexed journals.

Dr.K.Sujatha is working as Assistant

Professor, Department of Computer Science,

Wenzhou-Kean University, China. She had

completed her doctorate and now she is

pursuing her PDF in Wenzhou-Kean

University, China. She published number of

papers in SCI and Scopus journals and her

area of interest is Image processing. She

completed her doctorate in image processing and she had wide

knowledge in various computer science disciplines.

Sample Copy. Not Distribution.

Page 10: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

ix

Dr.S.Chidambaranathan is Associate

Professor & Head, Department of MCA, St.

Xavier’s College (Autonomous),

Palayamkottai affiliated to Manonmaniam

Sundaranar University, Abisekapatti,

Tirunelveli. He obtained his B.Sc., & M.Sc.,

degrees in Mathematics from Madurai

Kamaraj University, Madurai and MCA.,

M.Phil., and Ph.D., (Comp. Sci) degrees from Manonmaniam

Sundaranar University. He has been teaching Computer Science &

Mathematics subjects to B.Sc. and MCA students for more than 14

years. He has wrote four books and also has one patent right. He

has published more than 30 research papers in data mining and

image mining in peer reviewed UGC approved International

journals to his credit. He has been honored with 11 International,

National and Regional awards.

Dr.S.R.Boselin Prabhu obtained his

doctorate from Anna University Chennai,

India. He has 9 years of experience in

teaching and research. He has published 170

research articles in International Journals and

Conference Proceedings. He is an editorial

board member, advisory board member and

reviewer of 300 International Journals both

Scopus and SCI Indexed. He is an elected fellow member

FUAMAE, FISECE, FISRD, FUAAMP, FISQEM, SIESRP,

FUACEE and FISEEE. He has attained Google scholar citations-

1119 and h-index-16. He is the recipient of awards like Excellent

Professional Achievement Award Winner from Society of

Professional Engineers, Biography Included in Marquis Who’s

who in the World (Academic Year 2015, 2016, 2018 & 2019),

Dedicated Professional Engineer Award Winner from Society of

Engineers & Technicians, Albert Nelson Marquis Lifetime

Sample Copy. Not Distribution.

Page 11: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

x

Achievement Award Winner (2017), Best Young Scientist from

Association of Scientists, Developers and Faculties (2017), Best

Young Researcher from Association of Scientists, Developers and

Faculties (2017) and Bright Educator Award from International

Institute of Organized Research (2018). He has written 18 books

and two monographs for students. He has filed & published 2

Indian patents.

Sample Copy. Not Distribution.

Page 12: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xi

This book shall be dedicated to

Our Parents, Family members and

Student friends

Sample Copy. Not Distribution.

Page 13: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xii

CONTENTS

UNIT I GETTING STARTED

What is a programming Language – What is a compiler – What is a

compiler – What is C – Getting started with C –The first C

program – Compilation and Execution – Receiving input – C

instructions – Control instructions in C.

UNIT II DECISION, LOOP & CASE CONTROL

STRUCUTURE

Decisions – if statement – if.. else statement – Use of logical

operators – conditional operators. Loops – while loop – for loop –

Odd loop – break statement – continue statement – do.. while loop

Decisions using switch – switch vs if else ladder – go to statement.

UNIT III FUNCTIONS & POINTERS

What is function? – Passing values between functions – scope rule

functions – Calling convention – Advanced features of functions –

function declaration and prototypes – call by value and call by

reference – An Introduction to Pointers – Pointer Notations – Back

to function calls – Conclusions – Recursion – Recursion and Stack.

UNIT IV DATA TYPES & ARRAYS

Integers (long, short, signed and unsigned) – Chars (Signed and

unsigned) – Floats and doubles – Few More issues on data types –

storage classes in C – What are arrays – more on arrays – Pointers

and Arrays – Two Dimensional Arrays – Array of Pointers – Three

Dimensional Array.

Sample Copy. Not Distribution.

Page 14: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xiii

UNIT V STRUCTURES & FILES

Why use structures – array of structure features of structures –

Uses of Structures – Data Organization – File operations –

Counting Characters, Tabs, Spaces, - A file copy program File

opening modes – string I/O in files – Text and Binary Files – Low

level Disk I/O – I/O under windows.

Sample Copy. Not Distribution.

Page 15: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xiv

TABLE OF CONTENTS

Sr.No Chapter Page

1. UNIT I(GETTINGSTARTED) 1

What Is Programming Language 1

How do you write a program 1

Compiler 2

What is c 4

Getting started with C 6

The first C program 13

Compilation and Execution 19

Receiving input 20

C instructions 21

Control instructions in C 29

2. UNIT II (GETTING STARTED) 31

Decisions 31

if statement 31

if.. else statement 36

Use of logical operators 44

conditional operators. 50

Loops 51

while loop 51

do.. while loop 59

for loop 67

Odd loop 68

break statement 69

continue statement 71

go to statement 75

Decisions using switch 77

switch vs if else ladder 82

Sample Copy. Not Distribution.

Page 16: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xv

3. UNIT III (FUNCTIONS & POINTERS) 84

What is function? 84

Passing values between functions 90

scope rule functions 95

Calling convention 96

Advanced features of functions 97

function declaration and prototypes 97

call by value and call by reference 100

An Introduction to Pointers 101

Pointer Notations 101

Back to function calls 105

Conclusions 108

Recursion 108

Recursion and Stack 112

4. UNIT IV (DATA TYPES & ARRAYS) 115

Integers (long, short, signed and unsigned) 115

Chars (Signed and unsigned) 117

Floats and doubles 117

storage classes in C 124

What are arrays 126

Two Dimensional Arrays 129

Pointers and Arrays 159

Array of Pointers 166

5. UNIT V (STRUCTURES & FILES) 168

Why use structures 168

array of structure features of structures 172

Uses of Structures 179

Data Organization 181

File operations 181

Counting Characters, Tabs, Spaces, 186

A file copy program File opening modes 187

string I/O in files 188

Text and Binary Files 190

Low level Disk I/O 192

I/O under windows 192

Sample Copy. Not Distribution.

Page 17: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xvi

C PROGRAMMING

11UCK201

Course objectives

➢ To learn the control structures of C language

➢ To write programs using Functions & Pointers

➢ To using different data types and multi dimensional arrays in

programs

➢ To write programs using structures and files

UNIT I

GETTING STARTED

What is a programming Language – What is a compiler – What is a

compiler – What is C – Getting started with C – The first C

program – Compilation and Execution – Receiving input – C

instructions – Control instructions in C

UNIT II

DECISION, LOOP & CASE CONTROL STRUCUTURE

Decisions – if statement – if.. else statement – Use of logical

operators – conditional operators. Loops – while loop – for loop –

Odd loop – break statement – continue statement – do.. while loop

– Decisions using switch – switch vs if else ladder – go to

statement

UNIT III

FUNCTIONS & POINTERS

What is function? – Passing values between functions – scope rule

functions – Calling convention – Advanced features of functions –

function declaration and prototypes – call by value and call by

reference – An Introduction to Pointers – Pointer Notations – Back

to function calls – Conclusions – Recursion – Recursion and Stack.

Sample Copy. Not Distribution.

Page 18: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

xvii

UNIT IV DATA TYPES & ARRAYS

Integers (long, short, signed and unsigned) – Chars (Signed and

unsigned) – Floats and doubles – Few More issues on data types –

storage classes in C – What are arrays – more on arrays – Pointers

and Arrays – Two Dimensional Arrays – Array of Pointers –

Three-Dimensional Array.

UNIT V

STRUCTURES & FILES

Why use structures – array of structure features of structures Uses

of Structures – Data Organization – File operations – Counting

Characters, Tabs, Spaces, - A file copy program File opening

modes – string I/O in files – Text and Binary Files Low level Disk

I/O – I/O under windows.

TOTAL HOURS: 45+15

TEXT BOOKS

• Yashavant P. Kanekar, “Let Us C”, BPB Publication, 10th

Education, 2009

• B.W. Kernigham, Dennis M.Ritchie, “The C Programming

Language”, Pearson Education 2003

REFERENCES

• Samuel P. Harbison III, Guy L. Steele Jr., “C – A Reference

Manual”, Pearson Education, 5th edition, 2008

• Byron S. Gottfried, “Schaum’s outline of theory and problems

of programming with C” McGraw – Hill professional, 1995

Sample Copy. Not Distribution.

Page 19: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

Sample Copy. Not Distribution.

Page 20: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

C Simpler: How to Program

1

UNIT I

GETTING STARTED

What is programming Language?

• Programming Language is series of Instruction to accomplish a

task.

• In other words, A notation for describing algorithm and data

• Programs are set of rules, well forms sentences which are not

that meaningful and its no longer an algorithm.

How do you write a program?

1 Decide what strops are needed to complete the task

2 steps

• Psrudocode

• flow chart

• Algorithm

• Three major families.,

o M/C Language

o Assembly Language

o High level Language

Fortra C Pascal

High level Language

Assembly Language

Machine Language

Hardware (CPU)

Sample Copy. Not Distribution.

Page 21: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

C Simpler: How to Program

2

High level programming Languages.,

• Each Language has a unique set of keywords and a special

syntax for organizing programe

• High level programming languages are simple compared to

human languages

• They are two broad decision.,

1. Procedural Language

2. Object – oriented Language (OOP)

Procedural Language., is characterized by sequential set of liner

commands.

- It focus on the structure

Object – oriented Language.,

- It is about modeling data

- It is splitted into modules

• Assembly language and machine language have the same

structure.

- Machine Language is of 0’s and 1s’s

- Assembly Language in words

• When speed is essential use can go straight with assembly

language.

Assembly language contains the same instructions as a machine

language, but the instruction and variables have names instead of

just numbers.

Machine Language

- Computer only understands

- Machine Language remains same everywhere

Compiler

• A compiler is a program which reads a

Sample Copy. Not Distribution.

Page 22: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

C Simpler: How to Program

3

“Program” in a source Language and translates it into a program

(often executable) (i,e) a target language

Source Language Compiler target language

(high level language) (Assembly level language)

What should a compiler do?

1. Preserve the meaning of the original source code

2. producer reasonably efficient target code.

3. To perform job quickly.

Preprocessing., to expand macros

//macros is a name given to a block of c statement as a

preprocessor directive

advantage – execution speed of the program fragment

disadvantage – size of the program

compilation., from source code to assembly language Assembly.,

from Assembly Language to machine code.

linking., to create the final executables.

Sample Copy. Not Distribution.

Page 23: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

Get Complete Book At Educreation Store

www.educreation.in

Sample Copy. Not Distribution.

Page 24: Sample Copy. Not Distribution.(Dr.K.Sujatha) am grateful to my friends and Family. I (Dr.S.Chidambaranathan) am grateful to my friends and Family. I (Dr.S.R.Boselin Prabhu) am grateful

Sample Copy. Not Distribution.