In this chapter you will learn how to work with arrays in C programming. So by the end of this chapter you will learn how to delare, initialize and access an array.
Array is a special type of variable in which a certain number of values of the same type can be stored together.
For example: If you want to store exam marks of 1000 students then instead of single variable create array variable like below.
Table of Contents
float mark[1000];
In C programming, array type and length cannot be changed after array declaration.
There are two types of arrays in C programming:
(i) One-Dimensional array
(ii) Multidimensional array
Array Declaration Syntax
dataType arrayName[arraySize];
For example
float mark[5];
Here an array variable named mark and of floating point type is declared with size 5. So it can accept 5 float values.
Rules for accessign elements of Array
Array elements are accessed using index.
Suppose you have declared the mark array above. Then to access the elements of mak write marks [0] for the first element, mark[1] for the first element, mark[1] for the second element and so on.
Some important tips about Array
- Array indexing starts with 0 insead of 1. In the above example the first element of the array is: mark[0]
- If the size of Array is n(n-1) then index is used to access its last element. In the above example the size of the array is 5 so the last element is accessed through mark[4].
- Suppose mark[0] its address starts with 8024a then mark[1] its address will be 8028a and mark[2] its address will be 8032a, because size of float variable 4 bytes.
How is arrray intialized in C Programming?
Array variables can also be initialized during declaration. That is the value can be assigned to the array at the time of declaration.
for example:
Array initialization with size during size during declaration
int mark[5] = {89, 80, 88, 83, 90};
Array initialization without size during declaration
int mark[] = {89, 80, 88, 83, 90};
Diagram of array initialization
here
mark[0] is equal to 89
mark[1] is equal to 80
mark[2] is equal to 88
mark[3] is equal to 83
mark[4] is equal to 90
Array elements how to insert and print?
int mark[5] = {89, 80, 88, 83, 90}
// Insert different value in place of third element(insert)
marks[2] = 9;
// Insert value into third element with input by user
scanf("%d", &mark[2]);
// Insert value into (i+1)th element with input by user
scanf("%d", &mark[i]);
// Print the first element of the array
printf("%d", mark[0]);
// will print the i-th element of the array
printf("%d", age[i-1]);
Example: Use of Array in C Programming
// program to find the mean of n (n < 10) integers
#include
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i
output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average marks = 39
Some important things to keep in mind while working with C array!
Suppose you have declared an array testArray[0] using testArray[9]
testArray[15] If you want to access the array element from outside this range eg – then the compiler will not throw any arror. But because of this unexpected results like undefined can come.