Access Array Elements
Access Array Elements
We can access any element of an array in C using the array subscript operator [ ] and the index
value i of the element.
array_name [index];
One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements in the array.
// subscript
#include <stdio.h>
int main()
return 0;
Output
Element at arr[2]: 35
Element at arr[4]: 55
Element at arr[0]: 15
We can update the value of an element at the given index i in a similar way to accessing an element
by using the array subscript operator [ ] and assignment operator =.
array_name[i] = new_value;
arr[2]=40;
Element at arr[2]:40
C Array Traversal
Traversal is the process in which we visit every element of the data structure. For C array traversal,
we use loops to iterate through each element of the array.
#include <stdio.h>
int main()
arr[2] = 100;
return 0;
Output
There are two types of arrays based on the number of dimensions it has. They are as follows:
Multidimensional Arrays
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only one
dimension.
Syntax of 1D Array in C
array_name [size];
#include <stdio.h>
int main()
// 1d array declaration
int arr[5];
arr[i] = i * i - 2 * i + 1;
return 0;
Output
#include <stdio.h>
int main()
// printing string
int i = 0;
while (arr[i]) {
return 0;
Output
Geeks
Example
String Functions
C also has many useful string functions, which can be used to perform certain operations on strings.
To use them, you must include the <string.h> header file in your program:
#include <string.h>
Example
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
Note that sizeof and strlen behaves differently, as sizeof also includes the \0 character when
counting:
Example
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
It is also important that you know that sizeof will always return the memory size (in bytes), and not the
actual string length:
Example
char alphabet[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
Concatenate Strings
To concatenate (combine) two strings, you can use the strcat() function:
Example
char str1[20] = "Hello ";
char str2[] = "World!";
Note that the size of str1 should be large enough to store the result of the two strings combined (20 in our
example).
Hello World!
Example
char str1[20] = "Hello World!";
char str2[20];
// Print str2
printf("%s", str2);
Note that the size of str2 should be large enough to store the copied string (20 in our example).
Compare Strings
To compare two strings, you can use the strcmp() function.
It returns 0 if the two strings are equal, otherwise a value that is not 0:
#include <stdio.h>
// Main function
int main()
int i, n, a[100];
printf("------------------------------------------------------------------------
\n");
scanf("%d", &n);
printf("\n\n");
return 0;
Output:
------------------------------------------------------------------------
element - 1 : 5
element - 2 : 7
2 5 7
7 5 2
The second printf statement asks the user to input n number of elements into the array using a
for loop, and stores each input in the corresponding index of the array a[i].
The third printf statement then prints out the contents of the array in order using another for
loop.
The fourth printf statement then prints out the contents of the array in reverse order using yet
another for loop, which iterates over the elements of a starting from the last element and printing
each element out in reverse order.
// Main function
int main()
int arr1[100], arr2[100]; // Declare two arrays of size 100 to store integer values
printf("----------------------------------------------------\n");
// Prompt the user to input the number of elements to be stored in the array
scanf("%d", &n);
scanf("%d", &arr1[i]); // Read the input and store it in the first array
arr2[i] = arr1[i]; // Copy each element from the first array to the second array
return 0;
The second printf statement asks the user to input n number of elements into the first array arr1
using a for loop, and stores each input in the corresponding index of the array arr1[i].
The third for loop then copies the elements of arr1 into a second array arr2 by iterating over each
element of arr1 and assigning it to the corresponding index of arr2.
The next two printf statements then print out the contents of the first array arr1 and the second
array arr2 respectively, using separate for loops to iterate over the elements of each array and print
them out using printf.