Table of Contents
In this lesson, you will learn about arrays in C, their usage, how to declare and initialize them.
An array in C is a Data Structure that can store a collection of elements of the same Data Type. For Example, if you want to store ten values of the same Data Type, you can use an array to store your values instead of declaring ten variables.
char array[5];
An array can store data of Primitive Data Types, such as int, float, decimal, bool, or derived data types, such as structures and pointers.
Structure of an array
An array strat with the index zero means an array of 5 elements will have an index starting from zero to four. For Example:
int arr[5];
is represented in this form:
Array declaration in C
To declare an Array in C, first specify the Data Type of the elements of that array, the array name, then the number of elements in this array inside two brackets
int lessons[6]; // Array of type integer with 6 elements char Alphabets[4]; //Array of type char with 4 elements;
Array initializing in C
Array initializing can happen during the declaration.
int lesson[6] = {1, 2, 3, 4, 5, 6 }; // Declaration and initializing an array char Alphabets[4] ={'A' , 'B' , 'C' , 'D'}; // Declaration and initializing an array
Or, you can declare and initialize an array without specifying a size.
int months[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Accessing Array Elements in C
To access an array element, use the index of that element as shown below:
char Alphabets[4] = {'A' , 'B' , 'C' , 'D'}; char myAlphabet = Alphabets[2]; // assigning the value of second index to a variable.
Assign a value to an array element in C
You can assign or modify the value of an array.
char Alphabets[4] ={'A' , 'B' , 'C' , 'D'}; Alphabets[2] = 'Z'; // to change the 3 element value to 'Z'
Input/Output Array Elements in C
An Array can be assigned values from the user by using the scanf function.
scanf("%c", &Alphabets[0]); // take input and store it in the 1st element
Also, we can print the value of an array element to the console using the printf function.
printf("%c", Alphabets[3]); // print the 4th element of the array to the console
You can refer to Input Output in C lesson for more details.
Loop through an array in C Example
#include <stdio.h> int main() { int array[10] = {1, 2, 3, 4, 5 }; int sum = 0; int i; for (i = 0; i < 5; i++) { sum += array[i]; } printf("The Sum of the array elements is %d\n", sum); return 0; }
Output
The Sum of the array elements is 15
Array and input/output operations Example
#include <stdio.h> int main(void) { int numbers[5]; int count = 5; int sum = 0; float average = 0.0f; printf("\nPlease enter 5 numbers:\n"); int i; for (i = 0; i < count; i++) { printf("%2d> ", i + 1); scanf("%d", & numbers[i]); sum += numbers[i]; } average = (float) sum / count; printf("\nThe average of 5 numbers entered is: %f\n", average); return 0; }
Output
Please enter 5 numbers:
1> 5
2> 4
3> 4
4> 8
5> 7The average of 5 numbers entered is: 5.600000
Points to Remember
- An array is used to store a set of values of the same Data Type.
- An Array can be declared and initialized at the same time.