By now, you have understood the basics of C programming.
Today I would like to venture into the world of array.
What's an array? Have you ever heard of an array before you started a coding life? ๐ Probably not or probably yes. Well relax and sit in front and welcome to the world of an array.
Definition of An Array
The array is a collection of variables that are of the same data type. Each item in an array is called an element. All elements in an array are referenced by the name of the array and are stored in a set of consecutive.
Features Of Arrays
Arrays in C programming possess several distinctive features which include
Indexed Access*.* Elements in an array are accessed using an index which is typically an integer starting from 0 for examples
- Array[0] would refer to the first element of the array.
Fixed Size. In many programming languages arrays have fixed sizes when they are declared. This means that once the size is set, it cannot be changed during runtime.
Homogenous elements. Array usually store elements of the same data type for example an array of integers would only hold integer values.
How To Declare An Array
Syntax
Data-type array-name(array-size)
Here is the breakdown of the syntax
Data_type: specify the data type of the element that the array will hold (e.g. 'int', 'char'.....)
Array_name: Name of the array identifier
Array size: define how many elements the array can contain.
Examples
Int array_int[10]
Where int specifies the data type of the array. The size of the arrays is 10 which means the array can store 10 elements.
Initialization Of Array
An array initialization is the process of assigning values to the element of the array at the time of its declaration. It is a convenient way to provide initial data for an array making it ready for use immediately after creation.
When an array is declared and initialized, the following steps occur
Memory is allocated for the array based on the specified size
The provided values are copied into the allocated memory location filling the array element.
If you provide fewer values than the array size. The remaining element will be automatically initialized to zero or the null character.
Look at the examples.
#include <stdio.h>
int main() {
int grades[5] = {85, 92, 78, 95, 88};
char name[10] = "Binka";
double prices[3] = {12.5, 18.75, 22.0};
printf("Grades: %d %d %d %d %d\n", grades[0], grades[1], grades[2], grades[3], grades[4]);
printf("Name: %s\n", name);
printf("Prices: %.2f %.2f %.2f\n", prices[0], prices[1], prices[2]);
return 0;
}
In this example, there are three arrays which are
Grades
Name
Prices
They are all declared and initialized with values. The initialized values are then accessed and printed using 'printf'
Also from the examples, remember that double is a data type used to represent double-precision floating point numbers. It is used to store real numbers.
In conclusion, Array in C is a powerful feature that allows you to efficiently store and manage the collection of elements of the same data type.