CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – An introduction to Arrays

Arrays are sequences of elements of the same data type. In C, arrays have boundaries; the lowest boundary is always 0 (zero) and the highest boundary depends on the size allocated to the array. Arrays are very useful especially when dealing with large data.

Array declaration in C

In C, declaring an array is simple as declaring a variable; the only difference is for an array we need to mention the number of elements an array can hold through the square brackets, []. Memory will be allocated depending on the number of elements we have requested/defined for an array.

data_type array_name [number_of_elements];

Where data_type is the data type of the elements of the array; each array element has the same data type. array_name is the name of the array and number_of_elements is the number of elements the array can hold.

As mentioned above, an array has its boundaries; the least value is always 0 (zero) and here the highest boundary value is number_of_elements - 1.

Assigning values to an array

In C, we can assign values to an array at the time of declaring it, or by using the index operator [] we can assign the values to the specific location in an array.

Below are examples of assigning values to arrays at the time of declaration.

int values[] = { 1, 89, 78, 56, 45, 98, 6578, 57898 };
char str[10] = "Hello!"; // char str[10] = {'H', 'e', 'l', 'l', 'o', '!'};
float margins[5] = {45, 48.5, 25, 32.8, 38};
int arr[5] = {0}; // Assign all elements with 0.

And here are examples of assigning values to arrays using the index operator []. Note that when you assign the new values to the array, its old values will be erased.

int values[3] = 20; // previous value 56 replaced with the new value 20
char str[5] = '.'; // previous character '!' replaced with the character '.'.

Be cautious when you assign the values to the arrays using the index operator []. As mentioned earlier, arrays have boundaries; when defining an array we need to mention the maximum elements an array can store. If we attempt to access the array using out of its boundary value the program will be terminated.

As the boundary check can not be identified during the compile time, you do not see a warning and the application will simply be terminated during the runtime.

Accessing values from an array

By using the index operator [] and the index value, we can access the values from an array. Always remember that when accessing the values from an array the index value must be within the array boundaries.

For example, the below declaration creates an array and it has an upper boundary value is 4. That means, 0 to 4 are the index values we can use to access array elements.

int arr[5] = {0};

From above, arr[0] will give the first array element and arr[4] (as it has maximum 5 elements) will give the last element of an array. Below is the for loop to iterate within an array and display the values of an array;

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

// Malin

C Programming – An introduction to Arrays

2 thoughts on “C Programming – An introduction to Arrays

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top