SlideShare una empresa de Scribd logo
1 de 9
Arrays in C
When we work with a large number of data values we need that any number of different variables. As
the number of variables increases, the complexity of the program also increases and so the
programmers get confused with the variable names. There may be situations where we need to work
with a large number of similar data values. To make this work easier, C programming language
provides a concept called "Array".
An array is a special type of variable used to store multiple values of same data type at a
time.
An array can also be defined as follows...
An array is a collection of similar data items stored in continuous memory locations with
single name.
Declaration of an Array
In C programming language, when we want to create an array we must know the datatype of values
to be stored in that array and also the number of values to be stored in that array.
We use the following general syntax to create an array...
datatype arrayName [ size ] ;
Syntax for creating an array with size and initial values
datatype arrayName [ size ] = {value1, value2, ...} ;
Syntax for creating an array without size and with initial values
datatype arrayName [ ] = {value1, value2, ...} ;
In the above syntax, the datatype specifies the type of values we store in that array and size specifies
the maximum number of values that can be stored in that array.
Example Code
int a [3] ;
Here, the compiler allocates 6 bytes of contiguous memory locations with a single name 'a' and tells
the compiler to store three different integer values (each in 2 bytes of memory) into that 6 bytes of
memory. For the above declaration, the memory is organized as follows...
In the above memory allocation, allthe three memory locationshave a common name 'a'.So accessing
individual memory location is not possible directly. Hence compiler not only allocates the memory but
also assigns a numerical reference value to every individual memory location of an array. This
reference number is called "Index" or "subscript" or "indices". Index values for the above example are
as follows...
Accessing Individual Elements of an Array
The individual elements of an array are identified using the combination of 'arrayName' and
'indexValue'. We use the following general syntax to access individual elements of an array...
arrayName [ indexValue ] ;
For the above example the individual elements can be denoted as follows...
For example, if we want to assign a value to the second memory location of above array 'a', we use
the following statement...
Example Code
a [1] = 100 ;
The result of the above assignment statement is as follows...
Applications of Arrays in C
In c programming language, arrays are used in wide range of applications. Few of them are as
follows...
● Arrays are used to Store List of values
In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array data is stored in linear form.
● Arrays are used to Perform Matrix Operations
We use two dimensional arraysto create matrix. We can perform variousoperationson matrices using
two dimensional arrays.
● Arrays are used to implement Search Algorithms
We use single dimensional arrays to implement search algorihtms like ...
1. Linear Search
2. Binary Search
● Arrays are used to implement Sorting Algorithms
We use single dimensional arrays to implement sorting algorihtms like ...
1. Insertion Sort
2. Bubble Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort, etc.,
● Arrays are used to implement Datastructures
We use single dimensional arrays to implement datastructures like...
1. Stack Using Arrays
2. Queue Using Arrays
● Arrays are also used to implement CPU Scheduling
Algorithms
Strings in C
String is a set of characters that are enclosed in double quotes. In the C programming language,
strings are created using one dimension array of character datatype. Every string in C programming
language is enclosed within double quotes and it is terminated with NULL (0) character. Whenever c
compiler encounters a string value it automatically appends a NULL character (0) at the end. The
formal definition of string is as follows...
String is a set of characters enclosed in double quotation marks. In C programming, the
string is a character array of single dimension.
In C programming language, there are two methods to create strings and they are as follows...
1. Using one dimensional array of character datatype ( static memory allocation )
2. Using a pointer array of character datatype ( dynamic memory allocation )
Creating string in C programming language
In C, strings are created as a one-dimensional array of character datatype. We can use both static
and dynamic memory allocation. When we create a string,the size of the array must be one more than
the actual number of characters to be stored. That extra memory block is used to store string
termination character NULL (0). The following declaration stores a string of size 5 characters.
char str[6] ;
The following declaration creates a string variable of a specific size at the time of program execution.
char *str = (char *) malloc(15) ;
Assigning string value in C programming language
String value is assigned using the following two methods...
1. At the time of declaration (initialization)
2. After declaraation
Examples of assigning string value
int main()
{
char str1[6] = "Hello";
char str2[] = "Hello!";
char name1[] = {'s','m','a','r','t'};
char name2[6] = {'s','m','a','r','t'};
char title[20];
*title = "btech smart class";
return 0;
}
Reading string value from user in C programming
language
We can read a string value from the user during the program execution. We use the following two
methods...
1. Using scanf() method - reads single word
2. Using gets() method - reads a line of text
Using scanf() method we can read only one word of string. We use %s to represent string in scanf()
and printf() methods.
Examples of reading string value using scanf() method
#include<stdio.h>
#include<conio.h>
int main(){
char name[50];
printf("Please enter your name : ");
scanf("%s", name);
printf("Hello! %s , welcome to btech smart class !!", name);
return 0;
}
When we want to read multiple words or a line of text, we use a pre-defined method gets().
The gets() method terminates the reading of text with Enter character.
Examples of reading string value using gets() method
#include<stdio.h>
#include<conio.h>
int main(){
char name[50];
printf("Please enter your name : ");
gets(name);
printf("Hello! %s , welcome to btech smart class !!", name);
return 0;
}
C Programming language provides a set of pre-definied functions called String Handling
Functions to work with string values. All the string handling functions are defined in a header file
called string.h.
String Handling Functions in C
C programming language provides a set of pre-defined functions called string handling functions to
work with string values. The string handling functions are defined in a header file called string.h.
Whenever we want to use any string handling function we must include the header file called string.h.
The following table provides most commonly used string handling function and their use...
Function Syntax (or) Example Description
strcpy() strcpy(string1, string2) Copies string2 value into string1
strncpy() strncpy(string1, string2,
5)
Copies first 5 characters string2 into string1
strlen() strlen(string1) returns total number of characters in string1
strcat() strcat(string1,string2) Appends string2 to string1
strncat() strncpy(string1, string2,
4)
Appends first 4 characters of string2 to string1
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if
string1>string2
strncmp() strncmp(string1, string2,
4)
Compares first 4 characters of both string1 and
string2
strcmpi() strcmpi(string1,string2) Compares two strings, string1 and string2 by
ignoring case (upper or lower)
stricmp() stricmp(string1, string2) Compares two strings, string1 and string2 by
ignoring case (similar to strcmpi())
strlwr() strlwr(string1) Converts all the characters of string1 to lower case.
strupr() strupr(string1) Converts all the characters of string1 to upper case.
strdup() string1 = strdup(string2) Duplicated value of string2 is assigned to string1
Function Syntax (or) Example Description
strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of character
'b' in string1
strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of character
'b' in string1
strstr() strstr(string1, string2) Returns a pointer to the first occurrence of string2 in
string1
strset() strset(string1, 'B') Sets all the characters of string1 to given character
'B'.
strnset() strnset(string1, 'B', 5) Sets first 5 characters of string1 to given character
'B'.
strrev() strrev(string1) It reverses the value of string1

