Docsity

Array in C Programming – Complete Notes with Examples and Output, Study notes of Computer Science

Toggle sidebar
bg1
Arrays in C
An array is a linear data structure that stores a fixed-size sequence of
elements of the same data type in contiguous memory locations. Each
element can be accessed directly using its index, which allows for efficient
retrieval and modification.
#include <stdio.h>
int main() {
int arr[] = {2, 4, 8, 12, 16, 18};
int n = sizeof(arr)/sizeof(arr[0]);
// Printing array elements
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
2 4 8 12 16 18
The below image shows the array created in the above program.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Array in C Programming – Complete Notes with Examples and Output and more Study notes Computer Science in PDF only on Docsity!

Arrays in C

An array is a linear data structure that stores a fixed-size sequence of

elements of the same data type in contiguous memory locations. Each

element can be accessed directly using its index, which allows for efficient

retrieval and modification.

#include int main() { int arr[] = {2, 4, 8, 12, 16, 18}; int n = sizeof(arr)/sizeof(arr[0]); // Printing array elements for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }

Output

The below image shows the array created in the above program.

To understand the key characteristics of arrays such as fixed size, contiguous

memory allocation, and random access. Refer to this article: Properties of

Arrays

Creating an Array

The whole process of creating an array can be divided into two primary sub

processes i.e.

  1. Array Declaration

Array declaration is the process of specifying the type, name, and size of the

array. In C, we have to declare the array like any other variable before using

it.

When we declare an array in C, the compiler allocates the memory block of

the specified size to the array name.

Accessing Array Elements

Array in C provides random access to its elements, which means that we can

access any element of the array by providing the position of the element,

called the index.

#include int main() { // array declaration and initialization int arr[5] = {2, 4, 8, 12, 16}; // accessing element at index 2 i.e 3rd element printf("%d ", arr[2]); // accessing element at index 4 i.e last element printf("%d ", arr[4]); // accessing element at index 0 i.e first element printf("%d ", arr[0]); return 0; }

Output

Traversing An Array #include int main() { int arr[5] = {2, 4, 8, 12, 16}; // Print each element of // array using loop printf("Printing Array Elements\n"); for(int i = 0; i < 5; i++){ printf("%d ", arr[i]); } printf("\n"); // Printing array element in reverse printf("Printing Array Elements in Reverse\n"); for(int i = 4; i>=0; i--){ printf("%d ", arr[i]); } return 0; }

Output

Printing Array Elements 2 4 8 12 16 Printing Array Elements in Reverse 16 12 8 4 2 Size of Array

The size of the array refers to the number of elements that can be stored in

the array. The array does not contain the information about its size but we

can extract the size using sizeof() operator.

#include int main() { int arr[5] = {2, 4, 8, 12, 16}; // Size of the array int size = sizeof(arr)/sizeof(arr[0]); printf("%d", size); return 0; }

Output

The sizeof() operator returns the size in bytes. sizeof(arr) returns the total

number of bytes of the array. In an array, each element is of type int, which is