Go Back

Introduction to Arrays in c language

11/15/2023
All Articles

#Introduction Arrays in c language

Introduction to Arrays in c language

Introduction to Arrays in c language

An array is a fundamental data structure in programming that allows you to store multiple values of the same data type under a single name.
Each element in the array is identified by an index or a key.

following are step in Array :

1)Declaration:
To use an array, you need to declare it. The declaration specifies the type of elements the array will hold and its size (the number of elements).

int numbers[10];  // An array of 10 integers

2)Initialization:
Arrays can be initialized during declaration or later in the code as follow.

int numbers[5] = {1, 2, 3, 4, 5};  // Initializing during declaration
Alternatively, you can initialize elements individually:

numbers[0] = 1;
numbers[1] = 2;


3)Accessing Elements:
Elements in an array are accessed using their index. In c languages, indexing starts from 0.following are example

int thirdElement = numbers[2];  // Accessing the third element

4)Size of Array:
The size of an array is fixed during declaration and cannot be changed during runtime.
int size = sizeof(numbers) / sizeof(numbers[0]);  // Calculate the size of the array

5)Iterating Over Arrays:
Loops are commonly used to iterate over the elements of an array.

for (int i = 0; i < 5; ++i) {
    printf("%d ", numbers[i]);
}

6) Multidimensional Arrays:
Arrays can have multiple dimensions, forming matrices or higher-dimensional structures.

int matrix[3][4] = {{1, 2, 3,4}, {5, 6,7,8}, {9,10,11,12}};

Article