Arrays in C
Arrays in C
Define array:
An array represents a group of elements of same data type.
Arrays are generally categorized into two types:
Single Dimensional arrays (or 1 Dimensional arrays)
Multi-Dimensional arrays (or 2 Dimensional arrays, 3 Dimensional arrays, …)
Single Dimensional Arrays: A one dimensional array or single dimensional array represents a
row or a column of elements.
For example, the marks obtained by a student in 5 different subjects can be represented by a
1D array.
· We can declare a one dimensional array and directly store elements at the time of its
declaration,as:
int marks[] = {50, 60, 55, 67, 70};
Array elements are stored in contiguous memory locations. Each element is identified by an
index starting with "0". The lowest address corresponds to the first element and the highest
address to the last element.
Declaring an Array in C
To declare an array in C, you need to specify the type of the elements and the number of
elements to be stored in it.
type arrayName[size];
Initializing an Array in C
If a set of comma-separated sequence values put inside curly brackets is assigned in the
declaration, the array is created with each element initialized with their corresponding value.
ex:
#include <stdio.h>
int main()
{
int marks[] = {50, 60, 55, 67, 70,i;
for(i=0<i<5;i++)
{
printf("array elemements are: %d", marks[i]);
return 0;
}
ex:
#include<stdio.h>
void main()
{
int A[2][2]={{1,2},{3,4}},r,c;
printf("A matrix is \n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d\t",A[r][c]);
}
printf("\n");
}
}
Output:
1 2
3 4