How to create an array in C

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

  • 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

Array Initialization

An array can be initialized at the time of declaration as shown in the following example:

An array can be initialized later as shown in the following example:

However, size of an array can be omitted if the elements are assigned directly as shown in the following example:

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
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
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.

Live Code Playground

In the following C code editor, you can practice by creating more arrays of different data types. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output