Home » Programming Languages » C Programs » Array Initialisation in C

Array Initialisation in C

Array Initialization in C can be done using multiple methods. We will see examples of the simple techniques to initialize arrays below:

Technique 1:

This is a very simple method to initialize arrays. We will declare and provide elements for the array in the program itself as shown below:

$ vim array.c

The above command creates a text file using the Vim editor and extension”.c” Next, we type the following program in the file and exit edit mode by pressing Esc. To save the file, we press “:wq” and the editor exits into the terminal.

#include <stdio.h>

int main(void) {
	int x[2], i;
	x[0] = 4;
	x[1] = 10;
	printf ("x[0] = %d, x[1] = %d \n", x[0], x[1]);
return 0;
}

In the above program, we have declared array “x” with a size of 2 elements. “x[0]” has “4” whereas “x[1]” has a value of 10.

Compilation of program:

$gcc -o array array.c

The above line of code compiles the program into an executable file. This is done with the help of the GNU Compiler Collection utility.

Execution and output:

$ ./array
x[0] = 4, x[1] = 10

As seen above, after executing the program, the output displayed on the terminal consists of the array elements “x[0]” and “x[1]”

Technique 2:

The below technique can also be used to initialize arrays. In this method, we provide the elements for the array in a single step during the declaration.

$vim array2.c

The above command creates a text file using the Vim editor and extension”.c” Next, we type the following program in the file and exit edit mode by pressing Esc. To save the file, we press “:wq” and the editor exits into the terminal.

#include <stdio.h>

int main(void) {
        int x[] = {4, 10};
        printf ("x[0] = %d, x[1] = %d \n", x[0], x[1]);
return 0;
}

In the above program, we have declared array “x” without a size limit on the elements. The elements are entered in curly braces with the order of the elements starting from the left. Next, the elements are printed using the “printf” function.

Compilation of program:

$ gcc -o array2 array2.c

The above line of code compiles the program into an executable file. This is done with the help of the GNU Compiler Collection utility.

Execution and output:

$ ./array2
x[0] = 4, x[1] = 10

As seen above, after executing the program, the output displayed on the terminal consists of the array elements “x[0]” and “x[1]” according to the “printf” function.

Technique 3:

The below technique is similar to Technique 2 with a slight change. We declare the size of the array during the initialization.

$ vim array3.c

The above command creates a text file using the Vim editor and extension”.c” Next, we type the following program in the file and exit edit mode by pressing Esc. To save the file, we press “:wq” and the editor exits into the terminal.

#include <stdio.h>

int main(void) {
        int x[2] = {4, 10};
        printf ("x[0] = %d, x[1] = %d \n", x[0], x[1]);
return 0;
}

In the above program, we have declared array “x” with a size of 2 elements. The elements are entered in curly braces with the order of the elements starting from the left. Next, the elements are printed using the “printf” function.

Compilation of the program:

$ gcc -o array3 array3.c

The above line of code compiles the program into an executable file. This is done with the help of the GNU Compiler Collection utility.

Execution and Output:

$ ./array3
x[0] = 4, x[1] = 10

As seen above, after executing the program, the output displayed on the terminal consists of the array elements “x[0]” and “x[1]” according to the “printf” function.

Technique 4:

To accept input from users for the array, we use the following method:

$ vim array4.c

The above command creates a text file using the Vim editor and extension”.c” Next, we type the following program in the file and exit edit mode by pressing Esc. To save the file, we press “:wq” and the editor exits into the terminal.

#include <stdio.h>

int main(void) {
        int x[2], i;
        printf("Enter values to take from scanf : \n");
        for (i=0; i < 2; i++) {
                scanf("%d", &x[i]);
        }
        printf ("x[0] = %d, x[1] = %d \n", x[0], x[1]);

        return 0;
}

In the above program, we have declared array “x” with a size of 2 elements. The next step is accepting the elements from the user. The for loop will execute till the condition of increment is fulfilled. The “scanf” function will accept the inputs and add the elements to the array. Next, the elements are printed using the “printf” function.

Compiling the program:

$ gcc -o array4 array4.c

The above line of code compiles the program into an executable file. This is done with the help of the GNU Compiler Collection utility.

Execution and Output:

$ ./array4
Enter values to take from scanf : 
4
10
x[0] = 4, x[1] = 10

As seen above, after executing the program, the program asks for the user to input the elements for the array. Once the last element is entered, the program outputs the array of elements as shown above.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment