Static and Automatic Initialization of an Array in C

This C Tutorial Explains Static and Automatic Initialization of an Array in C with Example(s).

Let’s recollect concept of automatic and static initialization from declaring and initializing a scalar variable,

    auto int i;    /* auto keyword optional */

can be written as:

    int i;    /* by default automatic */

What’s in ‘i’? When a variable’s declared automatic but not initialized, it contains garbage. Now we initialize integer ‘i’

    i = 10;    /* now 'i' contains value 10 */

What’s if a variable’s declared static?

advertisement
advertisement
/* 
 * static_ini1.c -- program shows use of static declaration and
 * initialization
 */
#include <stdio.h>
 
int main(void)
{
    static int i;    /* 'i' declared static */
 
    printf("Default Value of \"static variable i\" is %d\n", i);
    return 0;
}

Output is as below:

Default Value of "static variable i" is 0

Here, static variable ‘i’, by default 0, didn’t seem to be of much use. Let’s modify the above program to use static variable in a function,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
/* 
 * static_ini2.c -- program shows use of static declaration and
 * initialization
 */
#include <stdio.h>
void count(int);
 
int main(void)
{
    int i;    /* 'i' declared automatic */
 
    for (i = 1; i <= 5; i++)
        count(i);
 
    return 0;
}
 
void count(int iteration)
{
    static int i;    /* 'i' initialized once */
 
    printf("Value of \"static int i\" in count(%d) is %d\n",
           iteration, ++i);
}

Output follows:

Value of "static int i" in count(1) is 1
Value of "static int i" in count(2) is 2
Value of "static int i" in count(3) is 3
Value of "static int i" in count(4) is 4
Value of "static int i" in count(5) is 5

Notice that static variable ‘i’, with default value 0, retained its last modified value each time function ‘count()’ was called and exited. Thus, static variable is declared once and its life time is entire execution of the program. It’s neither created anew each time ‘count()’ was entered nor did it destroy when ‘count()’ exited. Therefore, a significant amount of time is saved in using static variable.

advertisement

Let’s turn to consider declaration and initialization of one-dimensional array, for example,

    int children[5] = {234, 11, 453, 1212, 6663};

which has assignments happening as below,

    children[0] = 234;
    children[1] = 11;
    children[2] = 453;
    children[3] = 1212;
    children[4] = 6663;

Since an array usually have more elements, declaring an array to be automatic and initialized it within the function which needs to be called repeatedly wastes significant amount of time in each function call. Therefore, declaring an array to be static and initialized it within function which might be called several times is more efficient. For example,

advertisement
/* static_arr.c -- program shows an array declared static */
#include <stdio.h>
void update(int);
 
int main(void)
{
    update(1);
    update(0);
    return 0;
}
 
void update(int FLAG)
{
    static int count[10] = {12, 34, 45, 123, 1, 3, 56, 90, 88, 100};
    int i;
 
    if (FLAG) {
        for(i = 0; i < 10; i++)
            count[i] += 5;
        printf("Updated data!\n");
    }
    else {
        printf("No need!\n");
    }
}

Output follows:

Updated data!
No need!

Analysing the output of the above program is an easy task! When ‘update(1)’ was called first time, if condition in the called function accounted for TRUE and thus array ‘count[]’ updated by incrementing each value in array by 5. Interesting point here is that array ‘count[]’ was declared and initialized statically just once in the called function ‘update()’ and therefore it retained its last modified values each time function ‘update()’ entered. When ‘update(0)’ was called else component of if construct executed and displayed massage “No need!”.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.