C Arrays

Arrays are an important data structure in C programming that allow you to store multiple values of the same data type in a single variable. In this article, we will cover the basics of arrays in C, including declaration, value assignment, accessing index values, updating index values, and using arrays in loops.

Declaration of arrays

To declare an array in C, you use the following syntax:

1data_type array_name[array_size];

For example:

1int numbers[5];

This declares an integer array named "numbers" with 5 elements.

Value assignment to arrays

You can assign values to an array when it is declared or after it is declared. To assign values to an array when it is declared, you use the following syntax:

1data_type array_name[array_size] = {value1, value2, value3, ...};

For example:

1int numbers[5] = {1, 2, 3, 4, 5};

This declares an integer array named "numbers" with 5 elements and assigns the values 1, 2, 3, 4, and 5 to the elements of the array.

Accessing index values in arrays

To access an individual element of an array, you use the array name followed by the index of the element in square brackets. The index of the first element of an array is 0, the second element is 1, and so on.

For example:

1int first_element = numbers[0];

This assigns the value of the first element of the "numbers" array to the "first_element" variable.

Updating index values in arrays

To update the value of an element in an array, you simply assign a new value to it using the same syntax as accessing the value.

For example:

1numbers[0] = 10;

This updates the value of the first element of the "numbers" array to 10.

Using arrays in loops

Arrays can be used in loops to process the values stored in them. Two common examples of using arrays in loops are printing the elements of an array and finding the sum of the elements of an array.

Example 1: Printing elements of an array using a for loop

1#include <stdio.h> 2 3int main() { 4 int numbers[5] = {1, 2, 3, 4, 5}; 5 int i; 6 7 for (i = 0; i < 5; i++) { 8 printf("%d ", numbers[i]); 9 } 10 11 return 0; 12}

Output:

11 2 3 4 5

In this example, a for loop is used to iterate through the elements of the "numbers" array, and the elements are printed using the printf() function.

Example 2: Finding the sum of the elements of an array using a for loop

1#include <stdio.h> 2 3int main() { 4 int numbers[5] = {1, 2, 3, 4, 5}; 5 int i, sum = 0; 6 7 for (i = 0; i < 5; i++) { 8 sum += numbers[i]; 9 } 10 printf("The sum of the elements of the array is: %d\n", sum); 11 return 0; 12}

Output:

1The sum of the elements of the array is: 15

In this example, a for loop is used to iterate through the elements of the "numbers" array, and the sum of the elements is calculated and stored in the "sum" variable. The final value of "sum" is then printed using the printf() function.

Arrays are an important data structure in C programming that allow you to store and manipulate multiple values of the same data type. By understanding the basics of arrays in C, including declaration, value assignment, accessing index values, updating index values, and using arrays in loops, you can utilize arrays effectively in your C programs.