Array Declaration in C Language With Example

 

Before discussing Array declaration in C, first of all, let us look at the characteristic features of an array.

  • Array is a data structure storing a group of elements, all of which are of the same data type.
  • All the elements of an array share the same name, and they are distinguished from one another with the help of an index.
  • Random access to every element using a numeric index (subscript).
  • A simple data structure, used for decades, which is extremely useful.
  • Abstract Data type (ADT) list is frequently associated with the array data structure.

The declaration of an array is just like any variable declaration with additional size part, indicating the number of elements of the array. Like other variables, arrays must be declared at the beginning of a function.

The declaration specifies the base type of the array, its name, and its size or dimension.

Syntax for Array Declaration in C Language

data-type array_name [constant-size];
Data-type refers to the type of elements you want to store
Constant-size is the number of elements

The following are some of the declarations for arrays:

int char [80];
float farr [500];
static int iarr [80];
char charray [40];

There are two restrictions for using arrays in C:

  • The amount of storage for a declared array has to be specified at compile time before execution. This means that an array has a fixed size.
  • The data type of an array applies uniformly to all the elements; for this reason, an array is called a homogeneous data structure.

Size Specification

The size of an array should be declared using symbolic constant rather a fixed integer quantity (The subscript used for the individual element is of are integer quantity). The use of a symbolic constant makes it easier to modify a program that uses an array. All reference to maximize the array size can be altered simply by changing the value of the symbolic constant.

To declare size as 50 use the following symbolic constant, SIZE, defined:

#define SIZE 50

The following example shows how to declare and read values in an array to store marks of the students of a class.

Program to declare and read values in an array and display them.

/* Program to read values in an array*/

# include < stdio.h >

# define SIZE 5 /* SIZE is a symbolic constant */

main ( )

{

int i = 0; /* Loop variable */

int stud_marks[SIZE]; /* array declaration */

/* enter the values of the elements */

for( i = 0;i<SIZE;i++)

{

printf (“Element no. =%d”,i+1);

printf(“ Enter the value of the element:”);

scanf(“%d”,&amp;amp;stud_marks[i]);

}

printf(“\nFollowing are the values stored in the corresponding array elements: \n\n”);

for( i = 0; i<SIZE;i++)

{

printf(“Value stored in a[%d] is %d\n”i, stud_marks[i]);

}

}

OUTPUT

Element no. = 1 Enter the value of the element = 11
Element no. = 2 Enter the value of the element = 12
Element no. = 3 Enter the value of the element = 13
Element no. = 4 Enter the value of the element = 14
Element no. = 5 Enter the value of the element = 15

Following are the values stored in the corresponding array elements:

Value stored in a[0] is 11
Value stored in a[1] is 12
Value stored in a[2] is 13
Value stored in a[3] is 14
Value stored in a[4] is 15

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: