static keyword in c.

Hello coder! Welcome back!

Today we are going to look into-

  • What is a static keyword?
  • Why do we use it in a program?
To explain this concept lets look into the example-

So you can see that I have created a project named ' my_example'. In main.c file I'm calling a function named 'increment' trice.

Increment function is present in inc.c file. let's look at what program I have written there-

let me explain the code, I'm passing ASCII value of 'A' to count variable and then incrementing it by one.

According to main.c file, we are calling increment function trice so we expect the value to be incremented trice and it should produce 68, the equivalent character is 'D'

let's look at the output-

Its printing 'B'.
Let me explain what actually happening inside. We know that variables that are declared inside a function are automatic variables that mean they are going to destroy after the execution of that function.
I have mentioned in-depth about automatic variables click here
So second time when I'm again calling increment function, the count value is initialized to ASCII value 65 and incremented it to 66 and returning the value 66.
Again, when it is called the variable is destroyed, as it is an automatic variable and again gets initialized to 65 and gets incremented to 66, and then finally it prints 'B'.

So I need 68 equivalent ASCII character to be printed i.e, I want it to perform increment operation. What can be done?
There are two ways-

  • Either make the 'count'  variable as a global variable. 
  • or, use static keyword. 
I will explain both the methods along with their drawbacks.

When I declare it as a global variable. I'm able to fetch the required result.

We know the meaning of the global variable, which means that the 'count' variable is accessible throughout the project. As we know that global variables are not destroyed.

let's use this global variable in main.c file and look whether we can fetch the value or not.

So I'm using count variable in main.c file and incrementing it by one and storing that value in variable 'value' and printing it. So you can see that after 'D' 'E' variable is printed correctly on the command prompt. 

Here lies the biggest disadvantage to global variables. They become visible to all the files inside that project. Nothing remains private. To overcome this disadvantage we use static keyword.

Now let's look at what happens when I declare the global variable as static -



When I'm trying to access the static global variable in main.c file it shoots an error saying that undefined reference to 'count'. This means we cant access the variable. It's not visible throughout the project.

When I remove the 'count' variable from main.c file, I can fetch the correct answer-




Still, I have declared the count variable as global. If I don't want this variable to be visible even in inc.c file then declare this variable inside the increment function with the static keyword as shown below. Yet we can achieve the desired result.

Hence we can say that static variables can retain the value. 

There are few rules while declaring the static variables.

  • Static modifiers are always initialized with a constant value not with a variable name. 
Let me explain the above statement with a program

So I declared a variable 'count1' and made static variable get initialized with count1 instead of a constant value. You can see that it shoots an error.

Always declare static variables with constant values.

That's it for this blog!!  I tried my best to explain the concept. If any doubts drop a comment.

Happy learning!!!

Comments

Popular post