Introduction
Fundamentals
C Flow Control
C Functions
Arrays in C Language With Examples
In C programming language, an array is a collection of similar-type data items stored at adjacent memory locations, which is a derived data type so it can contain data types such as int, char, double, float, and etc. In this tutorial, we will cover an introduction of arrays in C Language.
An array is a collection of data of the same type (and therefore, the same size) stored in consecutive memory cells under one name. In C, an element of an array (i.e., an individual data item) is referred by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets.
Array declaration syntax
typeName ArrayName[size] = { list of values }
How to Take Array Input in C
Inside the loop we are displaying a message to the user to enter the values. All the input values are stored in the corresponding array elements using scanf function.
for (x=0; x<4;x++) {
printf("Enter number %d", (x+1));
scanf("%d", &num[x]);
}
Reading out data from an array
Suppose, if we want to display the elements of the array then we can use the for loop in C like this.
for (x=0; x<4;x++) {
printf("num[%d]", num[x]);
}
Example of Array:
#include <stdio.h>
int main() {
// declare an array and a loop variable.
int a[5], i;
printf("Enter five numbers:");
// read each number from the user
for (i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("The array contains:");
// print all the numbers in the array
for (i = 0; i < 5; i++) {
printf("%d ", a[i]);
}
return 0;
}
Output :
Enter five numbers: The array contains: 10 25 30 26 40