SlideShare a Scribd company logo
1 of 57
Download to read offline
Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: Programming In C
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
Subject: Programming In C
Topic: Unit II- Part I
Arrays
@Dr. Arpana Chaturvedi
Topics to be Covered
▰ Arrays
▰ Types of Arrays
▰ One Dimensional Array manipulation
▰ Searching an element in an Array
▰ Insertion of a element in an Array
▰ Deletion of an element from an Array
▰ Finding the largest/smallest element in an Array
Introduction to Arrays in C
@Dr. Arpana Chaturvedi
▰ An array is defined as the collection of similar type of data items stored at
contiguous memory locations.
▰ Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
▰ It also has the capability to store the collection of derived data types, such as
pointers, structure, etc.
▰ The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
▰ It is a best practice to initialize an array to zero or null while declaring, if we don’t
assign any values to array.
TYPES OF C ARRAYS:
▰ There are 2 types of C arrays. They are,
▰ One dimensional array
▰ Multi dimensional array: Two dimensional array,Three dimensional array etc.
Need of an Array in C
@Dr. Arpana Chaturvedi
In Case you need to store 100 different Integer Values to store Score of a Candidate,
you might declare and process it in the below mentioned way
Need of an Array in C
@Dr. Arpana Chaturvedi
Instead of declaring individual variables, such as score0, score1, ..., and score99, you
declare one array variable such as score and use score[0], score[1], and ..., score[99]
to represent individual variables. A specific element in an array is accessed by an
index.
Processing Data in an Array
@Dr. Arpana Chaturvedi
Declaration of an Arrays in C
@Dr. Arpana Chaturvedi
Initialization of an Array in C
@Dr. Arpana Chaturvedi
Only fixed-length arrays can be initialized when they are defined. Variable
length arrays must be initialized by inputting or assigning the values.
Example of an Array in C
@Dr. Arpana Chaturvedi
/*C Program to explain Use of Array,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input tvariable and an array
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
clrscr();
printf("ntExample of Array Initialization, Declaration and
Accessn");
printf("n
t**********************************************************n");
//Accept Inputs of Different data Types
/* initialize elements of array n to 0 */
printf("nt The Initialization of Elelment in an
Array")
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i +
100 */
}
/* output each array element's value */
Output of an Arrays in C
@Dr. Arpana Chaturvedi
Exchanging the Array elements: Wrong Way
@Dr. Arpana Chaturvedi
One array cannot be copied to another using assignment.
Exchanging the values of Element: Right Way
@Dr. Arpana Chaturvedi
Example to Swap Adjacent Element in an Array
in C
@Dr. Arpana Chaturvedi
/*C program to swap adjacent elements of an one dimensional
array.,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
//Declaration of Required Input tvariable and an array
int arr[MAX],n,i;
int temp;
clrscr();
printf("ntExample of Swapping Adjacent Elements of an
Array");
printf("n
t*************************************************");
//prompt the user to enter size of an array
printf("nt Enter total number of elementst:t ");
scanf("%d",&n);
//value of n must be even
if(n%2 !=0)
{
printf("Total number of elements should be EVEN.");
return;
Output: Example to Swap Adjacent Element in
an Array in C
@Dr. Arpana Chaturvedi
Searching an Element in C
@Dr. Arpana Chaturvedi
▰ Searching is the process of finding a given value position in a list of values.
▰ Searching means that given a value, we want to find the location (index) of the first
element in the array that contains that value.
▰ It decides whether a search key is present in the data or not.
▰ Searching Techniques
▰ To search an element in a given array, it can be done in following ways:
1. Sequential Search
2. Binary Search
Searching an Element in C
@Dr. Arpana Chaturvedi
Searching Data in Unsorted Array
@Dr. Arpana Chaturvedi
Unsuccessful Search in Unordered
Array in C
@Dr. Arpana Chaturvedi
Sequential Search or Linear Search
▰ Sequential search is also called as Linear Search.
▰ Sequential search starts at the beginning of the list and checks every element of
the list.
▰ It is a basic and simple search algorithm.
▰ Sequential search compares the element with all the other elements given in the
list. If the element is matched, it returns the value index, else it returns -1.
▰ The above figure shows how sequential search works. It searches an element or
value from an array till the desired element or value is not found. If we search the
element 25, it will go step by step in a sequence order. It searches in a sequence
order. Sequential search is applied on the unsorted or unordered list when there
are fewer elements in a list.
@Dr. Arpana Chaturvedi
Sequential Search in an Array in C
@Dr. Arpana Chaturvedi
Example of Sequential Search
@Dr. Arpana Chaturvedi
/*C Program to explain Linear Search
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output variables
int arr[50], search, cnt, num;
clrscr();
printf("ntExample of Linear Search Of an Element");
printf("nt*************************************n");
printf(" ntEnter the number of elements in array t:t");
scanf("%d",&num);
printf("nt Enter %d integer(s)in an Arrayn", num);
for (cnt = 0; cnt < num; cnt++)
{
printf("nt Element at arr[%d] t=t ",cnt);
scanf("%d", &arr[cnt]);
}
printf(" ntEnter the number to searcht:t");
scanf("%d", &search);
Output of Linear Search
@Dr. Arpana Chaturvedi
Binary Search in C
@Dr. Arpana Chaturvedi
▰ Binary Search is used for searching an element in a sorted array.
▰ It is a fast search algorithm with run-time complexity of O(log n).
▰ Binary search works on the principle of divide and conquer.
▰ This searching technique looks for a particular element by comparing the middle
most element of the collection.
▰ It is useful when there are large number of elements in an array.
▰ The above array is sorted in ascending order. As we know binary search is applied
on sorted lists only for fast searching.
Working of Binary Search in C
@Dr. Arpana Chaturvedi
For example, if searching an element 25 in the 7-element array, following figure
shows how binary search works:
Binary searching starts with
middle element. If the
element is equal to the
element that we are
searching then return true. If
the element is less than then
move to the right of the list
or if the element is greater
than then move to the left of
the list. Repeat this, till you
find an element.
Binary Search in C
@Dr. Arpana Chaturvedi
Unsuccessful Binary Search in C
@Dr. Arpana Chaturvedi
Example of Binary Search in C
@Dr. Arpana Chaturvedi
/*C Program to explain Binary Search
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output
variables
int f, l, m, size, i, sElement, list[50]; //int f,
l ,m : First, Last, Middle
clrscr();
printf("ntExample of Binary Search Of an
Element");
printf("nt*************************************
n");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ",
size);
for (i = 0; i < size; i++)
{
Output of Binary Search
@Dr. Arpana Chaturvedi
Insertion of an element in an Array C
@Dr. Arpana Chaturvedi
Input the array elements, the position of the new element to be inserted and the new
element.
Insert the new element at that position and shift the rest of the elements to right by
one position.
Insertion of an element in an Array in C
@Dr. Arpana Chaturvedi
/* C program to insert an element in an array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define N 100
void main()
{
//Declaration of Required Input and Output variables
int size;
int arr[N];
int i;
int pos;
int ele;
clrscr();
printf("ntExample of Insertion Of an Element in an Array");
printf("nt**********************************************");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (i = 0; i < size; i++)
{
printf("nt Element at arr[%d] t=t ",i);
Ouput after Insertion in an Array in C
@Dr. Arpana Chaturvedi
Deletion of an Element from an Array in
C
@Dr. Arpana Chaturvedi
Input the array elements, the position of the new element to be inserted and the new
element.
Delete the element and shift the rest of the elements to left by one position.
Example of Deletion of an Element from
an Array in C
@Dr. Arpana Chaturvedi
/* C program to delete an element in an array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define N 100
void main()
{
//Declaration of Required Input and Output variables
int arr[N], pos, c, n,size;
clrscr();
printf("ntExample of Deletion Of an Element in an Array");
printf("nt**********************************************");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (c = 0; c < size; c++)
{
printf("nt Element at arr[%d] t=t ",c);
scanf("%d",&arr[c]);
}
printf("nt Enter the position from which element is to be
Output of Deletion Example
@Dr. Arpana Chaturvedi
Finding the largest/smallest element
in an Array
@Dr. Arpana Chaturvedi
Program to find the smallest and largest elements
in an array is discussed here. Given an array, the
task is to find the largest and smallest elements of
the array.
Method 1: Traverse the array iteratively and keep
track of the smallest and largest element until the
end of the array.
Method 2: Traverse the array recursively and keep
track of the smallest and largest element until the
end of the array.
▰ For example, consider the array.
▰ arr = {1, 2, 3, 4, 5}
▰ Smallest element : 1
▰ Largest element : 5
Algorithm to find the smallest
and largest numbers in an
array
• Input the array elements.
• Initialize small = large =
arr[0]
• Repeat from i = 2 to n
• if(arr[i] > large)
• large = arr[i]
• if(arr[i] < small)
• small = arr[i]
• Print small and large.
Program to Find Largest and Smallest
Element from an Array
@Dr. Arpana Chaturvedi
/* C program to find the smallest and largest element in an
array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output variables
int arr[50],i,large,small,size;
clrscr();
printf("ntExample of Searching Smallest and Largest
Element in an Array");
printf("n
t*************************************************************
");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (i = 0; i < size; i++)
{
printf("nt Element at arr[%d] t=t ",i);
Output to show smallest and Largest
Element of an Array
@Dr. Arpana Chaturvedi
Properties of an Array
@Dr. Arpana Chaturvedi
The array contains the following properties.
▰ Each element of an array is of same data type and carries the same size, i.e., int =
4 bytes.
▰ Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
▰ Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
Advantages-Disadvantages of an Array in C
@Dr. Arpana Chaturvedi
Advantages:
1) Code Optimization: Less code to
the access the data.
2) Ease of traversing: By using the
for loop, we can retrieve the elements
of an array easily.
3) Ease of sorting: To sort the
elements of the array, we need a few
lines of code only.
4) Random Access: We can access
any element randomly using the array.
Disadvantages:
1) Fixed Size: Whatever size, we
define at the time of declaration of
the array, we can't exceed the
limit. So, it doesn't grow the size
dynamically like Linked List
Storage Classes
@Dr. Arpana Chaturvedi
Storage Classes in C
Storage class specifiers in C language tells the compiler where to store a variable,
how to store the variable, what is the initial value of the variable and life time of the
variable.
SYNTAX:
▰ storage_specifier data_type variable _name;
TYPES OF STORAGE CLASS SPECIFIERS IN C:
▰ There are 4 storage class specifiers available in C language. They are,
▰ auto
▰ extern
▰ static
▰ register
@Dr. Arpana Chaturvedi
Types of Storage Specifiers in C
@Dr. Arpana Chaturvedi
Storage Specifier Description
auto
Storage place: CPU Memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
extern
Storage place: CPU memory
Initial/default value: Zero
Scope: Global
Life: Till the end of the main program. Variable definition might be
anywhere in the C program.
static
Storage place: CPU memory
Initial/default value: Zero
Scope: local
Life: Retains the value of the variable between different function calls.
register
Storage place: Register memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
Key Points to use Storage Specifiers in C
▰ For faster access of a variable, it is better to go for register specifiers rather than
auto specifiers.
▰ Because, register variables are stored in register memory whereas auto variables
are stored in main CPU memory.
▰ Only few variables can be stored in register memory. So, we can use variables as
register that are used very often in a C program.
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR AUTO
VARIABLE IN C:
The scope of this auto variable is within the function only. It is
equivalent to local variable. All local variables are auto variables
by default.
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR AUTO
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Auto Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void increment(void);
void main()
{
//Declaration of Required Input to accept input using
unformatted getch function
int c;
char str[100];
clrscr();
printf("ntExample of Auto Variables");
printf("nt*************************n");
increment();
increment();
increment();
increment();
printf("nnttMade By Dr. Arpana");
getch();
}
OUTPUT OF EXAMPLE PROGRAM FOR
AUTO VARIABLE IN C:
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR STATIC
VARIABLE IN C:
▰ Static variables retain the value of the variable between
different function calls.
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR STATIC
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Static Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void increment(void);
void main()
{
clrscr();
printf("ntExample of Static Variablesn");
printf("nt***************************n");
increment();
increment();
increment();
increment();
printf("nnttMade By Dr. Arpana");
getch();
}
void increment(void)
{
static int i = 0 ;
printf ( "nt The Value of Static variable is %d ", i ) ;
OUTPUT OF EXAMPLE PROGRAM FOR
STATIC VARIABLE IN C:
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR EXTERN
VARIABLE IN C:
▰ The scope of this extern variable is throughout the main
program. It is equivalent to global variable. Definition for
extern variable might be anywhere in the C program
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR EXTERN
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain External Storage Variable,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
//Declared and initialised Variable externally or global
int x = 10 ;
void main()
{
//Declaration of Required Input to accept input using
unformatted getch function
extern int y;
clrscr();
printf("ntExample of External Storage Variablen");
printf("nt*************************************n");
//Display the value accessed of External variables
printf("nt The value of x is %d n",x);
printf("nt The value of y is %d",y);
printf("nnttMade By Dr. Arpana");
getch();
}
int y=50;
//Declared and initialised Variable externally or Outside
OUTPUT OF EXAMPLE PROGRAM FOR
EXTERN VARIABLE IN C:
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR REGISTER
VARIABLE IN C:
▰ Register variables are also local variables, but stored in
register memory. Whereas, auto variables are stored in main
CPU memory.
▰ Register variables will be accessed very faster than the
normal variables since they are stored in register memory
rather than main memory.
▰ But, only limited variables can be used as register since
register size is very low. (16 bits, 32 bits or 64 bits)
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR REGISTER
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Register Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Variables
register int i;
int arr[5];// declaring array
clrscr();
printf("ntExample of Register Variablesn");
printf("nt*****************************n");
arr[0] = 10;// Initializing array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
// Accessing each variable
printf("nt The value of arr[%d] using Register variables
is %d n", i, arr[i]);
}
OUTPUT OF EXAMPLE PROGRAM FOR
REGISTER VARIABLE IN C:
@Dr. Arpana Chaturvedi
Thank You !!

More Related Content

What's hot

Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
Compiler and symbol table
Compiler and symbol tableCompiler and symbol table
Compiler and symbol tableSunjid Hasan
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabusJK Knowledge
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1sumitbardhan
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER ModelAjit Nayak
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File ProcessingRanel Padon
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python BasicsRaghunath A
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of CSuchit Patel
 

What's hot (20)

Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Compiler and symbol table
Compiler and symbol tableCompiler and symbol table
Compiler and symbol table
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER Model
 
Unit3 cspc
Unit3 cspcUnit3 cspc
Unit3 cspc
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Clanguage
ClanguageClanguage
Clanguage
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 
C presentation
C presentationC presentation
C presentation
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 

Similar to Arrays Fundamentals Unit II

Similar to Arrays Fundamentals Unit II (20)

VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays
ArraysArrays
Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays
ArraysArrays
Arrays
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array assignment
Array assignmentArray assignment
Array assignment
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 

More from Arpana Awasthi

Unit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfUnit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfArpana Awasthi
 
Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdfArpana Awasthi
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfArpana Awasthi
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfArpana Awasthi
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette Arpana Awasthi
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsArpana Awasthi
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part IArpana Awasthi
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerArpana Awasthi
 

More from Arpana Awasthi (10)

Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Unit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdfUnit 2 Part 1.2 Data Types.pdf
Unit 2 Part 1.2 Data Types.pdf
 
Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdf
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
Programming language
Programming languageProgramming language
Programming language
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancer
 

Recently uploaded

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Arrays Fundamentals Unit II

  • 1. Jagannath Institute of Management Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: Programming In C Department of Information Technology Created By: Dr. Arpana Chaturvedi @Dr. Arpana Chaturvedi
  • 2. Subject: Programming In C Topic: Unit II- Part I Arrays @Dr. Arpana Chaturvedi
  • 3. Topics to be Covered ▰ Arrays ▰ Types of Arrays ▰ One Dimensional Array manipulation ▰ Searching an element in an Array ▰ Insertion of a element in an Array ▰ Deletion of an element from an Array ▰ Finding the largest/smallest element in an Array
  • 4. Introduction to Arrays in C @Dr. Arpana Chaturvedi ▰ An array is defined as the collection of similar type of data items stored at contiguous memory locations. ▰ Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. ▰ It also has the capability to store the collection of derived data types, such as pointers, structure, etc. ▰ The array is the simplest data structure where each data element can be randomly accessed by using its index number. ▰ It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any values to array. TYPES OF C ARRAYS: ▰ There are 2 types of C arrays. They are, ▰ One dimensional array ▰ Multi dimensional array: Two dimensional array,Three dimensional array etc.
  • 5. Need of an Array in C @Dr. Arpana Chaturvedi In Case you need to store 100 different Integer Values to store Score of a Candidate, you might declare and process it in the below mentioned way
  • 6. Need of an Array in C @Dr. Arpana Chaturvedi Instead of declaring individual variables, such as score0, score1, ..., and score99, you declare one array variable such as score and use score[0], score[1], and ..., score[99] to represent individual variables. A specific element in an array is accessed by an index.
  • 7. Processing Data in an Array @Dr. Arpana Chaturvedi
  • 8. Declaration of an Arrays in C @Dr. Arpana Chaturvedi
  • 9. Initialization of an Array in C @Dr. Arpana Chaturvedi Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning the values.
  • 10. Example of an Array in C @Dr. Arpana Chaturvedi /*C Program to explain Use of Array, Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void main() { //Declaration of Required Input tvariable and an array int n[ 10 ]; /* n is an array of 10 integers */ int i,j; clrscr(); printf("ntExample of Array Initialization, Declaration and Accessn"); printf("n t**********************************************************n"); //Accept Inputs of Different data Types /* initialize elements of array n to 0 */ printf("nt The Initialization of Elelment in an Array") for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */
  • 11. Output of an Arrays in C @Dr. Arpana Chaturvedi
  • 12. Exchanging the Array elements: Wrong Way @Dr. Arpana Chaturvedi One array cannot be copied to another using assignment.
  • 13. Exchanging the values of Element: Right Way @Dr. Arpana Chaturvedi
  • 14. Example to Swap Adjacent Element in an Array in C @Dr. Arpana Chaturvedi /*C program to swap adjacent elements of an one dimensional array., Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> #define MAX 100 void main() { //Declaration of Required Input tvariable and an array int arr[MAX],n,i; int temp; clrscr(); printf("ntExample of Swapping Adjacent Elements of an Array"); printf("n t*************************************************"); //prompt the user to enter size of an array printf("nt Enter total number of elementst:t "); scanf("%d",&n); //value of n must be even if(n%2 !=0) { printf("Total number of elements should be EVEN."); return;
  • 15. Output: Example to Swap Adjacent Element in an Array in C @Dr. Arpana Chaturvedi
  • 16. Searching an Element in C @Dr. Arpana Chaturvedi ▰ Searching is the process of finding a given value position in a list of values. ▰ Searching means that given a value, we want to find the location (index) of the first element in the array that contains that value. ▰ It decides whether a search key is present in the data or not. ▰ Searching Techniques ▰ To search an element in a given array, it can be done in following ways: 1. Sequential Search 2. Binary Search
  • 17. Searching an Element in C @Dr. Arpana Chaturvedi
  • 18. Searching Data in Unsorted Array @Dr. Arpana Chaturvedi
  • 19. Unsuccessful Search in Unordered Array in C @Dr. Arpana Chaturvedi
  • 20. Sequential Search or Linear Search ▰ Sequential search is also called as Linear Search. ▰ Sequential search starts at the beginning of the list and checks every element of the list. ▰ It is a basic and simple search algorithm. ▰ Sequential search compares the element with all the other elements given in the list. If the element is matched, it returns the value index, else it returns -1. ▰ The above figure shows how sequential search works. It searches an element or value from an array till the desired element or value is not found. If we search the element 25, it will go step by step in a sequence order. It searches in a sequence order. Sequential search is applied on the unsorted or unordered list when there are fewer elements in a list. @Dr. Arpana Chaturvedi
  • 21. Sequential Search in an Array in C @Dr. Arpana Chaturvedi
  • 22. Example of Sequential Search @Dr. Arpana Chaturvedi /*C Program to explain Linear Search Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void main() { //Declaration of Required Input and Output variables int arr[50], search, cnt, num; clrscr(); printf("ntExample of Linear Search Of an Element"); printf("nt*************************************n"); printf(" ntEnter the number of elements in array t:t"); scanf("%d",&num); printf("nt Enter %d integer(s)in an Arrayn", num); for (cnt = 0; cnt < num; cnt++) { printf("nt Element at arr[%d] t=t ",cnt); scanf("%d", &arr[cnt]); } printf(" ntEnter the number to searcht:t"); scanf("%d", &search);
  • 23. Output of Linear Search @Dr. Arpana Chaturvedi
  • 24. Binary Search in C @Dr. Arpana Chaturvedi ▰ Binary Search is used for searching an element in a sorted array. ▰ It is a fast search algorithm with run-time complexity of O(log n). ▰ Binary search works on the principle of divide and conquer. ▰ This searching technique looks for a particular element by comparing the middle most element of the collection. ▰ It is useful when there are large number of elements in an array. ▰ The above array is sorted in ascending order. As we know binary search is applied on sorted lists only for fast searching.
  • 25. Working of Binary Search in C @Dr. Arpana Chaturvedi For example, if searching an element 25 in the 7-element array, following figure shows how binary search works: Binary searching starts with middle element. If the element is equal to the element that we are searching then return true. If the element is less than then move to the right of the list or if the element is greater than then move to the left of the list. Repeat this, till you find an element.
  • 26. Binary Search in C @Dr. Arpana Chaturvedi
  • 27. Unsuccessful Binary Search in C @Dr. Arpana Chaturvedi
  • 28. Example of Binary Search in C @Dr. Arpana Chaturvedi /*C Program to explain Binary Search Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void main() { //Declaration of Required Input and Output variables int f, l, m, size, i, sElement, list[50]; //int f, l ,m : First, Last, Middle clrscr(); printf("ntExample of Binary Search Of an Element"); printf("nt************************************* n"); printf("nt Enter the size of the listt:t "); scanf("%d",&size); printf("nt Enter %d integer values t:t ", size); for (i = 0; i < size; i++) {
  • 29. Output of Binary Search @Dr. Arpana Chaturvedi
  • 30. Insertion of an element in an Array C @Dr. Arpana Chaturvedi Input the array elements, the position of the new element to be inserted and the new element. Insert the new element at that position and shift the rest of the elements to right by one position.
  • 31. Insertion of an element in an Array in C @Dr. Arpana Chaturvedi /* C program to insert an element in an array Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> #define N 100 void main() { //Declaration of Required Input and Output variables int size; int arr[N]; int i; int pos; int ele; clrscr(); printf("ntExample of Insertion Of an Element in an Array"); printf("nt**********************************************"); printf("nt Enter the size of the listt:t "); scanf("%d",&size); printf("nt Enter %d integer values t:t ", size); for (i = 0; i < size; i++) { printf("nt Element at arr[%d] t=t ",i);
  • 32. Ouput after Insertion in an Array in C @Dr. Arpana Chaturvedi
  • 33. Deletion of an Element from an Array in C @Dr. Arpana Chaturvedi Input the array elements, the position of the new element to be inserted and the new element. Delete the element and shift the rest of the elements to left by one position.
  • 34. Example of Deletion of an Element from an Array in C @Dr. Arpana Chaturvedi /* C program to delete an element in an array Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> #define N 100 void main() { //Declaration of Required Input and Output variables int arr[N], pos, c, n,size; clrscr(); printf("ntExample of Deletion Of an Element in an Array"); printf("nt**********************************************"); printf("nt Enter the size of the listt:t "); scanf("%d",&size); printf("nt Enter %d integer values t:t ", size); for (c = 0; c < size; c++) { printf("nt Element at arr[%d] t=t ",c); scanf("%d",&arr[c]); } printf("nt Enter the position from which element is to be
  • 35. Output of Deletion Example @Dr. Arpana Chaturvedi
  • 36. Finding the largest/smallest element in an Array @Dr. Arpana Chaturvedi Program to find the smallest and largest elements in an array is discussed here. Given an array, the task is to find the largest and smallest elements of the array. Method 1: Traverse the array iteratively and keep track of the smallest and largest element until the end of the array. Method 2: Traverse the array recursively and keep track of the smallest and largest element until the end of the array. ▰ For example, consider the array. ▰ arr = {1, 2, 3, 4, 5} ▰ Smallest element : 1 ▰ Largest element : 5 Algorithm to find the smallest and largest numbers in an array • Input the array elements. • Initialize small = large = arr[0] • Repeat from i = 2 to n • if(arr[i] > large) • large = arr[i] • if(arr[i] < small) • small = arr[i] • Print small and large.
  • 37. Program to Find Largest and Smallest Element from an Array @Dr. Arpana Chaturvedi /* C program to find the smallest and largest element in an array Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void main() { //Declaration of Required Input and Output variables int arr[50],i,large,small,size; clrscr(); printf("ntExample of Searching Smallest and Largest Element in an Array"); printf("n t************************************************************* "); printf("nt Enter the size of the listt:t "); scanf("%d",&size); printf("nt Enter %d integer values t:t ", size); for (i = 0; i < size; i++) { printf("nt Element at arr[%d] t=t ",i);
  • 38. Output to show smallest and Largest Element of an Array @Dr. Arpana Chaturvedi
  • 39. Properties of an Array @Dr. Arpana Chaturvedi The array contains the following properties. ▰ Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. ▰ Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. ▰ Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
  • 40. Advantages-Disadvantages of an Array in C @Dr. Arpana Chaturvedi Advantages: 1) Code Optimization: Less code to the access the data. 2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting: To sort the elements of the array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array. Disadvantages: 1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like Linked List
  • 42. Storage Classes in C Storage class specifiers in C language tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable. SYNTAX: ▰ storage_specifier data_type variable _name; TYPES OF STORAGE CLASS SPECIFIERS IN C: ▰ There are 4 storage class specifiers available in C language. They are, ▰ auto ▰ extern ▰ static ▰ register @Dr. Arpana Chaturvedi
  • 43. Types of Storage Specifiers in C @Dr. Arpana Chaturvedi Storage Specifier Description auto Storage place: CPU Memory Initial/default value: Garbage value Scope: local Life: Within the function only. extern Storage place: CPU memory Initial/default value: Zero Scope: Global Life: Till the end of the main program. Variable definition might be anywhere in the C program. static Storage place: CPU memory Initial/default value: Zero Scope: local Life: Retains the value of the variable between different function calls. register Storage place: Register memory Initial/default value: Garbage value Scope: local Life: Within the function only.
  • 44. Key Points to use Storage Specifiers in C ▰ For faster access of a variable, it is better to go for register specifiers rather than auto specifiers. ▰ Because, register variables are stored in register memory whereas auto variables are stored in main CPU memory. ▰ Only few variables can be stored in register memory. So, we can use variables as register that are used very often in a C program. @Dr. Arpana Chaturvedi
  • 45. EXAMPLE PROGRAM FOR AUTO VARIABLE IN C: The scope of this auto variable is within the function only. It is equivalent to local variable. All local variables are auto variables by default. @Dr. Arpana Chaturvedi
  • 46. EXAMPLE PROGRAM FOR AUTO VARIABLE IN C: @Dr. Arpana Chaturvedi /*C Program to explain Auto Variables Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void increment(void); void main() { //Declaration of Required Input to accept input using unformatted getch function int c; char str[100]; clrscr(); printf("ntExample of Auto Variables"); printf("nt*************************n"); increment(); increment(); increment(); increment(); printf("nnttMade By Dr. Arpana"); getch(); }
  • 47. OUTPUT OF EXAMPLE PROGRAM FOR AUTO VARIABLE IN C: @Dr. Arpana Chaturvedi
  • 48. EXAMPLE PROGRAM FOR STATIC VARIABLE IN C: ▰ Static variables retain the value of the variable between different function calls. @Dr. Arpana Chaturvedi
  • 49. EXAMPLE PROGRAM FOR STATIC VARIABLE IN C: @Dr. Arpana Chaturvedi /*C Program to explain Static Variables Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void increment(void); void main() { clrscr(); printf("ntExample of Static Variablesn"); printf("nt***************************n"); increment(); increment(); increment(); increment(); printf("nnttMade By Dr. Arpana"); getch(); } void increment(void) { static int i = 0 ; printf ( "nt The Value of Static variable is %d ", i ) ;
  • 50. OUTPUT OF EXAMPLE PROGRAM FOR STATIC VARIABLE IN C: @Dr. Arpana Chaturvedi
  • 51. EXAMPLE PROGRAM FOR EXTERN VARIABLE IN C: ▰ The scope of this extern variable is throughout the main program. It is equivalent to global variable. Definition for extern variable might be anywhere in the C program @Dr. Arpana Chaturvedi
  • 52. EXAMPLE PROGRAM FOR EXTERN VARIABLE IN C: @Dr. Arpana Chaturvedi /*C Program to explain External Storage Variable, Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> //Declared and initialised Variable externally or global int x = 10 ; void main() { //Declaration of Required Input to accept input using unformatted getch function extern int y; clrscr(); printf("ntExample of External Storage Variablen"); printf("nt*************************************n"); //Display the value accessed of External variables printf("nt The value of x is %d n",x); printf("nt The value of y is %d",y); printf("nnttMade By Dr. Arpana"); getch(); } int y=50; //Declared and initialised Variable externally or Outside
  • 53. OUTPUT OF EXAMPLE PROGRAM FOR EXTERN VARIABLE IN C: @Dr. Arpana Chaturvedi
  • 54. EXAMPLE PROGRAM FOR REGISTER VARIABLE IN C: ▰ Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory. ▰ Register variables will be accessed very faster than the normal variables since they are stored in register memory rather than main memory. ▰ But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits) @Dr. Arpana Chaturvedi
  • 55. EXAMPLE PROGRAM FOR REGISTER VARIABLE IN C: @Dr. Arpana Chaturvedi /*C Program to explain Register Variables Made by Dr. Arpana on 11-07-20 */ #include<stdio.h> #include<conio.h> void main() { //Declaration of Variables register int i; int arr[5];// declaring array clrscr(); printf("ntExample of Register Variablesn"); printf("nt*****************************n"); arr[0] = 10;// Initializing array arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; for (i=0;i<5;i++) { // Accessing each variable printf("nt The value of arr[%d] using Register variables is %d n", i, arr[i]); }
  • 56. OUTPUT OF EXAMPLE PROGRAM FOR REGISTER VARIABLE IN C: @Dr. Arpana Chaturvedi