Public App

a blog about technology

Arrays in C Language

In C programming language, an array is a collection of elements of the same data type, stored in contiguous memory locations. Each element in the array is accessed using an index, which is an integer value that represents the position of the element in the array.

Here is an example of declaring and initializing an array of integers in C:

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

In this example, myArray is an array of 5 integers, and the values of the elements are initialized with the values provided in the braces. The first element in the array is accessed using index 0, the second element with index 1, and so on. For example, to print the value of the third element in the array, we can use:

printf("%d", myArray[2]);  // prints "3"

Arrays can also be used with loops, such as the for loop, to iterate over all the elements in the array. For example, the following code uses a loop to print all the elements of the array:

for (int i = 0; i < 5; i++) {
    printf("%d ", myArray[i]);
}

This will output: “1 2 3 4 5”.

It is important to note that arrays in C are not dynamic, meaning their size is fixed at the time of declaration. In addition, the size of an array must be an integer constant expression, which means it must be a value that can be determined at compile time. To work with arrays that can be resized at runtime, dynamic memory allocation can be used with pointers.

Amitesh Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top