SlideShare a Scribd company logo
C PROGAMMING
BY MOHAMMED TAJUDDIN
C PROGRAMMING
Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
To create an array, define the data type (like int) and specify the name
of the array followed by square brackets [].
To insert values to it, use a comma-separated list, inside curly braces:
C PROGRAMMING
Syntax for declaring an array:-
dataType array_name[arraySize];
Example:- Declare an array
int student_marks[20];
char student_name[10];
float numbers[5];
Note:- After the declaration, size of the array cannot be changed.
C PROGRAMMING
Access elements of an array in C
In C, you can access the elements of the array using their index.
Suppose, you declare an array like below:-
If you want to access the first element of the above array then do
numbers[0] and if you want to access the second element then do
numbers[1] and so on.
The first index of the array is 0 not 1. That’s why numbers[0] is the first
element.
To access the last element of the array, do numbers[4].
numbers[0] numbers[1] numbers[2] numbers[3] numbers[4]
2 3 4 5 6
C PROGRAMMING
• Initialize an array:-
• At the time of declaration, it is possible to initialize an array.
• int numbers[5] = {2, 3, 4, 5, 6};
• You can also initialize an array like below:-
• int numbers[] = {2, 3, 4, 5, 6};
• In case, if you don’t specify the size of the array during initialization then the
compiler will know it automatically.
• numbers[0] = 2
• numbers[1] = 3
• numbers[2] = 4
• numbers[3] = 5
• numbers[4] = 6
C PROGRAMMING
Basic Example of an array:-
#include<stdio.h>
int main()
{
int numbers[5] = {2, 3, 4, 5, 6};
printf(" Basic Example of an array");
printf("First number is : %dn",numbers[0]);
printf("Third number is : %dn",numbers[2]);
printf("Last number is : %dn",numbers[4]);
}
C PROGRAMMING
Change the value of array elements:-
In C, you can also change the elements of the array after declaring it.
Example:- Changing the value of array elements
#include<stdio.h>
int main()
{
int numbers[5] = {2, 3, 4, 5, 6};
numbers[0] = 4; // changing value from 2 to 4!
numbers[2] = 5; // changing value from 4 to 5!
numbers[4] = 10; // changing value from 6 to 10!
printf("First number is : %dn",numbers[0]);
printf("Third number is : %dn",numbers[2]);
printf("Last number is : %dn",numbers[4]);
}
C PROGRAMMING
#include <stdio.h>
int main()
{
//Initialize array
int arr[5] = {1, 2, 3, 4, 5};
printf("Elements of given array: n");
//Loop through the array by incrementing value of i
for (int i = 0; i < =5; i++) {
printf("%d ", arr[i]);
}
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a;
for(a = 9; a >= 0; a--)
printf("%d ", array[a]);
return 0;
}
C PROGRAMMING
Arrays using functions :
#include <stdio.h>
void display(int age1, int age2) {
printf("%dn", age1);
printf("%dn", age2);}
int main() {
int ageArray[] = {2, 8, 4, 12};
// pass second and third elements to display()
display(ageArray[1], ageArray[2]);
return 0;
}

More Related Content

Similar to ARRAYS.pptx

Arrays
ArraysArrays
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
rohinitalekar1
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
FolkAdonis
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Array
ArrayArray
Array
ArrayArray
Array
Anil Dutt
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
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
aroraopticals15
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
SwapnaliPawar27
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
fixocin377
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1IIUM
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 

Similar to ARRAYS.pptx (20)

Arrays
ArraysArrays
Arrays
 
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
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Array
ArrayArray
Array
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
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 & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 

More from MohammedtajuddinTaju

EMBEDDED SYSTEM AUTOSAR.pdf
EMBEDDED SYSTEM AUTOSAR.pdfEMBEDDED SYSTEM AUTOSAR.pdf
EMBEDDED SYSTEM AUTOSAR.pdf
MohammedtajuddinTaju
 
numbers_systems.pptx
numbers_systems.pptxnumbers_systems.pptx
numbers_systems.pptx
MohammedtajuddinTaju
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
MohammedtajuddinTaju
 
Nested and Infinite loops.pptx
Nested and Infinite loops.pptxNested and Infinite loops.pptx
Nested and Infinite loops.pptx
MohammedtajuddinTaju
 
LOOPS IN C.pptx
LOOPS IN C.pptxLOOPS IN C.pptx
LOOPS IN C.pptx
MohammedtajuddinTaju
 
FUNCTIONS1.pptx
FUNCTIONS1.pptxFUNCTIONS1.pptx
FUNCTIONS1.pptx
MohammedtajuddinTaju
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
MohammedtajuddinTaju
 
