0% found this document useful (0 votes)
57 views

Programming in C and Data Structures: September 2014

This document is a book about programming in C and data structures written by Dr. Chandrakant. It contains an introduction to C language concepts like variables, operators, and input/output. It also covers branching and looping constructs in C like if/else statements, for/while loops. The book discusses arrays, strings, and functions in C including how to define, pass arguments to and return values from functions. It is intended to teach C programming and data structures.

Uploaded by

Keerthana M
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
57 views

Programming in C and Data Structures: September 2014

This document is a book about programming in C and data structures written by Dr. Chandrakant. It contains an introduction to C language concepts like variables, operators, and input/output. It also covers branching and looping constructs in C like if/else statements, for/while loops. The book discusses arrays, strings, and functions in C including how to define, pass arguments to and return values from functions. It is intended to teach C programming and data structures.

Uploaded by

Keerthana M
Copyright
© © All Rights Reserved
You are on page 1/ 9

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/263338168

Programming in C and Data Structures

Book · September 2014

CITATIONS READS

0 8,530

1 author:

Dr.Chandrakant Naikodi
MNC,Bangalore
69 PUBLICATIONS   40 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Publishing two books : Python Applications Programming and Fundamentals of Computer Programming View project

All content following this page was uploaded by Dr.Chandrakant Naikodi on 11 March 2015.

The user has requested enhancement of the downloaded file.


i i

“Chandrakant” — 2015/2/10 — 22:45 — page 1 — #1


i i

Programming in C and
Data Structures

CHANDRAKANT N

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page 2 — #2


i i

ABOUT AUTHOR
• Dr.Chandrakant N
Dr.Chandrakant Naikodi is presently working as a Senior Software Engineer in MNC,
Bangalore, India. He has successfully accomplished DCS, BE, ME and PhD degrees in
Computer Science and Engineering. He has published many research papers in referred
International Journals and Conferences. He is the author of two other technical books
namely “C:Test Your Aptitude” and “1000 Questions and Answers in C++” published by
Tata Mc-Graw Hill. His area of interest includes Computer Networks, MANETs, WSN,
Programming Languages etc.

PREFACE
• These days IT communities of Computer Science are encouraged to dive into different
languages and C language stands first among them. The C language is one of the popular
Computer Language, it has its own standard specifications. This book concentrates
on C introduction, syntaxes, examples,etc. I Dr.Chandrakant, as the author of this
work will appreciate the suggestions or feedback from readers and users of this book;
kindly communicate via email addresses chandrakant.naikodi@{yahoo.in, gmail.com,
facebook.com}.

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page 3 — #3


i i

ACKNOWLEDGEMENTS
My deep gratitude and thanks to my wife Mrs. Vidyadhare Chandrakant and my daughter
Vaishnavi N for their immense patience, prayers and support. My sincere thanks to my father
Mr. Dharmanna and mother Mrs. Shanthabhai Dharmanna for their blessings and support. I
thank my brothers Mr. Shankar N, Mr. Surykant N, father-in-law Mr. Venkatesh J, mother-in-
law Mrs. Kalavathi V, my brother-in-law Mr. Nataraj and Mr. Raghavendra.
I am greatly thankful to my well wishers and teachers, especially late Sri. B C Bhavikatti, Sri.
M S Stavermat, Sri. M B Naikodi, Dr. Badrinath GS, Smt. Anita Kanavalli, Sri. Sanjeevkumar
Chetti and Smt.Sushma B Malipatil who supported and encouraged me greatly in all steps.
This book could not be completed without the support of my friends and the help of several
individuals who extended their valuable support in the preparation and compilation of this
book.

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page 4 — #4


i i

I dedicate this book to my lovely


daughter Vaishnavi

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page i — #5


i i

Contents

1 INTRODUCTION to C LANGUAGE 1
1.1 Pseudocode Solution to Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Basic Concepts of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Declaration, Assignment and Print Statement . . . . . . . . . . . . . . . . . . . . . 3
1.4 Types of Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Programming Examples and Exercise . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 BRANCHING and LOOPING 25


2.1 Two Way Selection (if, if-else, nested if-else, cascaded if-else), ?:, switch . . . . . . 25
2.2 goto statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3 Loops (for, while, do-while) in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4 break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.5 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 31

3 ARRAYS, STRINGS and FUNCTIONS 47


3.1 ARRAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.1.1 Using an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.1.2 Using Arrays with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.1.3 Multi-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.2 STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.2.1 Declaring, Initializing, Printing and Reading Strings . . . . . . . . . . . . 54
3.2.2 Strings Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . 56
3.2.3 Strings Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . 61
3.2.4 Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2.5 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . 64
3.3 FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3.1 Functions in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3.2 Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.3.3 Functions and Program Structure . . . . . . . . . . . . . . . . . . . . . . . . 67
3.3.4 Location of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.3.5 Void and Parameterless Functions . . . . . . . . . . . . . . . . . . . . . . . 72
3.3.6 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.3.7 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . 75

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page ii — #6


