nitialize 1d one dimensional array in C Programming

  Array Initialization Methods in C programming.
Given Diagram is showing the different array initialization method used in C programming.
Let us Learn in detail one by one.
1. Array initialization with Declaration:
is the way to fill an array with values at the time of array creation.User can provide values enclosed in curly braces{}.
This method allows user to initialize the array at the time of declaration.We use an initializer
{   } list to initialize multiple values or elements of the array. An initializer list is the list of values enclosed within curly braces {  } separated by a comma(,).

Syntax for array initialization.
data_type  array_name [size] = {value1, value2, ... valueN};
Example:
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
{
 // array initialization using initializer list
 int no[5] = { 110, 200, 130, 400, 50 };
 printf("\n Value of array at third place=%d",no[2]);
 return(0);
}


2. Array initialization with Declaration without size:

This method is some what same as above method.
At the time of declaration an array can be initialized by skiping the size of the array.The compiler can automatically calculate the size of an array. The total size of an array is equal to the number of elements present in an array.The Compiler calculate size of the array automatically .

syntax:
data_type array_name[] = {v1,v2,v3,v4,v5};

The size of the above arrays is 5 which is automatically calculated by the compiler.
In the array declaration syntax given above, the compiler will allocate a size for an array is equal to the number of values or elements.
The following syntax can be used to declare and initialize an array at the same time.

// initialize an array at the time of declaration without size.
int emp_salary[] = {8000, 12000, 13000, 4000, 5000}

In the above syntax, we have declared and created an array of 5 elements without specifying the size. the compiler will allocate a size of 5 elements to the array emp_salary according to the number of integer elements present in the curly braces{}.
Following is the example for the same.

 // Array initialization with Declaration without size
#include<stdio.h>
int main()
{
 int emp_salary[] = {8000, 12000, 13000, 4000, 5000};
 printf("\n Third salary =%d",emp_salary[2]);
 return(0);
}
output:
Third salary=13000


3.Array initialization after Declaration.(Using Loops)

User can initialize an array using a for loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. This initialization method is useful when user want to calculate values runtime or dynamically.
The following syntax uses a “for loop” to initialize the array elements. This is the most common used way to initialize an array in C.

#include<stdio.h>
int main()
{
// declare an array.
int salary[5];
// initialize array using a "for" loop.
int i;
for(int i = 0; i < 5; i++)
 {
salary[i] = 1300 * i;
}
return(0);
}
Explanation:
The array salary will have the values
salary = {0, 1300,2600,3900, 5200}
In the above program, the array salary of size 5 is declared first.
The array is then initialized using a for loop that iterates over the array starting from index 0 to less than 5.


4.Array initialization by specifying size.

In C Programming user can create an array by specifying the size and assigning array elements at the time of declaration. This method of array creation and initialization is different from the previous methods. Here, in this method of initialization if the number of initialized values or elements is less than the size of the array, then the compiler automatically initialize the rest of elements to 0.

following is the syntax to understand this.
// declare an array by specifying size and
// initializing at the time of declaration
int salary[4] = {100, 200, 300, 400};
int salary2[4] = {100, 200, 300};

In the above array syntax, salary is an array of size 4 with all four values initialized. Whereas, salary2 is an array of size 4 with only three of its elements initialized. The remaining one elements of the second array will be initialized to 0 by the compiler.


5.Geting Input from User.

/* Input or enter the values from the user and store it in array using for loop.
Example. input and store value in array */
#include<stdio.h>
int main()
{
 int salary[5];
 int i;
 printf("\n Enter five different salaries\n");
 for(i = 0; i < 5; i++)
  scanf("%d",&salary[i]);
 printf("\n The Salaries Are..");
 for(i = 0; i < 5; i++)
  printf("%d\n",salary[i]);
 return 0;
}


6.Initialization from brace-enclosed Lists..

int salary[5] = {[4]=5000,[0]=1200,2560,3200,4500};
In the above array initialization and declaration method, we have initialized index-4 with value 5000 and index - 0 with the value 1200 and the rest indexes 1,2,3 with values 2560,3200,4500 respectively.
This type of array initialization is extremely useful in C programming as this method is flexible to specify the index numbers and assign value in brace list itself.


7. Using Designated Initializers(for GCC compilers Only)

This type of initialization method allows initializing specific array elements by specifying their indices.
Example.
int salary[4]=[0] 1500,[1] 2000,[2] 5400,[3] 6500;
This initializes salary elements directly by specifying their index and values.


Previous Topic:-->> Declare 1D array in C || Next topic:-->>Access element 1D array.