Documente Academic
Documente Profesional
Documente Cultură
Board of Studies
___________________________________________________________________________________________
Subject Expert Panel
___________________________________________________________________________________________
Content Review Panel
___________________________________________________________________________________________
Copyright ©
This book contains the course content for Programming in ‘C’ and Data Structure.
Printed by
Universal Training Solutions Private Limited
Address
05th Floor, I-Space,
Bavdhan, Pune 411021.
All rights reserved. This book or any portion thereof may not, in any form or by any means including electronic
or mechanical or photocopying or recording, be reproduced or distributed or transmitted or stored in a retrieval
system or be broadcasted or transmitted.
___________________________________________________________________________________________
Index
I. Content....................................................................... II
IV. Application.............................................................. 86
V. Bibliography............................................................. 93
Book at a Glance
I
Contents
Chapter I........................................................................................................................................................ 1
‘C’ Fundamentals.......................................................................................................................................... 1
Aim................................................................................................................................................................. 1
Objectives....................................................................................................................................................... 1
Learning outcome........................................................................................................................................... 1
1.1 Introduction............................................................................................................................................... 2
1.1.1 Operating System...................................................................................................................... 2
1.1.2 Application Software................................................................................................................ 2
1.1.3 Programming Languages.......................................................................................................... 2
1.1.4 Advanced Development Tools.................................................................................................. 2
1.1.5 Web Based Tools....................................................................................................................... 2
1.2 Introduction to ‘C’..................................................................................................................................... 2
1.2.1 Low Level Languages............................................................................................................... 3
1.2.2 High Level Languages.............................................................................................................. 3
1.3 Identifier and Keywords........................................................................................................................... 3
1.4 Data Types and Constants......................................................................................................................... 4
1.4.1 Basic Data Types....................................................................................................................... 4
1.4.2 Type Qualifiers.......................................................................................................................... 5
1.4.2.1 Long........................................................................................................................... 5
1.4.2.2 Unsigned.................................................................................................................... 5
1.4.2.3 Unsigned long............................................................................................................ 5
1.5 Variables.................................................................................................................................................... 5
1.5.1 Variable Declaration................................................................................................................. 5
1.5.2 Variable Initialisation................................................................................................................ 6
1.5.3 Declaring Variables as Constants.............................................................................................. 6
1.6 Operators and Expressions........................................................................................................................ 6
1.6.1 Arithmetic Operators................................................................................................................. 6
1.6.2 Rational Operators.................................................................................................................... 7
1.6.3 Logical Operators..................................................................................................................... 7
1.6.4 Comma Operator....................................................................................................................... 8
1.6.5 Conditional Operators............................................................................................................... 8
1.6.6 Bitwise Operators..................................................................................................................... 8
1.6.7 Assignment Operators............................................................................................................... 9
1.6.8 Increment and Decrement Operators........................................................................................ 9
1.7 Preprocessor Directives............................................................................................................................ 9
1.7.1 Macro Expansion...................................................................................................................... 9
1.7.2 File Inclusion...........................................................................................................................11
Summary...................................................................................................................................................... 12
References.................................................................................................................................................... 12
Recommended Reading.............................................................................................................................. 12
Self Assessment............................................................................................................................................ 13
Chapter II.................................................................................................................................................... 15
Data Input and Output............................................................................................................................... 15
Aim............................................................................................................................................................... 15
Objectives..................................................................................................................................................... 15
Learning outcome......................................................................................................................................... 15
2.1 Introduction............................................................................................................................................. 16
2.2 Getchar Functions................................................................................................................................... 16
2.3 Putchar Function..................................................................................................................................... 16
2.4 Scanf( ) Function..................................................................................................................................... 17
2.5 Printf( ) Function..................................................................................................................................... 17
2.6 Gets ( ) and Puts ( ) Functions................................................................................................................ 18
II
Summary...................................................................................................................................................... 19
References.................................................................................................................................................... 19
Recommended Reading.............................................................................................................................. 19
Self Assessment............................................................................................................................................ 20
Chapter III................................................................................................................................................... 22
Control Statements..................................................................................................................................... 22
Aim............................................................................................................................................................... 22
Objectives..................................................................................................................................................... 22
Learning outcome......................................................................................................................................... 22
3.1 Introduction............................................................................................................................................. 23
3.2 Loops...................................................................................................................................................... 23
3.2.1 For Loop Statement................................................................................................................ 23
3.2.1.1 Execution of ‘For’ Statement................................................................................... 23
3.2.2 While Loop............................................................................................................................. 24
3.2.3 Do – While Statement............................................................................................................. 25
3.3 The Break Statement............................................................................................................................... 26
3.4 Continue Statement................................................................................................................................. 26
3.5 ‘If’ Statement.......................................................................................................................................... 27
3.6 ‘If Else’ Statement................................................................................................................................... 27
3.7 Switch Statement.................................................................................................................................... 27
3.8 ‘If Else If Ladder’................................................................................................................................... 28
3.9 Nested If.................................................................................................................................................. 28
3.10 Iteration Statement................................................................................................................................ 29
3.11 Nested For............................................................................................................................................. 29
3.12 Goto Statement...................................................................................................................................... 29
3.12.1 Conditional Goto................................................................................................................... 29
3.12.2 Unconditional Goto............................................................................................................... 29
Summary...................................................................................................................................................... 30
References.................................................................................................................................................... 30
Recommended Reading.............................................................................................................................. 30
Self Assessment............................................................................................................................................ 31
Chapter IV................................................................................................................................................... 33
Arrays and Strings...................................................................................................................................... 33
Aim............................................................................................................................................................... 33
Objectives..................................................................................................................................................... 33
Learning outcome......................................................................................................................................... 33
4.1 Introduction............................................................................................................................................. 34
4.2 Declaration of An Array.......................................................................................................................... 34
4.3 Initialisation of Array.............................................................................................................................. 34
4.3.1 Drawbacks of Initialising An Array........................................................................................ 34
4.4 Dimensions of An Array......................................................................................................................... 34
4.4.1 Single Dimensional Arrays..................................................................................................... 35
4.4.1.1 Declaration of Single Dimensional Arrays.............................................................. 35
4.4.1.2 Initialisation of One Dimensional Array.................................................................. 35
4.4.2 Two Dimensional Arrays........................................................................................................ 35
4.4.2.1 Elements of Multidimensional Array....................................................................... 36
4.4.2.2 Initialisation of Multidimensional Array.................................................................. 36
4.5 Strings..................................................................................................................................................... 36
4.5.1 String Functions...................................................................................................................... 37
4.6 Passing Array to Functions..................................................................................................................... 37
Summary...................................................................................................................................................... 38
References.................................................................................................................................................... 38
Recommended Reading.............................................................................................................................. 38
Self Assessment............................................................................................................................................ 39
III
Chapter V..................................................................................................................................................... 41
Functions and Structures........................................................................................................................... 41
Aim............................................................................................................................................................... 41
Objectives..................................................................................................................................................... 41
Learning outcome......................................................................................................................................... 41
5.1 Introduction to Functions........................................................................................................................ 42
5.2 Uses of Functions.................................................................................................................................... 42
5.3 Elements of User Defined Functions...................................................................................................... 42
5.3.1 Function Declaration............................................................................................................... 42
5.3.2 Function Call........................................................................................................................... 44
5.3.2.1 Call by Value............................................................................................................ 44
5.3.2.2 Call by Reference..................................................................................................... 44
5.3.3 Function Definition................................................................................................................. 44
5.4 Scope and Lifetime of Variables............................................................................................................. 45
5.4.1 Automatic Variables................................................................................................................ 45
5.4.2 External Variables................................................................................................................... 45
5.4.3 External Declaration............................................................................................................... 45
5.4.4 Static Variables....................................................................................................................... 46
5.4.5 Register Variables................................................................................................................... 46
5.5 Return Values.......................................................................................................................................... 46
5.6 Function Categories................................................................................................................................ 46
5.6.1 Functions with No Arguments and No Return Values............................................................ 46
5.6.2 Functions with Arguments and No Return Values.................................................................. 46
5.6.3 Functions with No Arguments and Return Values.................................................................. 47
5.6.4 Functions with Arguments and Returning Values................................................................... 47
5.7 Recursion................................................................................................................................................ 47
5.8 Introduction to Structure......................................................................................................................... 47
5.8.1 Declaring of a Structure.......................................................................................................... 48
5.8.2 Accessing Structure Elements................................................................................................. 48
5.8.3 Process of Storing Structure Elements.................................................................................... 49
5.9 Array of Structures.................................................................................................................................. 49
5.10 Additional Features of Structures.......................................................................................................... 50
5.11 Uses of Structures................................................................................................................................. 51
5.12 Unions................................................................................................................................................... 51
Summary...................................................................................................................................................... 52
References.................................................................................................................................................... 52
Recommended Reading.............................................................................................................................. 52
Self Assessment............................................................................................................................................ 53
Chapter VI................................................................................................................................................... 55
Pointers........................................................................................................................................................ 55
Aim............................................................................................................................................................... 55
Objectives..................................................................................................................................................... 55
Learning outcome......................................................................................................................................... 55
6.1 Introduction............................................................................................................................................. 56
6.2 Pointer Declaration................................................................................................................................. 56
6.3 Reference Operator................................................................................................................................. 56
6.4 Dereference Operator.............................................................................................................................. 57
6.5 Pointer Arithmetic................................................................................................................................... 57
6.5.1 Increment (++)........................................................................................................................ 58
6.5.2 Decrement (--)......................................................................................................................... 58
6.5.3 Addition (+) and Subtraction (-)............................................................................................. 58
6.5.4 Differencing............................................................................................................................ 58
6.6 Pointers with Function............................................................................................................................ 58
6.6.1 Call by Value........................................................................................................................... 59
IV
6.6.2 Call by Reference.................................................................................................................... 59
6.6.3 Callback Functions................................................................................................................. 59
6.7 Function Pointer Syntax.......................................................................................................................... 59
6.8 Initialising Function Pointer................................................................................................................... 59
6.9 Using Function Pointer........................................................................................................................... 60
6.10 Arrays and Pointer................................................................................................................................. 60
6.11 Array of Pointers................................................................................................................................... 61
6.12 Pointers with Structures........................................................................................................................ 61
6.13 Pointers on Pointer................................................................................................................................ 61
Summary...................................................................................................................................................... 63
References.................................................................................................................................................... 63
Recommended Reading.............................................................................................................................. 63
Self Assessment............................................................................................................................................ 64
Chapter VII................................................................................................................................................. 66
Introduction to Data Structure.................................................................................................................. 66
Aim............................................................................................................................................................... 66
Objectives..................................................................................................................................................... 66
Learning outcome......................................................................................................................................... 66
7.1 Introduction............................................................................................................................................. 67
7.2 Types of Data Structure........................................................................................................................... 67
7.3 Linear Data Structure.............................................................................................................................. 67
7.4 Types of Linear Data Structure............................................................................................................... 67
7.4.1 Array....................................................................................................................................... 67
7.4.2 Linear Link List...................................................................................................................... 71
7.4.3 Stack........................................................................................................................................ 73
7.4.4 Queues.................................................................................................................................... 74
7.5 Non-Linear Data Structure...................................................................................................................... 76
7.5.1 Tree......................................................................................................................................... 76
7.5.1.1 Definition of Tree..................................................................................................... 76
7.5.1.2 Types of Trees.......................................................................................................... 76
7.5.1.3 Tree Terminology..................................................................................................... 76
7.5.1.4 Rooted Tree.............................................................................................................. 77
7.5.1.5 Ordered Tree............................................................................................................. 78
7.5.1.6 Binary Trees............................................................................................................. 78
7.5.1.7 Example of Trees...................................................................................................... 79
7.5.1.8 Binary Search Tree................................................................................................... 80
7.5.1.9 ++ Tree Traversal Types........................................................................................... 81
7.5.1.10 Traversal Method................................................................................................... 82
Summary...................................................................................................................................................... 83
References.................................................................................................................................................... 83
Recommended Reading.............................................................................................................................. 83
Self Assessment . ......................................................................................................................................... 84
V
List of Figures
Fig. 7.1 Classification of data structures....................................................................................................... 67
Fig. 7.2 Visual example of Array.................................................................................................................. 68
Fig. 7.3 Inserting element............................................................................................................................. 70
Fig. 7.4 Linear link list.................................................................................................................................. 72
Fig. 7.5 Stack................................................................................................................................................ 73
Fig. 7.6 Stack physical view......................................................................................................................... 74
Fig. 7.7 Queue............................................................................................................................................... 75
Fig. 7.8 Queue physical view........................................................................................................................ 75
Fig. 7.9 Tree.................................................................................................................................................. 76
Fig. 7.10 Tree terminology........................................................................................................................... 77
Fig. 7.11 Rooted tree..................................................................................................................................... 78
Fig. 7.12 Binary tree..................................................................................................................................... 78
Fig. 7.13 Example of trees............................................................................................................................ 79
Fig. 7.14 Binary search tree’s example......................................................................................................... 80
VI
List of Tables
Table 1.1 Keywords........................................................................................................................................ 3
Table 1.2 Character set.................................................................................................................................... 3
Table 1.3 Basic data types............................................................................................................................... 4
Table 1.4 Data type – range of values............................................................................................................. 4
Table 1.5 Arithmetic operators........................................................................................................................ 6
Table 1.6 Rational operators........................................................................................................................... 7
Table 1.7 Logical operators............................................................................................................................. 7
Table 1.8 Operator precedence....................................................................................................................... 8
Table 1.9 Bitwise operators............................................................................................................................. 8
Table 2.1 Scanf function - character conversion........................................................................................... 17
Table 4.1 Elements of multidimensional array............................................................................................. 36
Table 4.2 Backslash escape........................................................................................................................... 36
Table 4.3 String functions............................................................................................................................. 37
VII
Chapter I
‘C’ Fundamentals
Aim
The aim of the chapter is to:
• elucidate datatypes
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of the chapter, you will be able to:
• define operations
1
Programming in ‘C’ and Data Structure
1.1 Introduction
Computer is a system, made up of two major components; hardware and software. The set of physical components
i.e., mouse, keyboards, monitor, CPU, etc. are the forms of the hardware. Software is the secondary component
which acts as an interface between a user and hardware.
Without software it is not possible to use hardware. Software is the set of programs, while a program is a set of
instruction given to the hardware. Software helps the user to run the programs. Computer system cannot be used
without it. In other words, software can be called the bridge between the user and the computer hardware. The
computer system is classified into five main categories based on the requirement of the user. These are as discussed
below.
Back end
DBMS or RDBMS are used to store the data entered by the user. Oracle, Sybase, Informix etc are some popular
back ends.
‘C’ is the programming language which is used to write the program and execute it. It was developed at AT & T’s
Bell Laboratories in USA in1972 and was written by Dennis Ritchie. It is popular because of its reliability, simplicity
and user friendly facility. COBOL was the language which was used for commercial purpose. BASIC was used as
beginner’s language and FORTRAN was used for engineering applications.
2
There are mainly two types of languages, known as low level and high level languages.
letter ABCDEFGHIJKLMNOPQRSTUVWXYZ
digit 0123456789
underscore _
punctuation ! " # % & ' () * + , - . / ; < = > ? [ \ ] ^ { | } ~
3
Programming in ‘C’ and Data Structure
The programmer has to declare as to which data type has to be used for each data object for most of the programming
languages and most database systems require the user to declare the type of each data field. The data types differ from
one programming language to another and from one data base application to another. The constant value is the entity
that does not change when the program is executed. It refers to a value. ‘C’ supports various types of constants.
Integer int
Floating point float
Double floating point double
Character char
Void void
• int
used to define inter numbers
no decimal points are used
takes 16 bytes of memory to store the value on 16 bit machine
an integer can be positive or negative
• float
used to define floating point numbers
stores only floating point values
the values should be in decimal places and it takes 32 bytes of memory to store the value on 16 bit
machine
• double
used to define big floating points
used to get the store on 16 bit machine, it requires 64 bytes of memory
• char
used to define characters
used to store single character or number at a time
used a 16 bit machine, 8 byte memory is required for character type of data
• void
using void data type, we can specify a function
4
1.4.2 Type Qualifiers
Type qualifiers declare the variables along with the data types.
1.4.2.1 Short
• Short int is an integer of lower range.
• Short int may require less memory than an ordinary int or require same amount of memory, but will never
exceed an ordinary int in word length.
• The short int will have half the memory requirements, if long int and int have the same requirements.
1.4.2.2 Long
• Long int is an integer of increased range. Long int may require more memory than ordinary int or may require
the same amount of memory but will never be less than an ordinary int.
• Long int will have double the requirements, if short int and an ordinary int both have the same requirements.
1.4.2.3 Unsigned
• An unsigned int is the integer with no negative range, the space capacity is used to increase positive range
• Unsigned int and int have similar memory requirements.
• The unsigned int can be twice as large as an ordinary int.
1.5 Variables
A variable is an identifier which is used to represent a specific type of information within a designated portion of
a program. It represents a single data item, i.e., numerical quantity or a character constant. A variable is the entity
that changes when the program is executed. They can be changed at different times. A variable is a name which
stores a value in the memory. It stores only one value at a time. If the variable is of the real type it can be stored
using float or double data type.
Example,
• int num
• float percent
• char grade
5
Programming in ‘C’ and Data Structure
Example,
consta float pi = 3.14
6
1.6.2 Rational Operators
• Rational operators are required to compare the relationship between operands.
• They are popularly known as comparison operators. ‘C’ supports the following rational table.
• The operands fall within the same precedence group, and are lower than the arithmetic and unary operators.
• The equal to (=) and not equal to (! =) operators fall within different precedence group, beneath the rational
operators.
7
Programming in ‘C’ and Data Structure
It checks the condition and evaluates the result depending upon the status of the condition.
i.e. true or false.
Example,
max = a > b ? a : b
Here,
if a>b, then the condition becomes true and max variable will store the value of a
if a>b is false, then max variable will store the value of b
Thus, the conditional operator evaluates true or false and returns the result.
8
1.6.7 Assignment Operators
• In ‘C’ language, there are several different operators that are used to form assignment expressions which assign
the value of an expression to an identifier.
• The most commonly used assignment operator is '='. This is used to assign some value to the variable.
• The arithmetic operations precede assignment operation.
Example,
b = 10
Here, value 10 is assigned to the variable b.
#define UPPER 25
main ( )
{
int I ;
for ( i = 1 ; i ≤ UPPER ; i ++ )
printf ( “\n%d”, i ) ;
9
Programming in ‘C’ and Data Structure
}
In this program, instead of writing 25 in the ‘for’ loop, we are writing it in the form of UPPER, which has already
been defined before ‘main ( )’ through the statement,
#define UPPER 25
This statement is called ‘macro definition’ or more commonly, just a ‘macro’. During preprocessing, the preprocessor
replaces every occurrence of UPPER in the program with 25.
#define PI 3.1415
main ( )
{
float r = 6.25 ;
float area ;
area = PI* r*r ;
printf ( “\nArea of circle = %f”, area ) ;
}
UPPER and PI in the above programs are often called ‘macro templates’, whereas, 25 and 3.1415 are their
corresponding ‘macro expansions’.
if ( ( f , 5 ) AND ( x ≤ 20 OR y ≤ 45 ) )
printf ( “\nYour PC will alwys work fine…” ) ;
else
print (“\nIn front of the maintenance man” ) ;
}
if (ARRANGE)
printf ( “within range” ) ;
else
printf ( “out of range” ) ;
}
10
A #define directive could be used to replace even an entire ‘C’ statement.
#define FOUND printf ( “X virus” ) ;
main ( )
{
char signature ;
if ( signature = =’Y’ )
FOUND
else
printf ( “Safe… as yet !” ) ;
}
11
Programming in ‘C’ and Data Structure
Summary
• Operating system is a platform to use the computer system. Windows XP, Linux, MAC, etc. are a few examples
of the operating systems that are used. Java, ‘C’, ‘C+ +’, etc. are computer programming languages.
• Further, the programming languages are divided into two main types:
low level programming language
high level programming language
• A low level programming language interacts with the machine hardware, while the high level programming
language interacts in human understandable language, like English.
• In ‘C’ language, every word has a keyword or an identifier along with a character set.
• The constant value is the entity that does not change when the program is executed and it refers to a value.
• ‘C’ consists of various data types.- int, float, double, char and void.
• Short, long, unsigned and unsigned long are the data qualifiers.
• The variables are declared along with the data types so that the compiler understands which type of variable
it is.
• An operator is a symbol that enables the user to command the computer and perform certain manipulations and
calculations. These are classified as- arithmetic operators, rational operator, logical operator, comma operator,
conditional operation, bitwise operator, assignment operator and increment and decrement operators.
• In the code of any program, preprocessor directives are the lines included which are not program statements
but directives for the preprocessor.
References
• Dixit, J. B., 2005. Fundamentals of Computers and Programming in C, Firewall Media.
• Balagurusamy, 2008. Programming In Ansi C, 4th ed. Tata McGraw-Hill Education.
• Lipari, G., 2012. Fundamentals of Programming Introduction to the C language [Pdf] Available at: <http://retis.
sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf> [Accessed 26 June 2013].
• Introduction to the C Programming Language [Pdf] Available at: <http://163.25.99.51/Huang_Computer/
Programming%20Language-website2.pdf> [Accessed 26 June 2013].
• Dr. Kumar, A. S., 2008. Lecture - 1 Introduction to programming languages [Video online] Available at: <http://
www.youtube.com/watch?v=EbNJ05EVXs0> [Accessed 26 June 2013].
• PFC #1: Computer Basics, Programming, Languages, Algorithms, Flowcharts [Video online] Available at:
<http://www.youtube.com/watch?v=c_sDgHgNR6w> [Accessed 26 June 2013].
Recommended Reading
• Deitel, J. P. & Deitel, M. H., 2009. C: How to Program, 6th ed. Prentice Hall.
• Kochan, G. S., 2012. Programming in Objective-C, 4th ed. Addison-Wesley Professional.
• Kochan, 2005. Programming In C, 3/E, Pearson Education India.
12
Self Assessment
1. Low level languages interacts with machine __________.
a. software
b. hardware
c. operating system
d. programming language
2. In ‘C’ language, every __________ has a keyword or an identifier along with a character set.
a. sentence
b. line
c. word
d. segment
3. Increment and decrement operators are the shortcuts used to increment the values. The operator __________
is used to increase the value by 1 and the operator __________ is the decrement operator, used to reduce value
by 1.
a. ' + + ' , ' - - '
b. ' - ' , ' + + '
c. ' +' , ' - -'
d. ' - - ' , ' + +'
4. __________ is used to separate two or more expressions when only one expression is expected.
a. Assignment operator
b. Arithmetic operator
c. Comma operator
d. Conditional operator
13
Programming in ‘C’ and Data Structure
8. Which symbol is generally used to define the lines in the preprocessor directives?
a. Percentage ( % ) sign
b. Hash ( # ) sign
c. Ampersand ( & ) sign
d. Underscores ( _ ) sign
10. Which operators are required to compare the relationship between operands?
a. Logical operators
b. Rational operators
c. Conditional operators
d. Comma operators
14
Chapter II
Data Input and Output
Aim
The aim of the chapter is to:
Objectives
The objectives of this chapter are to:
• explicate scanf function, printf function and gets and puts function
Learning outcome
At the end of this chapter, you will be able to:
15
Programming in ‘C’ and Data Structure
2.1 Introduction
Input and output data is done by a collection of library functions like getchar, putchar, scanf( ), printf( ), gets and
puts in ‘C’ language, which permit the transfer of information between the computer and the standard input and
output devices.
Following a list of parameters enclosed in parentheses, simply by writing the function name an input or output
function can be accessed from anywhere within a program. The empty parenthesis must appear though some input
output functions do not require parameters. ‘C’ includes a collection of header files which provides the information
that is necessary in support of various library functions. Studio.h is the header file that contains the information
about the input or output library function.
Example,
char c;
c = getchar ()
From the standard input device, the second line causes a single character to be entered and then assigned to c. By
reading one character at a time within a multipass loop, this function can also be used to read multicharacter strings.
The value of the symbolic constant ‘end-of-file’ will automatically be returned if it is encountered while reading a
character with the getchar function.
Example,
putchar (‘N’)
putchar (‘a’)
putchar (‘t’)
putchar (‘i’)
putchar (‘o’)
putchar (‘n’)
putchar (‘a’)
putchar (‘l’)
However, each character must be output separately, when putchar is used. In the given statements, the parameters
to the function calls are character constants, represented between apostrophes as usual. The arguments could be
character variable instead. getc and putc are the two functions those require FILE pointers which are similar to
getchar and putchar except that they can operate on files other than the standard input and output. The getc function
is called with one argument. The expression getc(stdin) is similar to getchar( ) and the expression putc(c, stdout)
is same as putchar(c).
16
2.4 Scanf( ) Function
Scanf( ) is a C library function. By means of this function, input data can be entered from a standard input device
into the computer memory. This function is generally used to enter any combination of numerical values, single
character and strings. The function returns the number of data items that have been entered successfully.
The general form to write scanf( ) function is:
where,
String - string containing required formatting information
Parameter 1 and parameter 2 - parameters that represent the individual input data item.
Each character group must start with percent sign (%).
The parameters whose types match the corresponding characters groups in the control string are written as variables
or arrays. Each variable must be preceded by ampersand (&) sign. If two or more characters are entered, they must
be separated by white space characters. Since the newline character is considered to be white space character, data
items may continue onto two or more lines. All the arguments must be pointed to the variables used as arguments.
Parameters can be written as constants, single variables or array names or more complex expressions. Parameters
in printf( ) function do not represent memory addresses, unlike scanf( ) function and thus they are not preceded by
ampersand (&) sign. The control string is composed of individual groups of characters, with one character group for
each output data item. Each character group must start with a percent sign (%) followed by a conversion character
17
Programming in ‘C’ and Data Structure
indicating the type of the corresponding data item. Multiple characters can be separated by whitespace characters.
There are two modifiers of printf( ) function. Long integer is used to display long data type and short integer is used
to display short data type.
Example,
Char school [40]
gets(school)
puts(school)
These lines use gets and puts to transfer the line of text in and out of the computer. It will give the same result as
the scanf and printf function for input and output of given variable or array, when this program is executed.
18
Summary
• Input and output data is done by a collection of library functions like getchar, putchar, scanf, printf, gets and
puts in ‘C’ language, which permit the transfer of information between the computer and the standard input
and output devices.
• Getchar function reads a single character from standard input. It does not take parameters and the input character
is its returned value.
• The standard ‘C’ function that prints or displays a single character by sending it to standard output is called
putchar.
• Scanf is the function where input data can be entered from a standard input device into the computer
memory.
• Printf function is used to print out a message, either on screen or on paper. The letter ‘f’ in printf either stands
for “formatted” or “function”.
• Gets and puts functions facilitate the transfer of strings between the computer and the standard input or output
devices.
References
• Goel, A., Computer programming-I: As per the first-year engineering syllabus of University of Mumbai, Pearson
Education India.
• Veerana, V. K. & Jankidevi, S. J., 2007. C for U Including C and C Graphics, Firewall Media.
• Kernighan, W. B. & Ritchie, M. D., 1988. The C programming Language [Pdf] Available at: <http://net.pku.
edu.cn/~course/cs101/2008/resource/The_C_Programming_Language.pdf> [Accessed 26 June 2013].
• Input and Output in C [Pdf] Available at: <http://www.prog2impress.com/downloads/Input%20and%20
Output%20in%20C.pdf> [Accessed 26 June 2013].
• Introduction to C Programming I [Video online] Available at: <http://www.youtube.com/watch?v=fUcSXbQn
rLI&list=PL66D13A5B3585C213> [Accessed 26 June 2013].
• C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 1 [Video online] Available at:
<http://www.youtube.com/watch?v=5v7h4ZmgMvw> [Accessed 26 June 2013].
Recommended Reading
• Rajaraman, V., 1994. COMPUTER PROGRAMMING IN C, PHI Learning Pvt. Ltd.
• Reddy, R. & Ziegler, C., 2009. C Programming for Scientists and Engineers with Applications, Jones & Bartlett
Learning.
• Dixit, J.B., 2006. Comprehensive Programming in C and Numerical Analysis, Laxmi Publications.
19
Programming in ‘C’ and Data Structure
Self Assessment
1. Parameters of the printf function can be written as constants, __________ variables or array names or more
complex expressions.
a. double
b. triple
c. single
d. external
2. The meaning of conversion character __________ is type of data item is decimal, hexadecimal or octal
integer.
a. S
b. U
c. O
d. I
3. By reading one character at a time within a multipass loop, __________ can also be used to read multicharacter
strings.
a. getchar function
b. putchar function
c. printf function
d. scanf function
20
7. Which function reads a single character from standard input?
a. getchar
b. putchar
c. printf
d. scanf
8. Parameter of which function must be data item that represents a string and the string may include whitespace
characters.
a. printf
b. scanf
c. putchar
d. gets and puts
9. Type of data item is string which may include whitespace characters is the meaning of which conversion
character?
a. S
b. [. . .]
c. U
d. I
10. The expression getc(stdin) and putc(c, stdout) are similar to which functions?
a. getchar(c) and putchar(c)
b. scanf() and printf()
c. scanf ( )
d. printf ( )
21
Programming in ‘C’ and Data Structure
Chapter III
Control Statements
Aim
The aim of the chapter is to:
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of this chapter, you will be able to:
• define if statement
22
3.1 Introduction
Control statements are the statements in ‘C’ language that can alter the flow of a sequence of instruction. It is used
to carry out some actions. These statements enable the user to jump from one program to another. The program
transfer may be conditional or unconditional. All control statements involve the use of logical expressions.
Example:
a<b
3.2 Loops
The versatility of the computer lies in its ability to perform a set of instruction repeatedly. This involves some portion
of the program, either a specified number of times or until a particular condition is being satisfied. This repetitive
operation is done through a loop control instructions. There are three methods by which we can repeat a part of a
program. They are as follow:
• using a ‘for’ statement
• using a ‘while’ statement
• using a ‘do-while’ statements
23
Programming in ‘C’ and Data Structure
while ( i ≤ 10 )
while ( i ≥ 10 && j ≤ 15 )
while ( j > 10 && (b < 15 || c < 20 ) )
The statement within the loop may be a single line or a block of statements. in the first case the parentheses
are optional.
Example:
while ( i ≤ 10 )
i = i +1 ;
Is same as
while ( i ≤ 10 )
{
i=i+1;
}
As a rule, the ‘while’ must test a condition that will eventually become false.
main ( )
{
int i = 1 ;
while ( i ≤ 10)
printf ( “%d\/”, i) ;
}
24
This is an indefinite loop, since ‘i’ remains equal to 1 forever.
The correct form would be,
main ( )
{
int i = 1 ;
while ( i ≤ 10)
{
printf ( “%d\n”, i ) ;
}
}
Instead of incrementing loop counter, we can even decrement it and still manage to get the body of the loop executed
repeatedly. The following format may be referred.
main ( )
{
int i = 5 ;
while ( i ≥ 1 )
{
printf ( “\nmake the computer literate!” ) ;
i=i–1;
}
}
It is not necessary that a loop counter must only be an ‘int’. It can even be ‘float’.
Example:
main ( )
{
float a = 10.0 ;
while ( a ≤ 10.5 )
{
printf ( “\nRaindrops on roses.....” ) ;
printf ( “....and whiskers on kittens” ) ;
a = a + 0.1 ;
}
}
Even floating point loop counters can be decremented. Once again the increment and decrement could be by any
value, not necessarily 1.
25
Programming in ‘C’ and Data Structure
do
{
statement
}
while (expression) ;
• Here, the expression is evaluated after the statement is executed. If the condition expression is true then the
body is executed again and this process continues till the expression becomes false. The loop terminated when
the expression becomes false.
while ( /* . . */ ) {
while (/* . . */ ) {
/* . . */
Break;
}
/* when the break executes the program, the program continues to execute from here */
}
Example:
main ( )
{
int i, j ;
for ( i = 1 ; i ≤ 2 ; j ++ )
{
for (j = 1 ; j ≤ 2 ; j ++ )
{
if ( i = = j )
continue ;
printf ( “\n%d %d\n”, i, j ) ;
}
}
}
26
The output of the above program would be,
12
2 1
if (condition)
statement;
if ( expression )
statement 1
else
statement 2
Switch (expression)
{
case value -1:
27
Programming in ‘C’ and Data Structure
__
_ _ block of statements
break;
Case value -2:
__
_ _ block of statements
break;
__
__
default:
__
default block statements
break;
}
if (condition1)
{
__
__
}
Else if (condition 2)
{
__
__
}
Else
__
__
}
3.9 Nested If
‘Nested if’ statements are used when multiple conditions are introduced in a particular sequence.
28
{
__
__
}
{
goto here
/* . . */
Conditional and unconditional goto are the two types of goto statements used in a computer program.
29
Programming in ‘C’ and Data Structure
Summary
• Control statements are the statements in ‘C’ language that can alter the flow of a sequence of instruction. These
statements enable the user to jump from one program to another.
• ‘If’ statement is used in making a decision and allowing the flow of execution of a program.
• ‘If else’ statement is used to express decision.
• The ‘while’ statement can terminate when ‘break’, ‘goto’ or ‘return’ like statement body is executed.
• The ‘do while’ loop tests at the bottom of the loop after executing the body of the loop.
• The ‘for’ loop provides a more concise loop control structure.
• Goto statement makes the program jump to a given label.
References
• Ashok, K., 2010. C Programming: Test Your Skills, Pearson Education India.
• Ackermann, C. E., 1989. The Essentials of C Programming Language, Research & Education Assoc.
• Control Statements [Pdf] Available at: <http://download.nos.org/srsec330/330L9.pdf> [Accessed 26 June
2013].
• Program Control Statements [Pdf] Available at: <http://www.uotechnology.edu.iq/dep-eee/lectures/1st/
Computer%20science/2.pdf> [Accessed 26 June 2013].
• Control Statements in Programming Languages (final) part1 [Video online] Available at: <http://www.youtube.
com/watch?v=hX0Rrq8ouxY> [Accessed 26 June 2013].
• Control statements in programming languages-part 1-pldc [Video online] Available at: <http://www.youtube.
com/watch?v=ageuh_4_J9E> [Accessed 26 June 2013].
Recommended Reading
• Balagurusamy, E., Programming In Ansi C, 5E, Tata McGraw-Hill Education.
• Krishnamoorthy, 2010. DATA STRUCTURES USING C, Tata McGraw-Hill Education.
• Hanly, 2009. Programming In C And Data Structures (For Jntu), Pearson Education India.
30
Self Assessment
1. __________ statement is often used in making a decision and allowing the execution of a program.
a. if else
b. do while
c. do
d. if
2. The __________ loop tests at the bottom of the loop after executing the body of the loop.
a. switch
b. goto
c. do while
d. continue
31
Programming in ‘C’ and Data Structure
10. On what do the iterative statements depend to repeat the execution of the list of the statements?
a. Case label
b. Default case label
c. Integer expression
d. Case expression
32
Chapter IV
Arrays and Strings
Aim
The aim of this chapter is to:
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of this chapter, you will be able to:
• understand strings
33
Programming in ‘C’ and Data Structure
4.1 Introduction
• Array type is a data type. It is a variable that holds multiple elements which has the same data type.
• It is the capability that enables the user to define a set of ordered data items.
• In ‘C’ language, collection of the variables or values are selected by one or more indices those can be computed
by the computer by its run time. Such collection is called an array variable, array value or array.
• Variables are called grades in ‘C’ language. These grades represent the entire set of grades and not a single
value of grade.
• Each element of the set is referred by the index number or subscript.
data_type array_name[size]
The values in the list can be separated by commas. (Refer to the representation given below:)
• The digit 3 denotes the array size whereas, 0 denotes the number of the values of each element in the list which
are less than the number of elements. Only in such a case, many elements can be initialised and the remaining
elements will be set to 0 automatically.
• The compiler allocates enough space for all initialised elements when the size is omitted.
Example:
34
4.4.1 Single Dimensional Arrays
• It is the collection of multiple elements of the same data type.
• A number of location gets created where we get access to all the elements one by one.
• The above syntax defines two dimensional arrays where rows and column are constants.
• 'Array2d' is an array of length denoting the ROWS and ‘int’ denoting COLUMNS.
35
Programming in ‘C’ and Data Structure
In the above example, the array initialises the elements of the first row to zero and those in the second row to 1.
The initialisation is done row by row. The above statement can also be written in the form of:
4.5 Strings
• In ‘C’ language, strings are the arrays of ‘char’ object. In other words, these are defined as the arrays of
characters.
• An array of string can be declared like a two dimensional array.
• The string characters are compiled to an array of specific character value with an additional null terminating
character code, which has a value 0 to mark the end of the string. These string constants are surrounded by
double quotes (“).
• Backslash escape [\ n] may be used to include a newline in strings.
Escape Meaning
\\ Literal backslash
\“ Double quote
\‘ Single quote
\n Newline
\r Carriage return
\b Backspace
\t Horizontal tab
\f Form feed
\a Alert
\v Vertical tab
\? Question mark
\ nnn Character with octal value nnn
\ xhh Character with hexadecimal value hh
36
4.5.1 String Functions
strcat (dest, source) Appends the string ‘source’ to the end of the string ‘dest’
Strchr (s, c) Finds the first instance of character ‘c’ in string ‘s’ and returns a pointer to it or a
null pointer if ‘c’ is not found
Strcmp (a, b) Compares string a and b, returns negative if a is less than b, 0 is equal, positive if
greater
Strcpy (dest, source) Copies the string ‘source’ onto the string ‘dest’
Strlen (st) Return the length of string ‘st’
Strncat (dest, source, n) Appends a maximum of ‘n’ character from the string ‘source’ to the end of string
‘dest’ and null terminates the string at the end of the input or at index’ n+1 when
the max length is reached
Strncmp (a, b, n) Compares maximum of ‘n’ characters from strings ‘a’ and ‘b’; returns negative if
‘a’ is less than ‘b’, 0 if equal and positive if greater.
Strrchr (s, c) Find the last instance of character ‘c’ in string ‘s’ and returns a point to it or a null
pointer if ‘c’ is not found
Strcoll (s1, s2) Compare two strings according to a locale-specific collating sequence
Strcspn (s1, s2) Returns the index of the first character in ‘s1’ that matches any character in ‘s2’
Strerror (errno) Returns a string with an error message corresponding to the code in errno
Strncpy (dest, source, n) Copies ‘n’ character from the ‘source’ onto the string ‘dest’, substituting null byte
once past the end of ‘source’; does not null terminate if max length is reached
Strpbrk (s1, s2) Returns a pointer to the first character in ‘s1’ that matches any character in s2 or a
null pointer if not found
Strspn (s1. s2) Returns the index of the first character in ‘s1’ that matches no character in s2
Strstr (st, subst) Returns a pointer to the first occurrence of the string ‘subst’ in
St’ or a null pointer if no such substitute exists
Strtok(s1, s2) Returns a pointer to a token within ‘s1’ delimited by the character in ‘s2’
Strxfrm(s1, s2, n) Transforms ‘s2’ onto ‘s1’, such that ‘s1’ used with ‘strcmp’ gives the same results
as ‘s2’ used with ‘strcoll’
37
Programming in ‘C’ and Data Structure
Summary
• Array is a variable that holds multiple elements which has the same data type.
• Array must be declared before they are used like any other variable.
• While initialising an array the user has to provide initialising values which are enclosed by the curly brackets
in the declaration followed by an equal sign after the array name.
• Multidimensional array is the array with more than one index value.
• Strings are the arrays of ‘char’ object.
References
• Hoover, 2009. System Programming With C And Unix, Pearson Education India.
• Laitinen, K., 2004. A Natural Introduction to Computer Programming with C#, Trafford Publishing.
• Arrays and Strings [Pdf] Available at: <http://pages.cpsc.ucalgary.ca/~jacob/Courses/Fall00/CPSC231/Slides/10-
ArraysAndStrings.pdf> [Accessed 26 June 2013].
• Lecture 7: Arrays, strings, and functions [Pdf] Available at: <http://www.stanford.edu/~fringer/teaching/
operating_systems_03/handouts/lecture7.pdf> [Accessed 26 June 2013].
• C Programming Tutorial: Arrays (One and Two Dimensional Array ): Lesson 2 [Video online] Available at:
<http://www.youtube.com/watch?v=NFJMjgXb0uA> [Accessed 26 June 2013].
• C Programming Tutorial 44 - Strings and Characters [Video online] Available at: <http://www.youtube.com/
watch?v=EJBb8nQD-4g> [Accessed 26 June 2013].
Recommended Reading
• Malik, D. S., 2012. C++ Programming: From Problem Analysis to Program Design, 6th ed.: From Problem
Analysis to Program Design, 6th ed. Cengage Learning.
• Lafore, 2002. Object Oriented Programming In C++, 4/E, Pearson Education India.
• Malik, D. S., 2009. C++ Programming: Program Design Including Data Structures: Program Design Including
Data Structures, 4th ed. Cengage Learning.
38
Self Assessment
1. Variables are called __________ in ‘C’ language.
a. groups
b. grades
c. loops
d. arrays
39
Programming in ‘C’ and Data Structure
40
Chapter V
Functions and Structures
Aim
Aim of this chapter is to:
• explain functions
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of this chapter, you will be able to:
41
Programming in ‘C’ and Data Structure
• Any ‘C’ function, by default it returns an ‘int’ value. More specifically, whenever a call is made to a function,
the compiler assumes that this function would return a value of the type ‘int’. If we desire that a function should
return a value other than an ‘int’, then it is necessary to explicitly mention so in the calling function as well as
in the called function.
• Suppose we want to find out square of a number using function, consider following simple program.
42
main ( )
{
float a, b ;
b = square ( a ) ;
printf ( “\nSquare of %f is %f”, a, b ) ;
}
square ( float x )
{
float y ;
y=x*x;
return ( y ) ;
}
The first answer among the three is correct. The square of 1.5 is definitely not 2, nor is 6 a square of 2.5. This
happened because any ‘C’ function, by default, which always returns an integer value. Therefore, even though the
function ‘square ( )’ calculates the square of 1.5 as 2.25, the program crops up when this 2.25 is to be returned to
‘main ( )’. ‘Square ( )’ is not capable of returning a ‘float’ value to overcome this. Let us consider the following
program segment to illustrate how to make ‘square( )’ capable of returning a ‘float’ value.
main ( )
{
float square ( float ) ;
float a, b
printf ( “\nEnter any number” ) ;
scanf ( “%f”, &a ) ;
b = square ( a) ;
printf ( “\nSquare of %f is %f”, a, b ) ;
}
43
Programming in ‘C’ and Data Structure
Now the expected answers are, 2.25 and 6.25. Note that the function ‘square ( )’ must be declared in ‘main ( )’ as,
float square ( float ) ;
This statements is often called a prototype declaration of the ‘square ( )’ function. What it means is ‘square ( )’ is
a function that receives a ‘float’ and returns a ‘float’. We have done the prototype declaration in ‘main ( )’ because
we have called it from ‘main ( ). There is a possibility that we may call ‘square ( )’ from several other functions at
the beginning of the program.
44
5.4 Scope and Lifetime of Variables
• A period in which the variables hold the given value at the time of program execution is called lifetime of
variables.
• There are two types of variables, which include:
global variables – available throughout the program
local variables – accessible by the functions in which they are declared
main ( )
{
extern float amt;
__
__
}
fun1( )
{
extern float y: / / declarations
__
}
float y; / / definition
45
Programming in ‘C’ and Data Structure
static int i;
static float j;
return
or
return (expression)
46
• The compiler omits or discards extra formal or actual parameters if the number of actual parameters should
be equal to formal parameters. The type of the data of both the parameters should be same.
• This concept can be implemented in programs. Arguments can be variables of any data type like ‘char’, ‘int’,
‘float’, ‘double’ or an array or pointer.
5.7 Recursion
A function is called ‘recursive’ if a statement within the body of a function calls the same function. Sometimes
called ‘circular definition’, recursion is thus the process of defining something in terms of itself.
Example,
main ( )
{
int a, fact ;
face = factorial ( a ) ;
printf ( “Factorial value = %d”, fact ) ;
}
factorial ( int x )
{
int f = 1, i ;
for ( i = x ; i ≥ 1 ; i -- )
f=f*i;
return ( f ) ;
}
The output is,
Enter any number - 3
Factorial value = 6
main ( )
{
struct book
47
Programming in ‘C’ and Data Structure
{
char name ;
float price ;
int pages ;
};
struct book b1, b2, b3 ;
48
5.8.3 Process of Storing Structure Elements
Whatever be the elements of a structure, they are always stored in contiguous memory location.
Consider following illustration,
for (i = 0 ; i ≤ 99 ; i ++ )
{
printf ( “\nEnter name, price and pages” )
scanf ( “%c %d”, &b[i].name, &b[i].price, &b[i].pages ) ;
49
Programming in ‘C’ and Data Structure
}
for ( i = 0 ; i ≤ 99, i ++ )
printf ( “\n%c %f %d”, b[i].name, b[i].price, b[i].pages ) ;
}
linkboat ( )
{
float a = 0, *b ;
b = &a ; /* cause emulator to be linked */
a = *b ; /* suppress the warning – variable not used */
}
main ( )
{
struct employee
{
char name [10] ;
int age ;
float salary ;
};
struct employee e1 = { “Sanjay” , 30,5500.50 } ;
struct employee e2, e3 ;
/* peice-meal copying */
strcpy ( e2.name, e1.name ) ;
e2.age + e1.age ;
e2.salary = e1.salary ;
50
5.11 Uses of Structures
• changing the size of the structure
• clearing the contents of the screen
• placing the cursor at an appropriate position an screen
• drawing any graphics shape on the screen
• receiving a key from the keyboard
• checking the memory size of the computer
• finding out the list of equipment attached to the computer
• formatting a floppy
• hiding a file from the directory
• displaying the directory of a disk
• sending the output to printer
• interacting with the mouse
5.12 Unions
Like structures, unions too contain the members whose individual data types may differ from one another. The unions
are used to conserve memory since all members share the same storage area within the computer memory. They are
useful for application that involves multiple numbers. Unions can be declared using the keyword union.
51
Programming in ‘C’ and Data Structure
Summary
• Function is a sub program or set of instructions which is used to write a particular task.
• Function declaration is very similar to variable declaration.
• Calling a function in the program by writing the function name and if necessary by passing the argument is
called function call.
• When we call function by passing normal values or variables, the function call is called as call by value.
• If we pass the reference of variables or values at the time call, instead of passing variables or values, then
function call is called call by reference.
• Global and local are the two types of variables.
• Automatic variables are declared in the function in which they are utilised. They are known as local
variables.
• External variables are the global variables. They are declared above the main ( ) and are accessible to all
functions in the program.
• Static variables are the one whose value persists till the end of the program.
• Register is the temporary memory which is attached to the CPU.
• Return values are the values which are returned by the function to the calling function.
• The function with no arguments and no return value is the function which does not return any value and to
which we do not pass any arguments.
• The called function receives the actual arguments that we pass in its definition as formal parameters.
• The functions with no arguments and return value are the functions which do not accept arguments but returns
the value.
• Recursion is a function recursion when the function calls itself in its own body.
• Structure allows the user to club different data types together.
• Array of structures is used to store more than one record.
References
• Deshpande, P. S. & Kakde, O. G., 2004. C and Data Structures, Cengage Learning.
• Kernighan, W. B. & Ritchie, D., 1988. C Programming Language, 2nd ed. Prentice Hall.
• Functions in C [Pdf] Available at: <http://www.nada.kth.se/kurser/master/intro/Course03-04/Handouts/
Flecture05.pdf> [Accessed 26 June 2013].
• Data Structures Using C [Pdf] Available at: <http://www.gatesit.org/gitdownloads/C&DS.pdf> [Accessed 26
June 2013].
• C Programming Classes - Structure in C language [Video online] Available at: <http://www.youtube.com/
watch?v=Y_2DcVnOxPQ> [Accessed 26 June 2013].
• Functions in C Programming [Video online] Available at: <http://www.youtube.com/watch?v=s7ZY1E4smqU>
[Accessed 26 June 2013].
Recommended Reading
• Nair, 2009. Data Structures In C, PHI Learning Pvt. Ltd.
• Puntambekar, A. A., 2008. Data Structures With C, Technical Publications.
• Reddy, R. & Ziegler, C., 2009. C Programming for Scientists and Engineers with Applications, Jones & Bartlett
Learning.
52
Self Assessment
1. When we call function by passing normal values or variables, the function call is called __________.
a. call by reference
b. call by value
c. call by variable
d. reference
53
Programming in ‘C’ and Data Structure
9. Which function receives the actual arguments that we pass in its definition as formal parameters?
a. Called function
b. Call by reference
c. Call by value
d. Global function
10. Which functions are declared above the main ( ) as they are accessible throughout the program?
a. Local function
b. Static function
c. Static internal function
d. Global function
54
Chapter VI
Pointers
Aim
The aim of this chapter is to:
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of this chapter, you will be able to:
• define pointers
55
Programming in ‘C’ and Data Structure
6.1 Introduction
• Pointer is a variable that stores a reference to another variable. It points a memory location in which data is
stored. In the computer, each memory cell has an address which can be used to access that location. Thus, the
pointer variable points a memory location so that the user can access and make required changes in the content
of the memory location using pointers.
• The pointers are one of the powerful tools of ‘C’ programming. They are very efficient. There are many advantages
of pointers as they save memory space and process the data very fast.
• Instead of referring values, we can refer to the addresses at which they are stored.
• We are aware that the memory consists of millions of cells. Each cell has got its own address. We can access
these locations by either using variables or memory addresses. These addresses are nothing but the pointers.
• Asterisk (*) tells the compiler that you are creating pointer variable.
• The declaration tell the compiler to perform the following jobs:
reserve space in memory to hold the integer value
associate the name ‘i’ with this memory location, if ‘int i = 3’ is considered
store the value 3 at this location
• The concept of reference must not be confused with other values namely; identifiers or keys that uniquely identify
the data item, but give access to it only through a non-trivial lookup operation in some table data structure.
• The references are widely used in ‘C’ programming; especially pass large or mutable data as ‘arguments’ to
‘procedures’ or to share data among various uses.
• A reference may point to a variable or record that contains references to other data.
• This idea is the basis of indirect addressing and of many linked data structures such as, linked lists.
• References increase flexibility where the objects are stored, how they are allocated and how they are passed
between areas of code.
56
• We can access the data as long as we can access a reference to the data and the data itself need not be moved.
It also makes sharing of different codes easier, each keeps a reference to it.
int x = 0;
int *pointer_to_x = &x;
(*pointer_to_x) =+ 1;
//x is now equal to 1
The above code increments the value ‘x’ by using the indirection operator and a pointer to the variable ‘x’.
• In ‘C’ language, the dereferencing operator can be used in compositions where multiple acts of dereferencing
are used.
• Pointer can refer other pointers and in such cases multiple applications of the dereference operator are
needed.
• The dereference operator on a pointer only works if the pointer has a pointee. The pointee must be allocated
and the pointer must be set to point to it.
• The most common error in pointer code is forgetting to set up the pointee.
• The common runtime crash because of the error in the code is a failed dereferences operation.
Example,
y = *p1 ** p2;
sum = sum + * p1;
z = 5 * - * p2/p1;
*p2 = *p2 + 10;
• ‘C’ language supports five arithmetic operators on pointers. Those are given below:
Increment (++)
Decrement (++)
Addition(+)
Subtraction (-)
Differencing
57
Programming in ‘C’ and Data Structure
6.5.4 Differencing
Subtraction of two pointers is called as differencing.
Example,
#include<srdio.h>
#include<conio.h>
Void main ( )
{
int i [ ] = {4,5,21,7}, *pointer, j;
clrscr( );
pointer = & i;
for (j = 1;j<=4;j++)
{
printf (“%d elelment of array=%d/n”,j,*pointer);
pointer++;
}
for(j=4,j≥1,j--)
{
pointer --;
printf(“%d element of array=d/n”,j,*pointer);\
}
getch( )
}
• The usage of pointers in a function definition is classified into two types. Those are:
Call by value
Call by reference
58
6.6.1 Call by Value
When a function supports, a link is established between the formal and actual parameters. The value of actual
parameters is stored in the temporary storage that is created.
Here, ‘foo’ is a pointer to a function taking one argument and an integer that returns void.
Here, the key is to read inside-out; notice that the innermost element of the expression is ‘*foo’, otherwise it looks
like a normal function declaration.
#include <studio.h>
void my_int_func (int x)
{
printf ( “%d\n”, x);
}
int main ( )
{
void (*foo) (int);
/* the ampersand is actually optional */
foo = &my_int_func;
return 0;
}
59
Programming in ‘C’ and Data Structure
#include <stdio.h>
void my_int_func (int x)
{
printf (“%d\n”, x);
}
int main ( )
{
void (*foo) (int);
foo = &my_int_func;
/* call my_int_func
foo (2);
/* but if you want to, you may */
return 0;
}
main( )
{
int a[100];
/* A program to display the contents of array using pointer*/
int i,j,n;
printf (“\nEnter the elements of the array\n”);
scanf (“%d”, &n);
printf (“Enter the array elements”);
for (I =0; I < n; I++)
scanf (“%d”, &a[I] );
printf (“Array elements are”)
for (ptr = a, ptr< (a+n) ; ptr ++)
printf (“Value of a[%d] = %d stored at address %u’, j+=, ptr);
}
60
6.11 Array of Pointers
Array of pointers is formed by declaring its capacity with it.
Array of pointer can be declared as below,
Data type *arr-name[size] ;
Example,
char name [3] = { “Nasik”,
“Mumbai”,
“Pune”
}
This declaration will create array of pointer of exact required size. We can access these elements to print.
struct struct-name
{
variables
-
-
-
};
struct *ptr ;
int main ( )
{
int nvar = 2;
int* pvar = &nvar;
func (pvar)
std: : cout<<*pvar<<std: : end1; //will still show 2
return 0;
}
61
Programming in ‘C’ and Data Structure
//function prototype
void func (int++ ppInt);
int main ( )
{
int nvar = 2
int* pvar = &nvar;
func ( &pvar);
.....
return 0;
}
62
Summary
• Pointer is a variable that stores a reference to another variable.
• Pointer points a memory location in which data is stored.
• Reference is a value that enables a program to access a particular data item indirectly such as a variable or a
record in the computer memory or in some other storage device.
• Deference is a unary operator and is also known as indirect operator denoted by asterisk (*) sign.
• Pointer variables can be used in expressions. These are known as pointer arithmetic.
• Increment, decrement, addition, subtraction and differencing are the five pointer arithmetic supported by ‘C’.
• A function pointer is a variable that stores the address of a function that can later be called through that function
pointer.
• When a function supports, a link is established between the formal and actual parameters which is known as
call by value.
• The process where a calling function passes the address of the variables using pointers is known as call by
reference.
• When the “pass by pointer” is used to pass a pointer to a function, only a copy of the pointer is passed to the
function.
References
• Kenneth, R., 2007. Pointers On C, Pearson Education India.
• Schultz, W. T., 2004. C And The 8051, 3rd ed. PageFree Publishing, Inc.
• Jensen, T., 2003. A TUTORIAL ON POINTERS AND ARRAYS IN C [Pdf] Available at: <http://pdos.csail.mit.
edu/6.828/2012/readings/pointers.pdf> [Accessed 26 June 2013].
• Lou, Y., 2011. Introduction to C Pointers and Arrays [Pdf] Available at: <http://www.cs.cornell.edu/courses/
CS2022/2011sp/lectures/lect04.pdf> [Accessed 26 June 2013].
• Pointers in c [Video online] Available at: <http://www.youtube.com/watch?v=w2pbty8tLKU> [Accessed 26
June 2013].
• C Programming Tutorial # 21 - Concept of Pointers - Part 1 [HD] [Video online] Available at: <http://www.
youtube.com/watch?v=rjo9rfRPQU4> [Accessed 26 June 2013].
Recommended Reading
• Pandey, M. H., 2009. Trouble Free C, Laxmi Publications.
• Loudon, K., 2009. Mastering Algorithms with C, O'Reilly Media, Inc.
• Parthasarthy, S., 2008. Essentials Of Programming In C For Life Sciences, Ane Books Pvt Ltd.
63
Programming in ‘C’ and Data Structure
Self Assessment
1. ________ is a variable that stores a reference to another variable.
a. Array
b. Pointer
c. Function
d. Constants
2. __________ tells the compiler that you are crating pointer variable.
a. Asterisk (*)
b. Ampersand (&)
c. Backslash (\)
d. Hash (#)
3. __________ is a value that enables a program to indirectly access a particular data item such as a variable or a
record in the computer memory or in some other storage device.
a. Call by reference
b. Call by value
c. Reference
d. Pointer
4. The __________ is equivalent to the address of its first element, whereas a pointer is equivalent to the address
of the first element that it points to.
a. identifier of a pointer
b. identifier of an array
c. identifier of a constant
d. array
64
8. What is it called when a function supports and a link is established between the formal and actual
parameters?
a. Call by reference
b. Pointer
c. Call by value
d. Pass by pointer
65
Programming in ‘C’ and Data Structure
Chapter VII
Introduction to Data Structure
Aim
The aim of this chapter is to:
Objectives
The objectives of this chapter are to:
Learning outcome
At the end of this chapter, you will be able to:
• understand the concept of linear and nonlinear data structure with java code
66
7.1 Introduction
Data structure can be defined as the collection of elements and all the possible operations which are required for
the set of elements. The operation includes inserting, deleting, searching and printing an element. It is the way of
representing logical relationship between individual data elements.
7.4.1 Array
Array is a consecutive group of memory locations which all have the same name and are of identical type.
67
Programming in ‘C’ and Data Structure
Grades 0 Grades 5
Example:
Float Grades[] = new float[7];
• Grades is an array of type float with size 7.
• Grades[0], Grades[1], …, Grades[6] are the elements of the Grades; each is of type float.
• 0, 1, 2,…,6 are the indices of the array. Also called subscripts. (Note that the indices start at 0 and NOT 1)
• During array declaration we may also put the brackets before the variable name:
• i.e., float [ ]Grades = new float[7];
Initialising arrays
Arrays may be initialised in the following ways:
int n[] = { 2, 4, 6, 8, 10 };
• Creates and initialises a five element array with specified values
Note:
• If data type is a non primitive type then above expression would create and initialise a 10 element array of
nulls.
• You cannot assign data to arrays like:
List = {1, 2, 3, 4, 5}; Wrong!
• Array elements are indexed between zero and the size of the array minus one.
• Arrays can have any type.
• You can check the size of your array by calling the length member variable.
68
The above example will print out 5 to the terminal
Array parameters
One can pass arrays into functions as part of the function parameter like any other variable.
69
Programming in ‘C’ and Data Structure
Inserting elements
Adding an element to an array/list at an arbitrary position without overwriting the previous values, requires that you
move all elements "below" that position.
Algorithm
Insert (List [], position, element, ArraySize)
• Start at the top element of the array.
• Traverse the array backwards so as not to overwrite any previous data.
• Replace the current element that we are on with the element before it.
• Stop once we have reached our insertion position in the array.
• Insert our data into that position.
Sample Pseudo-code
Multidimensional array
• Arrays can be more than one dimensional.
• Used to represent tables of data, etc.
• Declaration of 2-d array: int grid [][] = new int[5][6];
• This declaration is interpreted as an array consisting of 5 rows and 6 columns
• Another way of declaring a 2-d array is given below.
• Same as declaring a 2 x 2 array except we are doing it one row at a time here.
70
Example for 3-d array:
int space = new int[100][100][100];
If we were to print out the values of these arrays by row we would get:
Array 1 Array 2 Array3
123 000 12
456 00 4
Note: If there are not initialisers for a given row, primitive types are initialised to zero and non primitive types are
initialised to null.
Same as declaring a one dimensional array as parameters except that we must remember to include extra square
brackets for each additional dimension.
Types of lists
• General: Data can be inserted and deleted anywhere in the list
Unordered or random data
Ordered data: data are arranged according to a key
• Restricted: Data can be inserted or deleted at the ends of the list
LIFO (stack)
FIFO (queue)
• Four basic operations associated with linear lists are as follows:
Insertion
Deletion
Retrieval
Traversal
71
Programming in ‘C’ and Data Structure
72
Linked list using java
7.4.3 Stack
• A stack is a linear list in which all additions and deletions are restricted to one end called top. It is a Last In
First Out (LIFO) data structure.
• Basic stack operations include:
Push: adds an item at the top of the stack.
Pop: removes the item at the top of the stack and return it to the user.
Stack top: reads the stack top and returns the data to the user.
• Stack-linked list implementation is discussed below.
Usually implemented with a pointer to a stack head structure stored in dynamic memory.
73
Programming in ‘C’ and Data Structure
• Head node contains metadata about the stack such as count and a pointer to the top of the stack.
• Data node looks like a typical linked list data node.
Stack algorithms
• Create stack
• Push stack
• Pop stack
• Stack top
• Empty stack
• Full stack
• Stack count
• Destroy stack
Stack applications
Four common stack applications are:
• Reversing data - A given set of data is recorded so that the first and last elements are exchanged, with all of the
positions between the first and the last elements are relatively exchanged also.
• Parsing - Any logic that breaks down the data into independent pieces for further processing.
• Postponement - The usage of data can be deferred until some later point.
• Backtracking - Making decisions between two or more paths. Stacks are also useful for implementing recursive
algorithms.
7.4.4 Queues
A queue is a linear list in which data can be inserted at one end (called the rear), and deleted from the other end
(called the front). It is a First In First Out (FIFO) data structure.
74
Fig. 7.7 Queue
Basic queue operations
The various queue operations are as follows:
• Enqueue - Inserts an element at the rear of the queue.
• Dequeue - Deletes an element at the front of the queue.
• Queue front - Examines the element at the front of the queue.
• Queue rear - Examines the element at the rear of the queue.
Queue algorithms
• Create queue
• Enqueue
• Dequeue
• Queue front
• Queue rear
• Empty queue
75
Programming in ‘C’ and Data Structure
• Full queue
• Queue count
• Destroy queue
7.5.1 Tree
A tree is discussed in detail below:
76
• Nodes with no children are leaves or terminal nodes.
77
Programming in ‘C’ and Data Structure
Root
Vertex Vertex
Leaf Leaf
Leaf Leaf
78
7.5.1.7 Example of Trees
79
Programming in ‘C’ and Data Structure
Solution:
One way of representing A node of a binary tree in Java as follows:
class BTreeNode
{
int data = 0;
BTreeNode left = null;
BTreeNode right = null;
}
Data : 0
We may then build our binary tree using this node structure
80
Question: How to construct a BST in java?
Answer: The most intuitive way of creating BST/adding nodes to a BST is to use recursive approach.
• Postorder traversal
Do postorder traversal of left subtree.
Do postorder traversal of right subtree.
Visit the node last and process, i.e., visits and processes each node in the tree AFTER visiting and processing
its children.
81
Programming in ‘C’ and Data Structure
• Inorder traversal
Do inorder traversal of left subtree.
Visit the node and process.
Do inorder traversal of right subtree, i.e., processes nodes in the tree in an ascending sorted order.
Preorder
Postorder
82
Summary
• The data structure can be defined as the collection of element and all the possible operations which are required
for those set of elements.
• The operation includes inserting, deleting, searching and printing an element. It is the way of representing logical
relationship between individual data elements.
• The data structure can be divided into two types, namely Linear and Nonlinear data structures.
• Traversal: Travel through the data structure.
• Search: Traversal through the data structure for a given element.
• Insertion: Adding new elements to the data structure.
• Deletion: Removing an element from the data structure.
• Sorting: Arranging the elements in some type of order.
• Merging: Combining two similar data structures into one.
• A consecutive group of memory locations that all have the same name and of identical type.
• Usual way to traverse a linear 1-d array is to use a loop. For example, getting the overall average grade.
• Adding an element to an array/list at an arbitrary position without overwriting the previous values requires that
you move all elements "below" that position.
• A linear list is a list in which each element has a unique successor.
• Types of lists are General and Restricted.
• Four basic operations associated with linear lists are Insertion, Deletion, Retrieval and Traversal.
References
• Storer, A. J., 2002. An Introduction to Data Structures and Algorithms, Springer.
• Baitipuli, N. V., 2009. Introduction to Data Structures Using C, Laxmi Publications, Ltd.
• INTRODUCTION TO DATA STRUCTURE [Pdf] Available at: <http://www.csbdu.in/econtent/DataStructures/
Unit1-DS.pdf> [Accessed 26 June 2013].
• Introduction to Data Structures Using C [Pdf] Available at: <http://vijayacollege.ac.in/Content/PDF/ds_notes.
pdf> [Accessed 26 June 2013].
• Lecture - 1 Introduction to Data Structures and Algorithms [Video online] Available at: <http://www.youtube.
com/watch?v=zWg7U0OEAoE> [Accessed 26 June 2013].
• Dr. Chakraborty, P. P., 2010. Lec-1 Introduction [Video online] Available at: <http://www.youtube.com/
watch?v=S47aSEqm_0I> [Accessed 26 June 2013].
Recommended Reading
• Kamthane, N. A., 2012. Introduction to Data Structures in C, Pearson Education India.
• Group, I., 2006. Data Structures Using C, Tata McGraw-Hill Education.
• Puntambekar, A. A., 2008. Advanced Data Structures And Algorithms, Technical Publications.
83
Programming in ‘C’ and Data Structure
Self Assessment
1. The ___________can be defined as the collection of element and all the possible operations which are required
for those set of elements.
a. data structure
b. molecular structure
c. chemical structure
d. code structure
84
8. __________ is adding new elements to the data structure.
a. Sorting
b. Insertion
c. Deletion
d. Merging
10. ______________ is a consecutive group of memory locations that all have the same name and of identical
type.
a. Array
b. Pointer
c. Data
d. Element
85
Programming in ‘C’ and Data Structure
Application I
CPython
CPython is an implementation of the Python programming language. It is the most-widely used, default
implementation. This language is written in C. Besides CPython, Jython and IronPython are two other production
quality implementations of the Python. Jython is written in Java whereas, IronPython is written for C#. All the three
implementations of Python mentioned above have several experimental implementations. CPython is a bytecode
interpreter. The term Bytecode is used to denote various forms of instruction sets, which are designed to ensure
efficient execution of the instruction by a software interpreter and also to enhance the suitability of the instruction for
further compilation into the machine code. An interpreter executes the instructions written in a computer program.
It can perform the following roles:
• execute the source code directly
• translate source code into intermediate code and execute the same immediately
• explicitly execute the stored precompiled code made by a compiler
CPYthon has a foreign function interface with C and several other languages. A foreign function interface is also
referred as FFI, which is a mechanism by which a program written in one programming language is capable of
calling routines and using the services written in another programming language. In C, it is necessary to explicitly
write bindings in a language other than Python. A binding from a programming language to a library or OS service
is referred as an API providing that service in the language.
Desktop OSes
• AROS
• AtheOS
• BeOS
• Windows
• Windows NT
• OS/2
• RISC OS
86
Special and embedded
• GP2X
• iPodLinux
• Nintendo DS
• Nintendo Gamecube
• Symbian OS Series60
• Nokia 770 Internet Tablet
• Nokia N800
• Nokia N810
• Palm OS
• PlayStation 2
• PlayStation 3 (Linux)
• Psion
• QNX
• Sharp Zaurus
• Xbox/XBMC
• VxWorks
• Openmoko
External ports
These are ports not integrated to Python Software Foundation's official version of CPython, with links to its main
development site. Ports often include additional modules for platform-specific functionalities, like graphics and
sound API for PSP and SMS and camera API for S60.
• Amiga: AmigaPython
• AS/400: iSeriesPython
• DOS using DJGPP: PythonD
• PlayStation Portable: Stackless Python for PSP
• Symbian OS: Python for S60
• Windows CE/Pocket PC: Python Windows CE port
87
Programming in ‘C’ and Data Structure
Questions
1. What is CPython and how is it linked to C?
Answers
CPython is an implementation of the Python programming language, which is most widely used. It is written
in C programming language.
2. Name three supported platforms of CPython separately based on Unix-like, Desktop OSes and Special Emebedded
platforms.
Answers
Supported Platforms of:
• Unix-like are – BSD, Darwin, Linux;
• Desktop OSes are – Windows NT, AROS, OS/2
• Special and embedded are – iPodLinux, Palm OS, QNX
88
Application II
/* This program reads a weight in pounds from the key board representing it as an integer. It converts the weight to
ounce, and prints it. Program from "Applications Programming in ANSI C".
*/
#include<stdio.h>
main( )
{
int pounds;
printf("\n\n\tWeight in pounds?");
scant("%d",£s);
while(pounds>=0)
{
printf("\n\n\tEquivalent weight in ounces: %d", pounds * 16);
printf("\n\n\tWeight in pounds?");
printf("\n\n\tEnter a negative integer to quit.");
scanf("%d",£s);
}
}
Question
1. Write a program to count blanks, tabs and newlines.
89
Programming in ‘C’ and Data Structure
Application III
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main ( )
{
int gd = DETECT, gm ;
float x1, x2, y1, y2, dx, co, j ;
int flag=0 ;
float distance, B_SPEED, F_SPEED ;
x1=10.0 ;
y1=400.0 ;
x2=20.0 ;
y2=200.0 ;
90
fillellipse (x2, y2, 5, 5) ;
outtextxy (x2=20, y2, ″FIGHTER″) ;
if (distance≤50)
{
flag = 1 ;
for (int i=0; i<15; i++)
{
sound ( i*180) ;
delay (50) ;
}
outtextxy (220, 150, ″Bomber Plane crashed″) ;
nosound ( ) ;
getch ( ) ;
break ;
}
delay (500) ;
}
if (flag = = 0)
{
outtextxy (220, 250, ″Sorry : Bomber Plane has Passed away Safely!″) ;
}
delay(50) ;
}
nosound ( ) ;
getch ( ) ;
}
Question
1. Write a chat program in C.
91
Programming in ‘C’ and Data Structure
Application IV
/* This program counts the number of bytes in a C source file. The program prompts the user for a file name and
then concatenates the ".c" extension to this name. It uses the function get c to read the characters.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main( )
{
FILE* fptr;
char extension[] = ".c";
return EXIT_SUCCESS;
}
Question
1. Write a program that takes three variable (a, b, b) in as separate parameters and rotates the values stored so that
value a goes to be, b, to c and c to a.
92
Bibliography
References
• Ackermann, C. E., 1989. The Essentials of C Programming Language, Research & Education Assoc.
• Ajay, M., 2010. Programming In C: A Practical Approach, Pearson Education India.
• Arrays and Strings [Pdf] Available at: <http://pages.cpsc.ucalgary.ca/~jacob/Courses/Fall00/CPSC231/Slides/10-
ArraysAndStrings.pdf> [Accessed 26 June 2013].
• Ashok, K., 2010. C Programming: Test Your Skills, Pearson Education India.
• Baitipuli, N. V., 2009. Introduction to Data Structures Using C, Laxmi Publications, Ltd.
• Balagurusamy, 2008. Programming In Ansi C, 4th ed. Tata McGraw-Hill Education.
• C Programming Classes - Structure in C language [Video online] Available at: <http://www.youtube.com/
watch?v=Y_2DcVnOxPQ> [Accessed 26 June 2013].
• C Programming Tutorial # 21 - Concept of Pointers - Part 1 [HD] [Video online] Available at: <http://www.
youtube.com/watch?v=rjo9rfRPQU4> [Accessed 26 June 2013].
• C Programming Tutorial 44 - Strings and Characters [Video online] Available at: <http://www.youtube.com/
watch?v=EJBb8nQD-4g> [Accessed 26 June 2013].
• C Programming Tutorial: Arrays (One and Two Dimensional Array ): Lesson 2 [Video online] Available at:
<http://www.youtube.com/watch?v=NFJMjgXb0uA> [Accessed 26 June 2013].
• C Programming Tutorial: Input Output (printf, scanf, format specifiers) Lesson 1 [Video online] Available at:
<http://www.youtube.com/watch?v=5v7h4ZmgMvw> [Accessed 26 June 2013].
• C programming tutorial-5- Dealing with the DATATYPES [Video online] Available at: <http://www.youtube.
com/watch?v=oLjV3vfmZmg> [Accessed 26 June 2013].
• Chapter 2: C Data Types [Pdf] Available at: <http://www.informatik.uni-giessen.de/staff/brass/iii01/c2_cdata.
pdf> [Accessed 26 June 2013].
• Control Statements [Pdf] Available at: <http://download.nos.org/srsec330/330L9.pdf> [Accessed 26 June
2013].
• Control Statements in Programming Languages (final) part1 [Video online] Available at: <http://www.youtube.
com/watch?v=hX0Rrq8ouxY> [Accessed 26 June 2013].
• Control statements in programming languages-part 1-pldc [Video online] Available at: <http://www.youtube.
com/watch?v=ageuh_4_J9E> [Accessed 26 June 2013].
• Data Structures Using C [Pdf] Available at: <http://www.gatesit.org/gitdownloads/C&DS.pdf> [Accessed 26
June 2013].
• Deshpande, P. S. & Kakde, O. G., 2004. C and Data Structures, Cengage Learning.
• Dixit, J. B., 2005. Fundamentals of Computers and Programming in C, Firewall Media.
• Dr. Chakraborty, P. P., 2010. Lec-1 Introduction [Video online] Available at: <http://www.youtube.com/
watch?v=S47aSEqm_0I> [Accessed 26 June 2013].
• Dr. Gupta, P., Aggarwal, V. & Varshney, M., 2007. Data Structures Using ‘C’, Firewall Media.
• Dr. Kumar, A. S., 2008. Lecture - 1 Introduction to programming languages [Video online] Available at: <http://
www.youtube.com/watch?v=EbNJ05EVXs0> [Accessed 26 June 2013].
• Functions in C [Pdf] Available at: <http://www.nada.kth.se/kurser/master/intro/Course03-04/Handouts/
Flecture05.pdf> [Accessed 26 June 2013].
• Functions in C Programming [Video online] Available at: <http://www.youtube.com/watch?v=s7ZY1E4smqU>
[Accessed 26 June 2013].
• Goel, A., Computer programming-I: As per the first-year engineering syllabus of University of Mumbai, Pearson
Education India.
• Hoover, 2009. System Programming With C And Unix, Pearson Education India.
93
Programming in ‘C’ and Data Structure
Recommended Reading
• Balagurusamy, E., Programming In Ansi C, 5E, Tata McGraw-Hill Education.
• Deitel, J. P. & Deitel, M. H., 2009. C: How to Program, 6th ed. Prentice Hall.
• Dixit, J.B., 2006. Comprehensive Programming in C and Numerical Analysis, Laxmi Publications.
• Ghosh, 2004. Programming In C, PHI Learning Pvt. Ltd.
• Ghosh, All Of C, PHI Learning Pvt. Ltd.
• Group, I., 2006. Data Structures Using C, Tata McGraw-Hill Education.
94
• Hanly, 2009. Programming In C And Data Structures (For Jntu), Pearson Education India.
• Kamthane, N. A., 2012. Introduction to Data Structures in C, Pearson Education India.
• Kochan, 2005. Programming In C, 3/E, Pearson Education India.
• Kochan, G. S., 2012. Programming in Objective-C, 4th ed. Addison-Wesley Professional.
• Krishnamoorthy, 2010. DATA STRUCTURES USING C, Tata McGraw-Hill Education.
• Lafore, 2002. Object Oriented Programming In C++, 4/E, Pearson Education India.
• Loudon, K., 2009. Mastering Algorithms with C, O’Reilly Media, Inc.
• Malik, D. S., 2009. C++ Programming: Program Design Including Data Structures: Program Design Including
Data Structures, 4th ed. Cengage Learning.
• Malik, D. S., 2012. C++ Programming: From Problem Analysis to Program Design, 6th ed.: From Problem
Analysis to Program Design, 6th ed. Cengage Learning.
• Nair, 2009. Data Structures In C, PHI Learning Pvt. Ltd.
• Pandey, M. H., 2009. Trouble Free C, Laxmi Publications.
• Parthasarthy, S., 2008. Essentials Of Programming In C For Life Sciences, Ane Books Pvt Ltd.
• Puntambekar, A. A., 2008. Advanced Data Structures And Algorithms, Technical Publications.
• Puntambekar, A. A., 2008. Data Structures With C, Technical Publications.
• Rajaraman, V., 1994. COMPUTER PROGRAMMING IN C, PHI Learning Pvt. Ltd.
• Reddy, R. & Ziegler, C., 2009. C Programming for Scientists and Engineers with Applications, Jones & Bartlett
Learning.
95
Programming in ‘C’ and Data Structure
Chapter II
1. c
2. d
3. a
4. a
5. d
6. b
7. a
8. d
9. b
10. a
Chapter III
1. d
2. c
3. a
4. b
5. d
6. b
7. c
8. a
9. d
10. c
Chapter IV
1. b
2. c
3. d
4. a
5. c
6. b
7. d
8. c
9. b
10. a
96
Chapter V
1. b
2. a
3. c
4. c
5. d
6. c
7. a
8. b
9. a
10. d
Chapter VI
1. b
2. a
3. c
4. b
5. d
6. a
7. c
8. a
9. b
10. a
Chapter VII
1. a
2. d
3. c
4. a
5. c
6. d
7. b
8. b
9. a
10. a
97