Initialize One dimensional 1D array in C Language
In this tutorial section we will learn how to initialize 1 Dimensional array in C Language?.
What is 1d array initialization?
Initialization of array in C language is one of the basic and fundamental step.
The question arises in our mind is "how to initialize array in C Language".
before continue to array initialization let learn what is array and initialization.
Array: " Array are a group of same or related data values stored contiguously next to one another in the memory."
Initialization: "The process of assigning some values to one dimensional array in C language is called 1d array initialization"
When the array variable is declared or some memory is allocated to the variable then we need to assign some initial value to the variable.
The elements of the array contain some garbage value when the array is declared or allocated memory. So, Programmer need to initialize the array to some meaningful initial value. There are multiple ways in which we can initialize an array in C.
How to Initialize an Array in C?
In C programming we need to be initialize every variable or data structure.
{ } opening and closing curly braces is used to initialize array,which may contains expression seperated by commas. In C programming one-dimensional array declared like.
type array_name[array_size];
The "array_size" should be greater than zero and constant. Programmer can give any name to array in place of array_name which should follow naming rule for variable in C.
"type" is data type used to declare array variable, type of an array can be int,char,float etc.
In C an array can be initialize in two ways:
i. Compile Time:
Compile time initialization of array means programmer provide the value for the array in the code at the time of array creation,means user has to enter the details in the program itself.
Compile time initialization of an array is same as normal variable initialization.
ii. Runtime:
During program's execution or rum time user can create an array as per the need. User can decides the size and values of an array while program is running. Using runtime initialization user can get a chance of accepting or entering different values during program execution.
An array can also be initialized at runtime using scanf() function or loops.
Methods to Initialize Arrays in C programming
Following are some of the methods to initialize Array in C programming.
1. Array initialization with Declaration.
2.Array initialization with Declaration without Size.
3.Array initialization after Declaration.(Using Loops)
4.Array initialization by specifying size.
5.Getting input from user.
6.Initialization from brace-enclosed Lists.
7. Using Designated Initializers(for GCC compilers Only)
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.