Vtu 1st Sem Programming in C and Data Structures Notes 14pcd13
Vtu 1st Sem Programming in C and Data Structures Notes 14pcd13
Code: 14PCD13/23
Hrs/Week: 04
Total Hrs: 52
IA Marks: 25
Exam Hrs: 03
Exam Marks:100
Objectives:
The objectives of this course is to make students to learn basic principles of Problem solving, implementing
through C programming language and to design & develop programming skills.
MODULE I
INTRODUCTION TO C LANGUAGE: Pseudocode solution to problem, Basic concepts of a C program,
Declaration, Assignment & Print statement, Types of operators and expressions, Programming examples and
exercise.
Text 1: Chapter 2
Text 2: 1.1, 1.2, 1.3
10 Hours
MODULE II
BRANCHING AND LOOPING: Two way selection (if, if-else, nested if-else, cascaded if-else), switch statement,
ternary operator? Go to, Loops (For, do-while, while) in C, break and continue, programming examples and
exercises.
Text 1: Chapter 3
Text 2: 4.4
10 Hours
MODULE II
BRANCHING AND LOOPING: Two way selection (if, if-else, nested if-else, cascaded if-else), switch statement,
ternary operator? Go to, Loops (For, do-while, while) in C, break and continue, programming examples and
exercises.
Text 1: Chapter 3
Text 2: 4.4
10 Hours
MODULE III
ARRAYS, STRINGS AND FUNCTIONS:
ARRAYS AND STRINGS: Using an array, Using arrays with Functions, Multi-Dimensional arrays. String:
Declaring, Initializing, Printing and reading strings, strings manipulation functions, strings input and output
functions, arrays of strings, programming examples and Exercises.
Text 1: 5.7
Text 2: 7.3, 7.4 Chapter 9
FUNCTIONS: Functions in C, Argument Passing call by value, Functions and program structure, location of
functions, void and parameter less Functions, Recursion, programming examples and exercises.
Text 1: 1.7, 1.8, Chapter 4
Text 2: 5.1 to 5.4
10 Hours
MODULE IV
STRUCTURES AND FILE MANAGEMENT: Basic of structures, structures and Functions, Arrays of structures,
structure Data types, type definition, Defining, opening and closing of files, Input and output operations,
programming examples and exercises.
Text 1: 6.1 to 6.3
Text 2: 10.1 to 10.4, Chapter 11
10 Hours
MODULE V
POINTERS AND PREPROCESSORS: Pointers and address, pointers and functions arguments, pointers and
arrays, address arithmetic, character pointer and functions, pointers to pointer ,Initialization of pointers arrays,
Dynamic allocations methods, Introduction to Preprocessors, Complier control Directives, programming examples
and exercises.
Text 1: 5.1 to 5.6, 5.8
Text 2: 12.2, 12.3, 13.1 to 13.7
Introduction to Data Structures: Primitive and non primitive data types, Definition and applications of Stacks,
Queues, Linked Lists and Trees.
Text 2: 14.1, 14.2, 14.11, 14.12, 14.13, 14.15, 14.16, 14.17, 15.1
08 Hours + 04 Hours
TEXT BOOK:
1. Brain W. Kernighan and Dennis M. Richie: The C programming Language, 2nd Edition, PHI, 2012.
2. Jacqueline Jones & Keith Harrow: Problem Solving with C, 1st Edition, Pearson 2011.
TABLE OF CONTENTS
MODULE I: INTRODUCTION TO C LANGUAGE
1-19
20-34
35-48
FUNCTIONS
49-57
58-64
65-76
77-89
WORKED EXAMPLES
BASIC C PROGRAMS
90-93
94-97
98-106
107-116
117-120
121-124
1
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
2
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
3
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
4
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
5
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
6
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
7
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
UNFORMATTED I/O
getchar() and putchar()
getchar() is used to
read a character from the keyboard and
store this character into a memory-location
You have to press ENTER key after typing a character.
The syntax is shown below:
char variable_name = getchar( );
For ex:
char z;
z= getchar( );
putchar() is used to display a character stored in the memory-location on the screen.
#include<stdio.h>
main()
{
char x;
char y=n;
printf(enter one letter terminated by ENTER key \n);
x = getchar();
putchar(y);
// same as printf(%c, z);
}
Output:
enter one letter terminated by ENTER key
m
n
8
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
9
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
10
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
For Example
+-*/%
++ -=
< > ==
&& || ~
?:
& | ^
[]
11
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
12
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
13
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
14
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
15
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
16
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
IMPLICIT CONVERSION
If a compiler converts one type of data into another type of data automatically, it is known as implicit
conversions.
There is no data loss in implicit conversion.
The conversion always takes place from lower rank to higher rank.
For ex, int to float as shown in the above datatype hierarchy.
For ex:
int a = 22, b=11;
float c = a; //c becomes 21.000000
float d=b/c=11/22.000000=11.000000/22.000000=0.500000
If one operand type is same as other operand type, no conversion takes place and type of result
remains same as the operands i.e. int+int=int
float+float=float
Conversion rules are as follows:
If either operand is long double, convert the other to long double.
Otherwise, if either operand is double, convert the other to double.
Otherwise, if either operand is float, convert the other to float.
Otherwise, convert char and short to int.
Then, if either operand is long, convert the other to long.
Example: Program to illustrate implicit conversion.
#include<stdio.h>
void main()
{
int a = 22, b=11;
float d ;
d=b/c;
printf("d Value is : %f ", d );
}
Output:
d Value is : 0.500000
17
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
18
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
19
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
20
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
4.
5.
6.
7.
Now, question is how to write programming code to handle such situation. C language provides
conditional i.e., decision making statements which work based on the following flow diagram:
21
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
22
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
23
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
24
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
25
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
26
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
27
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
28
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
29
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
30
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
to
to
to
to
to
C
C
C
C
C
language
language
language
language
language
31
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
32
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
Example: Program to read and add only positive numbers using continue statement.
#include<stdio.h>
void main()
{
int i=1, num, sum =0;
for(i=0; i < 5; i ++)
{
printf( Enter an integer:);
scanf( %d, &num);
if(num < 0)
{
printf(you have entered a negative number \n);
continue ;
// skip the remaining part of loop
}
sum=sum+num;
}
printf(The sum of the Positive Integers Entered = %d, sum);
}
Output:
Enter an integer: 10
Enter an integer:-15
You have entered a negative number
Enter an integer: 15
Enter an integer: -100
You have entered a negative number
Enter an integer: 30
The sum of the positive integers entered = 55
33
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
Example: Program to detect the entered number as to whether it is even or odd. Use goto statement.
#include<stdio.h>
void main()
{
int x;
printf(Enter a Number: \n);
scanf(%d, &x);
if(x % 2 = = 0)
goto even;
else
goto odd;
even: printf(%d is Even Number);
return;
odd: printf( %d is Odd Number);
}
Output:
Enter a Number:
5
5 is Odd Number.
34
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
=
=
=
=
=
10;
20;
30;
40;
50;
"number1:
"number2:
"number3:
"number4:
"number5:
%d
%d
%d
%d
%d
\n", number1);
\n", number2);
\n", number3);
\n", number4);
", number5);
}
Output:
number1: 10
number2: 20
number3: 30
number4: 40
number5: 50
It was simple, because we had to store just 5 integer numbers. Now let's assume we have to store
5000 integer numbers, so what is next? Are we going to use 5000 variables?
To handle such situation, C language provides a concept called the array.
Some examples where arrays can be used are
1) List of temperatures recorded every hour in a day, or a month, or a year
2) List of employees in an organization
3) List of products and their cost sold by a store
4) Test scores of a class of students
ARRAY
Array is a collection of elements of same data type.
The elements are stored sequentially one after the other in memory.
Any element can be accessed by using
name of the array
position of element in the array
Arrays are of 2 types:
1) Single dimensional array
2) Multi dimensional array
35
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
36
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
array
array
array
array
array
in
in
in
in
in
age[0]
age[1]
age[2]
age[3]
age[4]
array
array
array
array
array
age[0]
age[1]
age[2]
age[3]
age[4]
:
:
:
:
:
%d
%d
%d
%d
%d
\n", age[0]);
\n", age[1]);
\n", age[2]);
\n", age[3]);
",age[4]);
:2
:4
:34
:3
:4
37
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
at
at
at
at
at
position
position
position
position
position
0.
1.
2.
3.
4.
printf("enter 5 numbers:\n);
for(i=0;i<5;i++)
{
scanf("%d", &age[i]);
}
for(i=0;i<5;i++)
{
printf("Value in array age[%d] : %d \n ", i, age[i]);
}
}
Output:
enter 5 numbers:
2 4 34 3 4
Value in array age[0]
Value in array age[1]
Value in array age[2]
Value in array age[3]
Value in array age[4]
:2
:4
:34
:3
:4
38
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
A two dimensional array is used when elements are arranged in a tabular fashion.
Here, to identify a particular element, we have to specify 2 indices:
First index identifies the row number of the element and
Second index identifies the column number of the element
Declaration of Two Dimensional Arrays
The syntax is shown below:
data_type array_name[array_size][array_size];
For ex:
int matrix[2][3];
The above code can be pictorially represented as shown below:
39
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
row=0
row=0
row=0
row=1
row=1
row=1
col=0.
col=1.
col=2.
col=0.
col=1.
col=2.
40
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
41
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
STRING VARIABLE
There is no separate data type for strings in C.
They are treated as arrays of type char.
So, a variable which is used to store an array of characters is called a string variable.
Declaration of String
Strings are declared in C in similar manner as arrays.
Only difference is that, strings are of char type.
For ex:
char s[5]; //string variable name can hold maximum of 5 characters including NULL character
The above code can be pictorially represented as shown below:
Initialization of String
For ex:
char s[5]={'r', 'a', 'm', 'a' };
char s[9]="rama";
The above code can be pictorially represented as shown below:
42
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
43
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
strlen()
This function calculates the length of string. It takes only one argument, i.e., string-name.
The syntax is shown below:
temp_variable = strlen(string_name);
44
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
45
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
46
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
47
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
48
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
TYPES OF C FUNCTIONS
There are 2 types of functions in C programming:
Library function
User defined function
Library Function
Library functions are the in-built function in C compiler.
For example:
main()
//The execution of every C program starts from this main() function
printf() //prinf() is used for displaying output in C
scanf() //scanf() is used for taking input in C
User Defined Function
C allows programmer to define their own function according to their requirement. These types of
functions are known as user-defined functions.
Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in
same program. Then, he can create two separate user-defined functions in that program:
one for finding factorial and
other for checking whether it is prime or not
ADVANTAGES OF USER DEFINED FUNCTIONS
1) User defined functions helps to decompose the large program into small segments which makes
programmer easy to understand, maintain and debug.
2) If repeated code occurs in a program. Function can be used to include those codes and execute
when needed by calling that function.
3) Programmer working on large project can divide the workload by making different functions.
49
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
//function declaration
// function call
//function definition
As mentioned earlier, every C program begins from main() and program starts executing the codes
inside main() function.
When the control of program reaches to function_name() inside main() function, the control of
program jumps to void function_name() and executes the codes inside it.
When all the codes inside that user-defined function are executed, control of the program jumps to
the statement just after function_name() from where it is called.
Function Declaration
Every function in C program should be declared before they are used.
Function declaration gives compiler information about
function name
type of arguments to be passed and
return type
The syntax is shown below:
return_type function_name(type(1) argument(1),....,type(n) argument(n));
Function Call
Control of the program cannot be transferred to user-defined function unless it is called
invoked.
The syntax is shown below:
function_name(argument(1),....argument(n));
Function Definition
Function definition contains programming codes to perform specific task.
The syntax is shown below:
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}
Example: Program to Print a sentence using function.
#include<stdio.h>
void display();
//function declaration
void main()
{
display();
}
void display()
{ printf("C Programming");
return;
}
Output:
C Programming
//function call
//function definition
50
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
51
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
52
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
53
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
54
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
void main()
{
int num, sum;
printf("Enter a positive integer: ");
scanf("%d",&num);
sum=add(num);
printf("sum=%d", sum);
}
Output:
Enter a positive integer: 5
15
Explanation:
Here, add() function is invoked from the same function.
If n is not equal to 0 then, the function calls itself passing argument 1 less than the previous
argument it was called with.
Suppose, n is 5 initially. Then, during next function calls, 4 is passed to function and the value of
argument decreases by 1 in each recursive call.
When, n becomes equal to 0, the value of n is returned which is the sum numbers from 5 to 1.
Observe the following for better visualization of recursion in this example:
add(5)
=5+ add(4)
=5+4+ add(3)
=5+4+3+ add(2)
=5+4+3+2+ add(1)
=5+4+3+2+1+ add(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In this example when, n
is equal to 0, there is no recursive call and recursion ends.
55
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
56
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
57
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
58
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
59
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
60
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
61
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
62
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
63
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
64
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
65
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
66
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
The name of the array always points to the first element of an array.
Here, address of first element of an array is &arr[0].
Also, arr represents the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to
arr.
Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0]
and value in address arr is *arr. Hence, arr[0] is equivalent to *arr.
Similarly,
&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
.
.
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).
You can declare an array and can use pointer to alter the data of an array.
Example: Program to access elements of an array using pointer.
#include<stdio.h>
void main()
{
int data[5], i;
printf("Enter elements: ");
for(i=0;i<5;++i)
scanf("%d", data+i);
printf("You entered: ");
for(i=0;i<5;++i)
printf("%d ",*(data+i) );
}
Output:
Enter elements: 1 2 3 5 4
You entered: 1 2 3 5 4
Example: Program to find sum of all the elements of an array using pointers.
#include<stdio.h>
void main()
{
int i, a[10], n, sum=0;
printf("Enter the size of the array:");
scanf("%d", &n);
printf("Enter the elements into the array: ");
for(i=0;i<n;i++)
scanf("%d ",&a[i]);
for(i=0;i<n;i++)
sum+=*(a+i);
printf("Sum --> %d ",sum);
}
Output:
Enter the size of the array:5
Enter the elements into the array: 2 4 6 10 15
Sum --> 37
67
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
68
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
69
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name.
For example, following is the declaration to declare a pointer to a pointer of type int:
int **var;
When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires
that the asterisk operator be applied twice, as is shown below in the example:
#include<stdio.h>
void main()
{
int var;
int *ptr;
int **pptr;
var = 3000;
// take the address of var
ptr = &var;
// take the address of ptr using address of operator &
pptr = &ptr;
// take the value using pptr
printf("Value of var = %d \n", var );
printf("Value available at *ptr = %d \n", *ptr );
printf("Value available at **pptr = %d ", **pptr);
return 0;
}
Output:
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
70
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
71
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
72
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
73
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
74
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
75
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
76
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
77
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
APPLICATIONS OF STACK
1) Conversion of expressions: The compiler converts the infix expressions into postfix expressions
using stack.
2) Evaluation of expression: An arithmetic expression represented in the form of either postfix or prefix
can be easily evaluated using stack.
3) Recursion: A function which calls itself is called recursive function.
4) Other applications: To find whether the string is a palindrome, to check whether a given expression
is valid or not.
78
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
if (top==(MAXSIZE-1))
{
printf ("Error: Overflow ");
}
else
{
printf ("Enter the element to be pushed \n");
scanf ("%d", &num);
top = top + 1;
stack[top] = num;
}
}
void pop()
//Function to delete an element from the stack
{
int num;
if (top==-1)
{
printf ("Error: Stack Empty\n");
}
else
{
num = stack[top];
printf ("popped element is = %d \n", num);
top=top-1;
}
}
void display()
//Function to display the status of the stack
{
int i;
if (top == -1)
{
printf ("Error: Stack Empty");
}
else
{
printf ("\n Items in Stack \n");
for (i = top; i >= 0; i--)
{
printf ("%d \n", stack[i]);
}
}
}
79
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
80
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
81
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
82
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
83
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
84
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
85
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
86
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
87
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
88
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
89
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
WORKED EXAMPLES
BASIC C PROGRAMS
PROGRAM TO PRINT A SENTENCE.
#include <stdio.h>
void main()
{
printf("C Programming"); //displays the content inside quotation
return;
}
Output:
C Programming
PROGRAM TO READ AND DISPLAY AN INTEGER.
#include <stdio.h>
void main()
{
int num;
printf("Enter a integer: ");
scanf("%d",&num); // Storing a integer entered by user in variable num
printf("You entered: %d", num);
return;
}
Output:
Enter a integer: 25
You entered: 25
PROGRAM TO ADD TWO INTEGERS
#include <stdio.h>
void main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); // Stores the two integer entered by user in
// variable num1 and num2
sum=num1+num2;
// Performs addition and stores it in variable sum
printf("Sum: %d",sum); // Displays sum
return;
}
Output:
Enter two integers: 12
11
Sum: 23
90
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
91
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
: ");
92
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
93
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
94
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
95
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
96
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
97
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
98
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
99
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
100
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
101
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
102
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
103
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
104
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
105
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
106
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
107
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
108
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
109
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
110
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
111
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
112
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
113
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
114
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
115
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
116
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
117
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
118
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
119
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
120
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
121
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
122
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
123
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/
124
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit http://victory4sure.weebly.com/