Mbed Technologies
  Learn, Develop, Compete


    Introduction: Welcome to Mbed Blogs!. The periodically submitted blogs in this webpage will discuss more interesting topics on Embedded C programming. Each blog is written with specific focus, interest, to experiment and dip into different important features of embedded C programming. We encourage students and professionals to follow the steps exactly written in the blogs, write, modify and compile the programs to gain maximum knowledge out of it. The blogs will be equally useful to both fresh engineering graduates as well as experienced software/firmware professionals.

    Blog Title: In this blog we will study and experiment about the static storage class variable in C programming and its usage in embedded applications.

by Admin, Posted on January 24, 2018 at 9:00 PM


     Blog Background     
 Compilation Requirement: Linux Environment. Any Linux distribution will be suitable for executing the steps as explained.
 Learning Requirement: Basic and Mandatory Requirement of C Programming.
 Audience Eligibility: Undergraduate engineering students or experienced software/firmware professionals.
 Difficulty Level: Low to Medium.

  Step 1: To begin with, Let's write a simple c program and name it as static1.c where we will be having a function that has one variable declared as normal variable and another one declared as static variable. We increment both the variables inside the function. The function has been called several times from main() to illustrate the impact it makes on both normal and static variable.


#include "stdio.h"

void stat_func();

int main()
{
    int count = 0;
    for(count = 0; count < 4; count++)
    {
        printf("Now at iteration %d\n",count);
        printf("=========================\n");
        stat_func();
    }
    return 0;
}

void stat_func()
{
    int loc_var = 10;
    static int stat_var = 10;
    stat_var++;
    loc_var++;
    printf("Now loc_var value is %d \n",loc_var);
    printf("Now stat_var value is %d \n",stat_var);
}
    

  Step 2: Lets compile and analyze the output of the static1.c program we wrote in previous step. The output is shown below. The function has been called 4times from main using a for loop. The normal variable value has been reset to zero each time the function is called. Hence its incremented value is always 11. The static variable keeps incrementing by 1 each time the function has been called. So the final incremented value is 14 for the static variable.


# ./a.out
Now at iteration 0
=========================
Now loc_var value is 11
Now stat_var value is 11
Now at iteration 1
=========================
Now loc_var value is 11
Now stat_var value is 12
Now at iteration 2
=========================
Now loc_var value is 11
Now stat_var value is 13
Now at iteration 3
=========================
Now loc_var value is 11
Now stat_var value is 14


  Step 3: Static variables are allocated memory in data segment, not stack segment. To illustrate this point lets modify the program static1.c and name the modified program as static2.c as shown below.


#include "stdio.h"

void stat_func();

int main()
{
    int count = 0;
    for(count = 0; count < 4; count++)
    {
        printf("Now at iteration %d\n",count);
        printf("=========================\n");
        stat_func();
    }
    return 0;
}

void stat_func()
{
    int loc_var = 10;
    loc_var++;
    printf("Now loc_var value is %d \n",loc_var);
}



  Step 4: Compile static2.c and execute size command, note down the memory allocation in data segment which is 296bytes.


#size ./a.out
text    data     bss     dec     hex filename
 957     296       4    1257     4e9 ./a.out
    

  Step 5: In static2.c get the static variable back in the program and name the modified program as static3.c.


#include "stdio.h"

void stat_func();

int main()
{
    int count = 0;
    for(count = 0; count < 4; count++)
    {
        printf("Now at iteration %d\n",count);
        printf("=========================\n");
        stat_func();
    }
    return 0;
}

void stat_func()
{
    int loc_var = 10;
    static int stat_var = 10;
    stat_var++;
    loc_var++;
    printf("Now loc_var value is %d \n",loc_var);
    printf("Now stat_var value is %d \n",stat_var);
}

    

  Step 6 Compile static3.c and the compilation output is as shown below. Notice from the output that data segment size has been changed from 296bytes to 300bytes. What it means is that the static variable we introduced in the last step has been allocated in memory in data segment.


#size ./a.out
text    data     bss     dec     hex filename
1037     300       4    1341     53d ./a.out

  Step 7:Static variables are unavailable to functions defined in other files, even if these functions attempt to use the extern storage class keyword to access them. To illustrate this let's create two new files and name them as static4.c and static5.c as shown below.


//static4.c
#include "stdio.h"

int func(int );
static int test = 90;

int main()
{
    func(4);
    return 0;
}

//static5.c
#include "stdio.h"

extern int test;

int func(int a)
{
    return a + test;
}


  Step 8: Now compile the programs static4.c and static5.c together. Look at the output below. The compilation throws out error as test static variable in static4.c could not be accessed in static5.c


# gcc static4.c static5.c
/tmp/cchDmzDQ.o: In function `func':
static5.c:(.text+0x8): undefined reference to `test'
static5.c:(.text+0xc): undefined reference to `test'
collect2: ld returned 1 exit status

  Step 9: Functions declared as static are unavailable to other files, even if these files attempt to use the extern storage class keyword to access them.To illustrate this lets create two new files and name them as static6.c and static7.c as shown below.


//static6.c
#include "stdio.h"

int func(int );

int main()
{
    func(4);
    return 0;
}

//static7.c
#include "stdio.h"

static int func(int a)
{
    return a+2;
}

  Step 10: Now compile the programs static6.c and static7.c together. Look at the output below. The compilation throws out error as func function which is declared as static in static7.c could not be accessed in static6.c

# gcc static6.c static7.c
/tmp/ccT2tv8i.o: In function `main':
static6.c:(.text+0x8): undefined reference to `func'
collect2: ld returned 1 exit status
  Step 11: For the last step of this blog, let's analyze that like global variables static variables are initialized as 0 if not initialized explicitly. To illustrate this lets write a simple program static8.c with a static variable declared without initialization as shown below.


#include "stdio.h"

static int var;

int main()
{
    printf("The value of static variable var is %d\n",var);
    return 0;
}

  Step 12: Now compile the static8.c program. The output is shown below. Notice that the value of static variable var is set to 0 by default though it is not initialized explicitly in the program.


//static8.c program output
# ./a.out
The value of static variable var is 0