Previous Next

C Arrays

An array is a collection of comparable data elements. It is stored in consecutive memory locations in arrays.

Array Declaration

Just like any other variable, an array has to be declared so that the compiler can recognize what type and how big we need it to be.

data_type array_name [size];

Accessing Elements of an Array

The index number of an element in an array makes it easy to access the element. The index number starts from 0.

Example:

#include <stdio.h>

int main()
{
    int myArr[] = {2, 6, 8, 10};

    printf("%d", myArr[2]);
    return 0;
}

Output

8

Modify an Array Element

To modify the value of an element, use the index number.

Example:

#include <stdio.h>

int main()
{
    int myArr[] = {2, 6, 8, 10};
    myArr[1] = 5;

    printf("%d", myArr[1]);
    return 0;
}

Output

5
Previous Next