C Arrays

What are Arrays in C?

An array in C is a collection of elements of the same data type stored in consecutive memory locations.

Arrays allow you to store multiple values using a single variable name, making programs easier to write and manage.

Each element in an array is accessed using an index number. In C, array indexing starts from 0.

Arrays are commonly used to store lists of numbers, characters, or other related data.

Example

int marks[5] = {80, 75, 90, 85, 95};

Explanation

• marks is the array name.

• The array stores five integer values.

• Each value is stored in consecutive memory locations.

• The first element is marks[0].

• The last element is marks[4].

Why Arrays are Important?

Arrays are important because they:

• Store multiple values using a single variable.

• Reduce the need for creating many variables.

• Make programs more organized and readable.

• Allow easy access to data using index numbers.

• Improve program efficiency when working with large amounts of data.

Types of Arrays in C

C mainly provides the following types of arrays:

• One-Dimensional Array

• Two-Dimensional Array

• Multi-Dimensional Array

Each type of array is used for different programming requirements.

One-Dimensional Array

A one-dimensional array stores elements in a single row. It is the most commonly used type of array in C Programming.

Each element is accessed using a single index, starting from 0.

Example

int marks[5] = {80, 75, 90, 85, 95};

Explanation

• marks is the array name.

• The array can store 5 integer values.

• The first element is stored at index 0.

• The last element is stored at index 4.

• Each element can be accessed using its index number.

Two-Dimensional Array

A two-dimensional array stores data in rows and columns, forming a table-like structure.

Each element is accessed using two indexes: one for the row and one for the column.

Two-dimensional arrays are useful for storing matrices, tables, and grid-based data.

Example

int marks[2][3] = {
{80, 75, 90},
{85, 95, 88}
};

Explanation

• marks is a two-dimensional array.

• It has 2 rows and 3 columns.

• The first row contains 80, 75, and 90.

• The second row contains 85, 95, and 88.

• Elements are accessed using row and column indexes.

Multi-Dimensional Array

A multi-dimensional array is an array with more than two dimensions.

It is used to store complex data in multiple levels.

Although one-dimensional and two-dimensional arrays are commonly used, multi-dimensional arrays are useful in advanced applications such as 3D graphics, scientific calculations, and simulations.

Example

int marks[2][2][2];

Explanation

• marks is a three-dimensional array.

• It contains multiple two-dimensional arrays.

• Three indexes are used to access an element.

• Multi-dimensional arrays are mainly used for complex data structures.

• They are commonly used in advanced programming applications.

Advantages of Arrays

• Stores multiple values using a single variable.

• Makes programs easier to read and maintain.

• Provides quick access using index numbers.

• Reduces code repetition.

• Improves program organization.

Example Program

Program

#include <stdio.h>

int main()
{
int marks[5] = {80, 75, 90, 85, 95};
int i;

printf(“Student Marks:\n”);

for(i = 0; i < 5; i++)
{
printf(“%d\n”, marks[i]);
}

return 0;
}

Output

Student Marks:
80
75
90
85
95

Explanation

• int marks[5] declares an integer array that can store 5 values.

• The array is initialized with the values 80, 75, 90, 85, and 95.

• int i declares a loop variable.

• The for loop starts from index 0 and continues until index 4.

• marks[i] accesses each element of the array using its index.

• printf() displays each array element on a new line.

• return 0; ends the program successfully.

Key Points

• An array stores multiple values of the same data type.

• Array indexing in C starts from 0.

• Arrays provide fast and easy access to elements using indexes.

• One-dimensional arrays use a single index.

• Two-dimensional arrays use row and column indexes.

• Multi-dimensional arrays are used for complex data structures.

• Arrays help reduce code repetition and improve program organization.