i i

ii

4 STRUCTURES and FILE MANAGEMENT 101


4.1 Basics of Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
4.2 Structures and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.3 Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.4 Structure Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.5 Type Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4.6 Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.7 Defining File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
4.8 Opening and Closing of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
4.9 Input and Output Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.10 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 115

5 POINTERS and PREPROCESSORS 129


5.1 Pointers and Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.2 Pointers and Functions Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
5.3 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
5.4 Address Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.5 Character Pointer and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.6 Pointers to Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
5.7 Initialization of Pointers Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
5.8 Dynamic Allocations Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.9 Introduction to Preprocessors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
5.10 Compiler Control Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
5.11 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 149

6 INTRODUCTION to DATA STRUCTURES 167


6.1 Primitive and Non-primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . 167
6.2 Definition and Applications of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . 170
6.3 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
6.4 Linked Lists and Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
6.5 Programming Examples and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 190

7 Laboratory Programs 235


7.1 Quadratic Equation Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
7.2 Reverse an Integer Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.3 Square Root and Leap Year Finding Program . . . . . . . . . . . . . . . . . . . . . 238
7.3.1 Square Root Finding Program . . . . . . . . . . . . . . . . . . . . . . . . . . 238
7.3.2 Leap Year Finding Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
7.4 Polynomial using Horners Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
7.5 Find Sin(x) using Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
7.6 Find Sin(x) using Library Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
7.7 Bubble Sorting Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
7.8 Compute the Product of 2 Matrices A and B . . . . . . . . . . . . . . . . . . . . . . 248
7.9 Binary Searching Technique Program . . . . . . . . . . . . . . . . . . . . . . . . . . 250
7.10 Copy Operation and Count of Vowels/Consonants . . . . . . . . . . . . . . . . . . 252
7.10.1 STRCOPY operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page iii — #7


i i

iii

7.10.2 Frequency of Vowels and Count of Consonants . . . . . . . . . . . . . . . 253


7.11 Program to RightShift and Find Prime Number . . . . . . . . . . . . . . . . . . . . 254
7.11.1 Write a Program to RightShift(x ,n) . . . . . . . . . . . . . . . . . . . . . . . 254
7.11.2 Program to Find isprime(num) . . . . . . . . . . . . . . . . . . . . . . . . . 256
7.11.3 Find the Factorial of a Number . . . . . . . . . . . . . . . . . . . . . . . . . 257
7.12 Copy the Content of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
7.13 N Students Data using Array of Structures . . . . . . . . . . . . . . . . . . . . . . 259
7.14 Find Sum, Mean and Stddev of List of Numbers. . . . . . . . . . . . . . . . . . . . 262

8 Frequently Asked Questions (FAQs) 265


8.1 Questions and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
8.2 Difference between C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

i i

i i
i i

“Chandrakant” — 2015/2/10 — 22:45 — page 1 — #8


i i

Chapter 1

INTRODUCTION to C
LANGUAGE

C is a programming language which born at ”AT & T”s Bell Laboratory of USA in 1972. C
was written by Dennis Ritchi which is derived from B programming language and B was
derived from BCPL. A programming language is a formal constructed language intended to
communicate via instructions to a computer. A program written in 1s and 0s is called as a
machine language. As an alternative to binary bits (0/1), programmers started using English-
like words as commands that can be easily understood by users called assembly language.
Procedure-oriented languages are called high-level languages(HLL). The programmer does not
require remembering the architecture and registers of a CPU for developing a program. The
compilers are used to interpret high-level language program to a machine language. HLL are
COBOL, FORTRAN, BASIC, C, C++, Java etc.
C programming is one of the most popular computer programming languages that allow users
to create instructions for a computer to execute some task.

1.1 PSEUDOCODE SOLUTION TO PROBLEM


Writing comfortable high-level statements of a C program or algorithm intended for human
reading rather than machine reading. Pseudocode is used as a problem-solving tool which acts
as intermediate step(s) between English and C program. This is most acceptable procedure
because it helps programmers to translate English to C.
How to solve a problem? First requirement is to list a set of steps (algorithm) which describe
the operations necessary to obtain the solution.
When you get a problem, try to solve below questions,
(a) Carefully read the problem and understand what exactly expected to do?
(b) What outputs/answers to be printed?
(c) What are the steps to translate a problem from English to C?
(d) If a problem is too big, then check weather it is possible to break into pieces?

i i

i i

View publication stats

You might also like