Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Global Array

The array declared outside the function is known as global array.
Generally, global array is defined at the beginning of the program.
Global array has global scope. That is they can be used anywhere in any function of the program.

Example

#include<stdio.h>
void extraFunction();
 
int numbers[5] = {10,20,30,40,50};  // global array
int main()
{
    int i;
	 
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
    extraFunction();
    return 0;
}
 
void extraFunction()
{
    int i;
    printf("\n\n Inside Extra Function");
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
}

If we did not initialize the global array at the time of declaration, then all its elements are assigned with zero.

Example

#include <stdio.h>
int numbers[5];  // global array
int main()
{
    int i;
	
    printf("\n Elements of an array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
	
    return 0;
}

Output

Elements of an array numbers are ::
0
0
0
0
0

Static Array

An array declared with the static keyword is known as a static array.
If we did not initialize the static array at the time of declaration, then all their elements are assigned with zero.

Example for static array

#include <stdio.h>
int main()
{
    static int numbers[5];  // static array
    int i;
	
    printf("\n Elements of an static array numbers are :: ");
    for(i=0;i<5;i++)
    {
        printf("\n %d",numbers[i]);
    }
	
    return 0;
}

Output

Elements of an static array numbers are ::
0
0
0
0
0

 

Leave a reply