ARRAYS1.pptx
ARRAYS1.pptxARRAYS1.pptx
ARRAYS1.pptx
MohammedtajuddinTaju
 
DMA.pptx
DMA.pptxDMA.pptx
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
MohammedtajuddinTaju
 
EMBEDDED SYSTEMS INTRODUCTION.pptx
EMBEDDED SYSTEMS INTRODUCTION.pptxEMBEDDED SYSTEMS INTRODUCTION.pptx
EMBEDDED SYSTEMS INTRODUCTION.pptx
MohammedtajuddinTaju
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
MohammedtajuddinTaju
 

More from MohammedtajuddinTaju (12)

EMBEDDED SYSTEM AUTOSAR.pdf
EMBEDDED SYSTEM AUTOSAR.pdfEMBEDDED SYSTEM AUTOSAR.pdf
EMBEDDED SYSTEM AUTOSAR.pdf
 
numbers_systems.pptx
numbers_systems.pptxnumbers_systems.pptx
numbers_systems.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
Nested and Infinite loops.pptx
Nested and Infinite loops.pptxNested and Infinite loops.pptx
Nested and Infinite loops.pptx
 
LOOPS IN C.pptx
LOOPS IN C.pptxLOOPS IN C.pptx
LOOPS IN C.pptx
 
FUNCTIONS1.pptx
FUNCTIONS1.pptxFUNCTIONS1.pptx
FUNCTIONS1.pptx
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
ARRAYS1.pptx
ARRAYS1.pptxARRAYS1.pptx
ARRAYS1.pptx
 
DMA.pptx
DMA.pptxDMA.pptx
DMA.pptx
 
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
 
EMBEDDED SYSTEMS INTRODUCTION.pptx
EMBEDDED SYSTEMS INTRODUCTION.pptxEMBEDDED SYSTEMS INTRODUCTION.pptx
EMBEDDED SYSTEMS INTRODUCTION.pptx
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 

ARRAYS.pptx

  • 2. C PROGRAMMING Arrays Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces:
  • 3. C PROGRAMMING Syntax for declaring an array:- dataType array_name[arraySize]; Example:- Declare an array int student_marks[20]; char student_name[10]; float numbers[5]; Note:- After the declaration, size of the array cannot be changed.
  • 4. C PROGRAMMING Access elements of an array in C In C, you can access the elements of the array using their index. Suppose, you declare an array like below:- If you want to access the first element of the above array then do numbers[0] and if you want to access the second element then do numbers[1] and so on. The first index of the array is 0 not 1. That’s why numbers[0] is the first element. To access the last element of the array, do numbers[4]. numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] 2 3 4 5 6
  • 5. C PROGRAMMING • Initialize an array:- • At the time of declaration, it is possible to initialize an array. • int numbers[5] = {2, 3, 4, 5, 6}; • You can also initialize an array like below:- • int numbers[] = {2, 3, 4, 5, 6}; • In case, if you don’t specify the size of the array during initialization then the compiler will know it automatically. • numbers[0] = 2 • numbers[1] = 3 • numbers[2] = 4 • numbers[3] = 5 • numbers[4] = 6
  • 6. C PROGRAMMING Basic Example of an array:- #include<stdio.h> int main() { int numbers[5] = {2, 3, 4, 5, 6}; printf(" Basic Example of an array"); printf("First number is : %dn",numbers[0]); printf("Third number is : %dn",numbers[2]); printf("Last number is : %dn",numbers[4]); }
  • 7. C PROGRAMMING Change the value of array elements:- In C, you can also change the elements of the array after declaring it. Example:- Changing the value of array elements #include<stdio.h> int main() { int numbers[5] = {2, 3, 4, 5, 6}; numbers[0] = 4; // changing value from 2 to 4! numbers[2] = 5; // changing value from 4 to 5! numbers[4] = 10; // changing value from 6 to 10! printf("First number is : %dn",numbers[0]); printf("Third number is : %dn",numbers[2]); printf("Last number is : %dn",numbers[4]); }
  • 8. C PROGRAMMING #include <stdio.h> int main() { //Initialize array int arr[5] = {1, 2, 3, 4, 5}; printf("Elements of given array: n"); //Loop through the array by incrementing value of i for (int i = 0; i < =5; i++) { printf("%d ", arr[i]); }
  • 9. #include <stdio.h> int main() { int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int a; for(a = 9; a >= 0; a--) printf("%d ", array[a]); return 0; }
  • 10. C PROGRAMMING Arrays using functions : #include <stdio.h> void display(int age1, int age2) { printf("%dn", age1); printf("%dn", age2);} int main() { int ageArray[] = {2, 8, 4, 12}; // pass second and third elements to display() display(ageArray[1], ageArray[2]); return 0; }