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:
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:
You can also initialize the array elements during declaration:
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:
You can determine the number of elements in an array using the sizeof operator. For example:
You can perform various operations on single-dimensional arrays, such as looping through the elements:
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:
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:
- We declare an integer array values capable of holding 5 integers.
- We use a for loop to prompt the user to enter 5 values one at a time, and we store them in the array.
- 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
We start by asking the user to enter the number of values they want to calculate the average for (n).Above program:
- We start by asking the user to enter the number of values they want to calculate the average for (n).
- We declare an array values of size n to store the input values.
- We use a loop to read n values from the user and store them in the array.
- We calculate the sum of all the values by iterating through the array.
- Finally, we calculate the average by dividing the sum by n and display the result.
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.
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.