Table of Contents
Array in C
What is array?
Array is an ordered collection of the same type of elements which are stored in contiguous memory locations and these elements can be accessed using indices of an array.
It is used to store elements of the same data types, elements can be of primitive data types such as int, char, float, double, etc. it can also store composite data types like structure and union.

Declaration of array
It is the same as other variables but we have to specify the size of an array next to the variable in square brackets”[ ]”.
Syntax :
data_type array_name[size_of_array];
E.g.
int num[10];
Here we have created an array variable named num with size 10, which means it can store upto 10 numbers.
Initialization of array
We can initialize elements to array at the time of creation.
E.g.
int num[] = {1,2,3,4,5,6};
char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};
During initialization of an array variable there is no need to specify size of an array because the compiler knows the exact number of elements which are initialized so it will allot size and memory accordingly.
Array indexing
Array index starts from 0 not from 1. Let us take an example.
char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};
If we want to access ‘I’ from array then we have to write name[0].
0 in square brackets indicates the position of the element in an array.
The 0th (a.k.a 0 base) element is the first element.
Index of the last element of an array is “n-1” where ‘n’ is the size of the array.
As we can see in the first image.
Memory allocation
All elements of array are in contiguous memory locations,
E.g. suppose name[0] is at 1001 then name[1] will be at 1002, name[2] will be at 1003 and so on. Here it will take only one byte because it’s a character array and size of char is 1 byte in C.
Change value of array
int main()
{
char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};
name[4] = ‘Z’; // It will change the 4th element to ‘Z’.
name[8]= ‘A’; // It will change the 8th element to ‘A’.
/* If we want to access single element from array then we have to specify its index number in square bracket*/
printf(“%c n”,name[4]);
printf(“%s”,name);
returns 0
}
Output :
Z
ITVoZageAs
Program to take values from user
int main()
{
int nums[2],i;
printf(“Enter first number : ”);
scanf(“%d”,&nums[0]);
printf(“Enter second number : ”);
scanf(“%d”,&nums[1]);
printf(“Entered numbers are : ”);
for(i=0;i<2;i++)
{
printf(“%d ”,nums[i]);
}
returns 0;
}
Output :
Enter first number :
10
Enter second number :
20
Entered numbers are :
10 20
Accessing outbound element from array
There is no index out of bound checking in C and C++. If we run the following code then we can see that it will get compiled without any error but it will generate unexpected output.
int main()
{
int nums[5];
printf(“%d “, nums[8]);
return 0;
}
Output :
13570928
In C, if we initialize an array with more elements than specified size. Then the compiler will give a warning “[Warning] excess elements in array initializer” but it will not generate any error hence the program will run with warning.
int main()
{
int nums[3] = {1,2,3,4,5,8};
printf(“%d “, nums[1]);
return 0;
}
Output :
2
Advantages of array
We can access any element from the array using its index.
We have to write less lines of code to store many elements.
Looping through the array is much easier; only one loop is enough.
It is easy to sort elements.
Disadvantages of array
It is not dynamic in nature which means we can change its size during execution. It has a fixed size which is decided at the time of deceleration.
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
