Array in C Program
In C programming, an array is a collection of elements of the same data type (e.g. int, float, char).
In C, array elements are stored in contiguous (together in sequence) memory locations.
The size of an array must be declared at the time of its creation and cannot be altered later.
C array has Zero-Based Indexing. Array indexing starts from 0.
Syntax of C array
xxxxxxxxxx
datatype arrayName[arraySize];
-
datatype
is the type of the data of array. -
arrayName
is the variable name of array. -
[arraySize]
is the size (number of elements) of the array.
Array Declaration
xxxxxxxxxx
int numArray[5];
Array Initialization
An array can be initialized at the time of declaration as shown in the following example:
xxxxxxxxxx
int numArray[5] = {1, 2, 3, 4, 5};
An array can be initialized later as shown in the following example:
xxxxxxxxxx
int numArray[5];
numArray[0] = 10;
numArray[1] = 20;
numArray[2] = 11;
numArray[3] = 42;
numArray[4] = 51;
However, size of an array can be omitted if the elements are assigned directly as shown in the following example:
xxxxxxxxxx
int numArray[] = {1, 2, 3, 4, 5};
Create an array of integers in C
In C programming, you can create an array of integers by declaring int
data type and initializing it with integer numbers as demonstrated in the following example:
Examples of array of integers
xxxxxxxxxx
#include <stdio.h>
int main() {
//Initialized at the time of declaration
int numArray[] = {5, 6, 4, 9};
printf("The third integer number is %d\n", numArray[2]);
//Initialized later
int intNumArray[5];
intNumArray[0] = 1; //adding array element
intNumArray[1] = 2;
intNumArray[2] = 3;
intNumArray[3] = 4;
intNumArray[4] = 5;
printf("The 2nd integer number is %d", intNumArray[1]);
return 0;
}
Output of the above example
The third integer number is 4 The 2nd integer number is 2
Example Explanation
-
int numArray[] = "5, 6, 4, 9";
: The line declares an array of integer numbers named 'numArray' and initializes the array with integer numbers. -
printf("The third number is %d\n", numArray[2]);
: It prints the third element (index 2) of the array. -
int intNumArray[5];
: The line declares an array of integers numbers named 'intNumArray'. -
intNumArray[0] = 1;
: The line add first element to the 'intNumArray' array.intNumArray[1] = 2; intNumArray[2] = 3; intNumArray[3] = 4; intNumArray[4] = 5;
add more integer numbers to the array. -
printf("The 2nd integer number is %d", intNumArray[1]);
: It prints the second element (index 1) of the array.
Create an array of chars in C
In C programming, you can create an array of chars by declaring char
data type and initializing it with string literal or character by character as demonstrated in the following example:
Examples of array of chars
xxxxxxxxxx
#include <stdio.h>
int main() {
//using string literal
char alphabet[] = "ABCDEF";
printf("The third char is %c\n", alphabet[2]);
//using character by character
char message[13] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
printf("The 8th char is %c", message[7]);
return 0;
}
Output of the above example
The third char is C The 8th char is W
Example Explanation
-
char alphabet[] = "ABCDEF";
: The line declares a char array named 'alphabet' and initializes the array with string literal "ABCDEF". -
printf("%c", alphabet[2]);
: It prints the third char element (index 2) of the array. -
char message[13] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
: The line declares a char array named 'message' and initializes the array character by character. A null terminator ('\0') is added at the end. It indicates the end of the string. -
printf("The 8th char is %c", message[7]);
: It prints the 8th char element (index 7) of the array.