Code Point Blog
Iam presenting here ,all type of knowledge if you want to interest knowing about any topic then click below thumbnail view button and knowing.
In C programming, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays provide a way to store multiple values of the same type under a single name and are widely used to work with lists, matrices, strings, and other data structures. The elements in an array can be accessed using their index, which starts from 0 and goes up to the size of the array minus one.
The syntax to declare an array in C is as follows:
data_type array_name[size];
Here, 'data_type' represents the type of elements in the array, 'array_name' is the name of the array, and 'size' is the number of elements the array can hold.Here's an example of declaring and using an array in C:
#include <stdio.h>
int main() {
( Declaration of an integer array of size 5 )
int numbers[5];
( Initialization of array elements )
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
( Accessing and printing array elements using a loop )
for (int i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0;
}
output :
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Output:
In this example, we declared an integer array called 'numbers' with a size of 5. Then, we initialized each element with different values and used a 'for' loop to access and print each element in the array.
It's important to note that arrays in C have fixed sizes determined at the time of declaration. Once the size is defined, it cannot be changed during program execution. C also supports multidimensional arrays, which are arrays of arrays and can be used to represent matrices and other complex data structures.