Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Array in C: Declare, Initialize and Access Elements (With Examples)

7 min 18 sec read

Suppose you want to store the age of one student in memory then how do you perform this operation? It can be defined like this,

int age1 = 23;

Right?

Now suppose you want to store age of 5 students in memory. It can be defined like this,

    int age1 = 23;
    int age2 = 24;
    int age3 = 25;
    int age4 = 26;
    int age5 = 27;
    

Now suppose you want to store age of 1000 students in memory. You have to write

int age1 = 23;
    .
    .
    .
    .
    int age 100 = 40;

Can you write this 1000 times ?

and

Is it a good programming practice?

Since you are the programmer you have to think simplified. So C provides the best feature which is called as Arrays. With the help of Array in C, you have to write just a single line to store age of 1000 students.

What are Arrays?

    Arrays are used to store values of the same type.
    Arrays can store either int types at one time.
                         or
    Arrays can store float values at a time.
                        or 
    Similarly,
    Can store values of the same type say?
    

Syntax:-

    datatype variableName[size];
    Example :-
    int students[6];

Initialise array like this,

                       0    1     2
    int students[ ] = {12 , 13 , 14};  
    

0,1,2 are the indexes of each element in an array with the help of which you can access individual elements from the array.

You can access elements of array like this,

    students[2]; 
    Output:-
    14
    
    students[1];
    Output:-
    13
    

Example :-

    #include <stdio.h>
    int main()
    {
        int age[5];
        for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Enter age of Student " ) ;
            scanf ( "%d", &age[i] ) ;
        }
        /* Display Age of Students which you had taken as an input */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Student[%d] = %d ",i, age[i] ) ;
        }
    return 0;
    }
    

Output :-

    Enter age of Student 1
    Enter age of Student 3
    Enter age of Student 4
    Enter age of Student 5
    Enter age of Student 6
    Enter age of Student 7
    
    Student[0] = 1 
    Student[1] = 3 
    Student[2] = 4 
    Student[3] = 5 
    Student[4] = 6 
    Student[5] = 7
    

This program will first take 5 inputs from the user and store those inputs inside an array age. So this piece of program will take input from a user.

for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Enter age of Student " ) ;
            scanf ( "%d", &age[i] ) ;
        }
    
    

Now, After taking inputs from user this piece of program will display the individual elements inside an array using for loop

for (int i = 0 ; i <= 5 ; i++ )
        {
         printf ( "
Student[%d] = %d ",i, age[i] ) ;
        }
    
    

Example 2:-

#include <stdio.h>
    int main()
    {
        int age[5];
        int students[] = {12,13,14};
for(int i=0;i<=(sizeof(students)/sizeof(students[0]));i++)
        {
            printf ( "
Student[%d] = %d ",i, students[i] ) ;
        }
    return 0;
    }

Output :-

    Student[0] = 12 
    Student[1] = 13 
    Student[2] = 14 
    Student[3] = 0

This program initialises an array of students with 3 values. First element count starts with 0 index. ow the task of this program is to display each elements inside an array.

Inside for loop, you have to write one condition so that loop knows how many times it should execute the block of for loop. So to count the elements of the array you can use sizeof() operator.

sizeof(students) = 3 elements inside array * size of type of these array elements (int = 4 bytes ) in this case. = 3 *4 = 12

sizeof(students[0]) = size of first element of array with 0 index is 4 bytes.

(sizeof(students) / sizeof(students[0])) = 12/4 = 3

So this loop runs for 3 times.

That why the output is

    Student[0] = 12 
    Student[1] = 13 
    Student[2] = 14 
    Student[3] = 0
    

Pass array to Functions using Call by Value

#include <stdio.h>
    /* Accepts simple value from calling array.
    This is called array. */
    void displayStudentInfo (int agea)
    {
        printf("Age = %d 	", agea);
    }
    
    int main()
    {
        int age[] = {1,2,3,4,5,6};
        /* Take input from user */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            displayStudentInfo(age[i] );
        }
    
    return 0;
    }
    

Output :

Age = 1 Age = 2 Age = 3 Age = 4 Age = 5 Age = 6

In this program, we initialized an array age and applied a loop to it. So that we can retreive information from that array one by one. To do that we passed value at "i" index through displayStudentInfo function.

Pass array to Functions using Call by Reference

#include <stdio.h>
    /* Accepts value as pointer variable */
    void displayStudentInfo (int *age)
    {
        printf("Age = %d 	", *age);
    }
    
    
    /* Accepts simple value from calling array. This is called array. */
    int main()
    {
        int age[] = {1,2,3,4,5,6};
        /* Take input from user */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            displayStudentInfo( &age[i] );
        }
        return 0;
    }
    

Output :

Age = 1 Age = 2 Age = 3 Age = 4 Age = 5 Age = 6

In this program we initialised an array age and applied a loop to it . So that we can retrieve information from that array one by one. To do that we passed address at "i" index through displayStudentInfo function. And caught it using pointers.

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor