What is an array? How to declare one dimensional array? In which case it is
used? OR Explain how to initialize one dimensional array with example.
Answers
Array:
An array is a list of variables on a similar data type present in continuous memory location.
• 1D Array declaration Syntax is as follows :
datatype array name [size]
For example, int buchiage[ 15 ]
• Initialization:
An array can be initialized in two ways, which are as follows...
- Compile time initialization
- Runtime initialization
1. Compile time initialization:
In compile time initialization array elements can be initialized at the time of Array declaration.
• Here Array size are may or may not be defined
EX:- int varshumarks[ ] = { 100, 100, 100, 100, 100 }
• Following is the C program on compile time initialization :
#include<stdio.h>
int main ( )
{
int pandumarks[ 5 ] = { 50, 60, 70, 60, 50 };
int i;
printf ( "elements of the array are" );
for ( i=0; i<5; i++)
printf ( "%d", a[ i ] );
}
2. Run time initialization:
In Run time initialization for loop is used initialized the variables of Array
• Following is the C program on Run time initialization:
#include<stdio.h>
main ( )
{
int a[ 5 ],i;
printf ( "enter 5 elements" );
for ( i=0; i<5; i++ )
scanf( "%d", &a[ i ] );
printf( "elements of the array are" );
for ( i=0; i<5; i++ )
printf( "%d", a[ i ] );
}