C Array (With Examples)

Arrays in C programming are collections of elements of the same data type stored in contiguous memory locations, accessible through an index. They offer a convenient way to store and manipulate a list of values of the same type. Arrays have fixed sizes determined at declaration and are declared using square brackets with the number of elements.

Declaration and Initialization

To declare a single-dimensional array in C, you must specify the type of the elements in the array and the number of elements in the array. The following code shows how to declare a single-dimensional array of integers:

int myArray[10];

This code declares an array of 10 integers. The index of the first element in the array is 0, and the index of the last element in the array is 9.

To initialize the elements of an array, you can use the following syntax:

arrayName[0] = value1; arrayName[1] = value2; ... arrayName[9] = value10;

You can also initialize the array elements during declaration:

int numbers[5] = {1, 2, 3, 4, 5}; // Initializes array elements during declaration.

Accessing Elements

Elements in a single-dimensional array are accessed using square brackets and an index. The index starts at 0 for the first element. For example:

int thirdElement = numbers[2]; // Accesses the third element (index 2) and assigns it to 'thirdElement'.

Array Size

You can determine the number of elements in an array using the sizeof operator. For example:

int size = sizeof(numbers) / sizeof(numbers[0]); // Calculates the number of elements in 'numbers'.

Array Operations

You can perform various operations on single-dimensional arrays, such as looping through the elements:

for (int i = 0; i < 5; i++) { printf("Element at index %d: %d\n", i, numbers[i]); }

Calculate the sum of the elements in the array

Single-dimensional arrays can be used to solve a variety of problems in C programming. For example, the following code uses a single-dimensional array to calculate the sum of the elements in the array:

int main() { int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0; for (int i = 0; i < 10; i++) { sum += myArray[i]; } printf("The sum of the elements in the array is: %d\n", sum); return 0; }
Output: The sum of the elements in the array is: 55

Input and Output in C Array

Following is a simple C program that takes 5 values from the user and stores them in an array, then prints the elements stored in the array:

#include <stdio.h> int main() { // Declare an array to store 5 values int values[5]; // Prompt the user to enter 5 values printf("Enter 5 values, one at a time:\n"); // Use a loop to read values from the user and store them in the array for (int i = 0; i < 5; i++) { printf("Enter value %d: ", i + 1); scanf("%d", &values[i]); } // Print the elements stored in the array printf("Elements stored in the array:\n"); for (int i = 0; i < 5; i++) { printf("Value %d: %d\n", i + 1, values[i]); } return 0; }
Above program:
  1. We declare an integer array values capable of holding 5 integers.
  2. We use a for loop to prompt the user to enter 5 values one at a time, and we store them in the array.
  3. After all values are entered, we use another for loop to print the elements stored in the array, along with their corresponding positions (indices).

Program to find the average of n numbers using arrays

#include <stdio.h> int main() { int n; // Prompt the user to enter the number of values printf("Enter the number of values: "); scanf("%d", &n); // Declare an array to store the values double values[n]; // Read values from the user and store them in the array printf("Enter %d values, one at a time:\n", n); for (int i = 0; i < n; i++) { printf("Enter value %d: ", i + 1); scanf("%lf", &values[i]); } // Calculate the sum of all values double sum = 0; for (int i = 0; i < n; i++) { sum += values[i]; } // Calculate and display the average double average = sum / n; printf("Average of %d values: %.2lf\n", n, average); return 0; }

We start by asking the user to enter the number of values they want to calculate the average for (n).

Above program:
  1. We start by asking the user to enter the number of values they want to calculate the average for (n).
  2. We declare an array values of size n to store the input values.
  3. We use a loop to read n values from the user and store them in the array.
  4. We calculate the sum of all the values by iterating through the array.
  5. Finally, we calculate the average by dividing the sum by n and display the result.

Array Bounds

Be cautious not to access elements outside the bounds of the array, as it can lead to undefined behavior and memory corruption. C does not perform bounds checking.

Arrays with Other Data Types

You can create arrays of other data types, such as characters, floats, or custom structures, following the same syntax.

Conclusion

Single-dimensional arrays are fundamental in C programming and are widely used for tasks like storing data, implementing data structures, and performing numerical computations. Understanding how to declare, initialize, and manipulate arrays is essential for any C programmer.