Más contenido relacionado

La actualidad más candente

Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSGautham Rajesh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programmingnmahi96
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserMindbowser Inc
 

La actualidad más candente (20)

C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
String & its application
String & its applicationString & its application
String & its application
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
C# Strings
C# StringsC# Strings
C# Strings
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
String notes
String notesString notes
String notes
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
14 strings
14 strings14 strings
14 strings
 
M C6java7
M C6java7M C6java7
M C6java7
 
Syntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - MindbowserSyntax Comparison of Golang with C and Java - Mindbowser
Syntax Comparison of Golang with C and Java - Mindbowser
 
Java execise
Java execiseJava execise
Java execise
 

Similar a Arrays in C explained with examples

Similar a Arrays in C explained with examples (20)

Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
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
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
 
Strings in c
Strings in cStrings in c
Strings in c
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Array assignment
Array assignmentArray assignment
Array assignment
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Session 4
Session 4Session 4
Session 4
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 

Más de Sowri Rajan

Más de Sowri Rajan (10)

Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 

Último

💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...sonalitrivedi431
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...Call Girls in Nagpur High Profile
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja Nehwal
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxjeswinjees
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneLukeKholes
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...Call Girls in Nagpur High Profile
 

Último (20)

💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 

Arrays in C explained with examples

  • 1. Arrays in C When we work with a large number of data values we need that any number of different variables. As the number of variables increases, the complexity of the program also increases and so the programmers get confused with the variable names. There may be situations where we need to work with a large number of similar data values. To make this work easier, C programming language provides a concept called "Array". An array is a special type of variable used to store multiple values of same data type at a time. An array can also be defined as follows... An array is a collection of similar data items stored in continuous memory locations with single name. Declaration of an Array In C programming language, when we want to create an array we must know the datatype of values to be stored in that array and also the number of values to be stored in that array. We use the following general syntax to create an array... datatype arrayName [ size ] ; Syntax for creating an array with size and initial values datatype arrayName [ size ] = {value1, value2, ...} ; Syntax for creating an array without size and with initial values datatype arrayName [ ] = {value1, value2, ...} ;
  • 2. In the above syntax, the datatype specifies the type of values we store in that array and size specifies the maximum number of values that can be stored in that array. Example Code int a [3] ; Here, the compiler allocates 6 bytes of contiguous memory locations with a single name 'a' and tells the compiler to store three different integer values (each in 2 bytes of memory) into that 6 bytes of memory. For the above declaration, the memory is organized as follows... In the above memory allocation, allthe three memory locationshave a common name 'a'.So accessing individual memory location is not possible directly. Hence compiler not only allocates the memory but also assigns a numerical reference value to every individual memory location of an array. This reference number is called "Index" or "subscript" or "indices". Index values for the above example are as follows... Accessing Individual Elements of an Array The individual elements of an array are identified using the combination of 'arrayName' and 'indexValue'. We use the following general syntax to access individual elements of an array... arrayName [ indexValue ] ;
  • 3. For the above example the individual elements can be denoted as follows... For example, if we want to assign a value to the second memory location of above array 'a', we use the following statement... Example Code a [1] = 100 ; The result of the above assignment statement is as follows... Applications of Arrays in C In c programming language, arrays are used in wide range of applications. Few of them are as follows... ● Arrays are used to Store List of values
  • 4. In c programming language, single dimensional arrays are used to store list of values of same datatype. In other words, single dimensional arrays are used to store a row of values. In single dimensional array data is stored in linear form. ● Arrays are used to Perform Matrix Operations We use two dimensional arraysto create matrix. We can perform variousoperationson matrices using two dimensional arrays. ● Arrays are used to implement Search Algorithms We use single dimensional arrays to implement search algorihtms like ... 1. Linear Search 2. Binary Search ● Arrays are used to implement Sorting Algorithms We use single dimensional arrays to implement sorting algorihtms like ... 1. Insertion Sort 2. Bubble Sort 3. Selection Sort 4. Quick Sort 5. Merge Sort, etc., ● Arrays are used to implement Datastructures We use single dimensional arrays to implement datastructures like... 1. Stack Using Arrays 2. Queue Using Arrays
  • 5. ● Arrays are also used to implement CPU Scheduling Algorithms Strings in C String is a set of characters that are enclosed in double quotes. In the C programming language, strings are created using one dimension array of character datatype. Every string in C programming language is enclosed within double quotes and it is terminated with NULL (0) character. Whenever c compiler encounters a string value it automatically appends a NULL character (0) at the end. The formal definition of string is as follows... String is a set of characters enclosed in double quotation marks. In C programming, the string is a character array of single dimension. In C programming language, there are two methods to create strings and they are as follows... 1. Using one dimensional array of character datatype ( static memory allocation ) 2. Using a pointer array of character datatype ( dynamic memory allocation ) Creating string in C programming language In C, strings are created as a one-dimensional array of character datatype. We can use both static and dynamic memory allocation. When we create a string,the size of the array must be one more than the actual number of characters to be stored. That extra memory block is used to store string termination character NULL (0). The following declaration stores a string of size 5 characters. char str[6] ; The following declaration creates a string variable of a specific size at the time of program execution. char *str = (char *) malloc(15) ; Assigning string value in C programming language String value is assigned using the following two methods... 1. At the time of declaration (initialization) 2. After declaraation Examples of assigning string value
  • 6. int main() { char str1[6] = "Hello"; char str2[] = "Hello!"; char name1[] = {'s','m','a','r','t'}; char name2[6] = {'s','m','a','r','t'}; char title[20]; *title = "btech smart class"; return 0; } Reading string value from user in C programming language We can read a string value from the user during the program execution. We use the following two methods... 1. Using scanf() method - reads single word 2. Using gets() method - reads a line of text Using scanf() method we can read only one word of string. We use %s to represent string in scanf() and printf() methods. Examples of reading string value using scanf() method #include<stdio.h> #include<conio.h> int main(){ char name[50]; printf("Please enter your name : "); scanf("%s", name); printf("Hello! %s , welcome to btech smart class !!", name);
  • 7. return 0; } When we want to read multiple words or a line of text, we use a pre-defined method gets(). The gets() method terminates the reading of text with Enter character. Examples of reading string value using gets() method #include<stdio.h> #include<conio.h> int main(){ char name[50]; printf("Please enter your name : "); gets(name); printf("Hello! %s , welcome to btech smart class !!", name); return 0; } C Programming language provides a set of pre-definied functions called String Handling Functions to work with string values. All the string handling functions are defined in a header file called string.h. String Handling Functions in C C programming language provides a set of pre-defined functions called string handling functions to work with string values. The string handling functions are defined in a header file called string.h. Whenever we want to use any string handling function we must include the header file called string.h. The following table provides most commonly used string handling function and their use...
  • 8. Function Syntax (or) Example Description strcpy() strcpy(string1, string2) Copies string2 value into string1 strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1 strlen() strlen(string1) returns total number of characters in string1 strcat() strcat(string1,string2) Appends string2 to string1 strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1 strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same; less than 0 if string1<string2; greater than 0 if string1>string2 strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and string2 strcmpi() strcmpi(string1,string2) Compares two strings, string1 and string2 by ignoring case (upper or lower) stricmp() stricmp(string1, string2) Compares two strings, string1 and string2 by ignoring case (similar to strcmpi()) strlwr() strlwr(string1) Converts all the characters of string1 to lower case. strupr() strupr(string1) Converts all the characters of string1 to upper case. strdup() string1 = strdup(string2) Duplicated value of string2 is assigned to string1
  • 9. Function Syntax (or) Example Description strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of character 'b' in string1 strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of character 'b' in string1 strstr() strstr(string1, string2) Returns a pointer to the first occurrence of string2 in string1 strset() strset(string1, 'B') Sets all the characters of string1 to given character 'B'. strnset() strnset(string1, 'B', 5) Sets first 5 characters of string1 to given character 'B'. strrev() strrev(string1) It reverses the value of string1