Microcontroller Embedded C Programming Lecture 37| Storage class specifiers in ‘C’

 

Storage class specifiers in ‘C’

 

 

 

Type Storage Classes in ‘C’ Language decides:

  • Scope of a variable
  • Visibility of a variable or function 
  • Lifetime of a variable

Scope, visibility, and lifetime are features of a variable. These features can be modified by using storage class specifiers.

 

2 widely used storage class specifiers in ā€˜Cā€™

 

The ‘static’ Storage Class Specifier

Now, let’s first understand the Static keyword in C. We will understand this by using an example. 

#include<stdio.h>

/*this is a function prototype*/
void myFun1(void);

int main()

{
   myFun1();
   myFun1();
   myFun1();
   myFun1();
   return 0;
}

void myFun1(void)
{
   int count = 0;
   count = count +1;
   printf("This function executed %d time(s)\n", count);
}

Static storage class specifier in ā€˜Cā€™ example

Here I have a function called main()

Now let me create one more function called myFun1, which doesn’t return anything and doesn’t take any arguments. All are void. So, void myFun1(void) And now, let’s give the prototype of that function and end this statement with a semicolon. 

This program aims to count how many times myFun1 is called from the main function. Call this function from the main couple of times. And I’m calling myFun1 4 times, as shown above.

And this function has to print how many times this function gets called. For that, to count, let me use one variable int count. Now let me initialize this to 0 and increment the count by 1, count + 1. So, whenever this function is called, the count will be incremented by 1. 

After that, I’m going to print that count value. Printf(“This function executed %d time(s)\n”); after that, give the argument that is this variable name count. The goal of this application is to print how many times this function gets executed. 

 

Let’s run the program. Look at Figure 1, what’s happening here? Everywhere it says that This function is executed 1 time, 1 time, 1 time. But our goal is to count how many times this function gets executed. 

Figure 2. Output of the Program
Figure 1. Output of the Program

 

First, analyze what’s happening in this program. Let’s start with the main because execution starts with the main. Inside the main body, so we have first there is a call to myFun1. So, control comes here(line 29). 

And when it comes here, a local variable is created and initialized to 0. After that, 0 is incremented by 1, and then the value is stored back into the variable count. The count is 1. And printf prints the value that is 1. 

After that, this function returns. The moment the execution control goes out of this function, what happens is that int count = 0; this local variable dies. The memory location associated with that variable is removed, and the value is lost. So, the int count =0 variable loses its existence. 

And then, the function returns to main() again, and after that, there is one more call to the same function(myFun1). And the control again comes to the main function(line 29), again the variable count is created, and it is initialized to 0, and again that 0 is incremented by 1, and again it prints 1 here. So, it happens like that. 

But, our goal is to count. We don’t want the count variable to die. We want this variable to hold its last value across multiple function calls.

How do you achieve that?

This problem can be easily solved by removing the int count =0 variable from local space and moving that variable to global space. So now, the count variable is moved to global space, so global variables will not die until you terminate the application. So, their lifetime is until you terminate the application, the global variable will be there, and their scope is visible to all the functions. 

#include<stdio.h>

/*this is a function prototype*/
void myFun1(void);
int count = 0;
int main()

{
   myFun1();
   myFun1();
   myFun1();
   myFun1();
   return 0;
}

void myFun1(void)
{
  count = count +1;
  printf("This function executed %d time(s)\n", count);
}

Modified program

Let’s run once again. 

Figure 4. Output
Figure 2. Output

 

Now, the problem is solved. This function is executed 1 time, 2 times, 3 times, 4 times. So, it is now correctly printed. So, this is what we expected. 

But, there is a small problem. The problem is now you made this variable public. Any function in the program can modify this variable. So, even this variable can be modified by some other file of the project. So, this is completely public to all the functions of your project. That is not good.

Now our requirement for this program is, we want a global variable that is private to a function. We want a private variable that does not lose its existence even if the execution control goes out of the scope of that variable. 

 

In order to achieve this requirement, that is we want a global variable but it should be private to a function. So, this can be achieved in ‘C’ by using a storage class specifier called static. 

Let’s see how to use that. Let’s go back to the function. Now again remove int count =0 this variable definition from global space, and let’s move that into the local space again. Use the static keyword here. static int count = 0; Now, the count is a global variable, but it is private to the myFun1

#include<stdio.h>

/*this is a function prototype*/
void myFun1(void);

int main()
{
  myFun1();
  myFun1();
  myFun1();
  myFun1();
  return 0;
}

void myFun1(void)
{
  static int count = 0;
  count = count +1;
  printf("This function executed %d time(s)\n", count);
}

Used static storage class specifiers in c

 

Let’s run this program to see how it goes. 

You see Figure 3; we got the expected result. But, in this case, this count variable is accessible only from myFun1, not from other functions.

Figure 6. Output
Figure 3. Output

 

And also, remember that the count variable will not die even if the execution control goes out of this function. The scope of this variable is now global, but visibility is only for this function. So, we will understand more about static storage class specifiers in the following article.

 

Get Microcontroller Embedded C Programming Full Course on Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.