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

PDF
35001622067_SOUMYADIP MAITY .pdf C programming
PPT
Basics of Data structure using C describing basics concepts
PPTX
Programming fundamentals week 12.pptx
PPTX
Basics of array.pptx
PPTX
Arrays.pptx
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PPTX
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
PDF
Arrays-Computer programming
35001622067_SOUMYADIP MAITY .pdf C programming
Basics of Data structure using C describing basics concepts
Programming fundamentals week 12.pptx
Basics of array.pptx
Arrays.pptx
Unit4pptx__2024_11_ 11_10_16_09.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
Arrays-Computer programming

Similar to ARRAYS.pptx (20)

PPTX
PPTX
Arrays-from-Basics-to-Advanced final.pptx
PPT
Lecture 15 Arrays with C++ programming.ppt
PPTX
Arrays in c v1 09102017
PPTX
Arrays in c language
PDF
Arraysincv109102017 180831194256
PPTX
Arrays & Strings
PDF
Arrays
PPTX
COM1407: Arrays
PDF
[ITP - Lecture 15] Arrays & its Types
PPTX
Arrays accessing using for loops
PDF
arraysfor engineering students sdf ppt on arrays
PPTX
Array
PPTX
array.pptx ppt ggsipu bpit delhi rohini.
PPTX
Data Structures - Lecture 3 [Arrays]
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
Arrays basics
PPTX
BHARGAVIARRAY.PPT.pptx
PDF
Cunit3.pdf
Arrays-from-Basics-to-Advanced final.pptx
Lecture 15 Arrays with C++ programming.ppt
Arrays in c v1 09102017
Arrays in c language
Arraysincv109102017 180831194256
Arrays & Strings
Arrays
COM1407: Arrays
[ITP - Lecture 15] Arrays & its Types
Arrays accessing using for loops
arraysfor engineering students sdf ppt on arrays
Array
array.pptx ppt ggsipu bpit delhi rohini.
Data Structures - Lecture 3 [Arrays]
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Arrays basics
BHARGAVIARRAY.PPT.pptx
Cunit3.pdf
Ad

More from MohammedtajuddinTaju (12)

PDF
EMBEDDED SYSTEM AUTOSAR.pdf
PPTX
numbers_systems.pptx
PPTX
POINTERS.pptx
PPTX
Nested and Infinite loops.pptx
PPTX
LOOPS IN C.pptx
PPTX
FUNCTIONS1.pptx
PPTX
FUNCTIONS.pptx
PPTX
ARRAYS1.pptx
PPTX
PPTX
INTRODUCTION TO C LANGUAGE.pptx
PPTX
EMBEDDED SYSTEMS INTRODUCTION.pptx
PPTX
C PROGRAMING.pptx
EMBEDDED SYSTEM AUTOSAR.pdf
numbers_systems.pptx
POINTERS.pptx
Nested and Infinite loops.pptx
LOOPS IN C.pptx
FUNCTIONS1.pptx
FUNCTIONS.pptx
ARRAYS1.pptx
INTRODUCTION TO C LANGUAGE.pptx
EMBEDDED SYSTEMS INTRODUCTION.pptx
C PROGRAMING.pptx
Ad

Recently uploaded (20)

PPTX
Ways of Finding Calm In Everyday Life
PPTX
vinay_mahavar_enhanddsfsdfssdfssfced.pptx
PDF
Secondary Research in Case Competitions.pdf
PPTX
Unit-5 .pptx sem 3 electrical circuits and machines
PPTX
Fabrication 1.pptx from electronics circuits and devices
PPSX
Presentatiohdhdhdhdhdhfhfbfhrrbrurbrurbn.ppsx
PDF
Research_on_Rail_Pressure_Control_of_High-Pressure.pdf
PPTX
Understanding-Oral-Thrusyrgffthfyhfh.pptx
PPTX
Installation and Maintenance in Hardware
PDF
Controllers, Flowmeters, and Signal Converters.pdf
PPT
Light utility vehicles 522024.ppt
PPTX
美国乔治华盛顿大学硕士毕业证{GWU毕业完成信GWU成绩单}如何办理学历认证
PPTX
LESSON-9-Gender-anddtdffffcd-School.pptx
PPTX
vinay_mahavar_industrial_training_3D.pptx
PPTX
PUBLIC ADMINISTRATIONS -104-GROUP-1.pptx
PPTX
加拿大埃德蒙顿康考迪亚大学毕业证书{CUE海牙认证CUE成绩单防伪}100%复刻
PDF
crc presentation44444444444444444444.pdf
PPTX
ppt stssssssssssssssssssssssssssssssssssssss.pptx
PPTX
5. PPT Bersikap Kreatif.pptxjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
PPTX
Quiz template 300 pages advanced and Tech friendly
Ways of Finding Calm In Everyday Life
vinay_mahavar_enhanddsfsdfssdfssfced.pptx
Secondary Research in Case Competitions.pdf
Unit-5 .pptx sem 3 electrical circuits and machines
Fabrication 1.pptx from electronics circuits and devices
Presentatiohdhdhdhdhdhfhfbfhrrbrurbrurbn.ppsx
Research_on_Rail_Pressure_Control_of_High-Pressure.pdf
Understanding-Oral-Thrusyrgffthfyhfh.pptx
Installation and Maintenance in Hardware
Controllers, Flowmeters, and Signal Converters.pdf
Light utility vehicles 522024.ppt
美国乔治华盛顿大学硕士毕业证{GWU毕业完成信GWU成绩单}如何办理学历认证
LESSON-9-Gender-anddtdffffcd-School.pptx
vinay_mahavar_industrial_training_3D.pptx
PUBLIC ADMINISTRATIONS -104-GROUP-1.pptx
加拿大埃德蒙顿康考迪亚大学毕业证书{CUE海牙认证CUE成绩单防伪}100%复刻
crc presentation44444444444444444444.pdf
ppt stssssssssssssssssssssssssssssssssssssss.pptx
5. PPT Bersikap Kreatif.pptxjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
Quiz template 300 pages advanced and Tech friendly

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; }