Skip to content
Home » All Posts » Explore the Role of the Static Keyword in C

Explore the Role of the Static Keyword in C

static keyword in c

Introduction

The static keyword in C provides control over the scope and lifetime of variables and functions. It has different behaviors when used in different contexts. The static keyword can generally be used in the following contexts:

  • static variable inside a function
  • static global variable
  • static function

We will explore the effects of static keyword in C used in the above contexts.

Static Variable Inside a Function

When a function declares a static local variable, it means that the declaration happens only “once“. It means that it will have extended life time even after the function returns. In other words, the value of this local static variable will remain the same at subsequent calls to this function. For example:

void promptuser(void)
{
	static int mode = 0;
	if (mode == 0)
	{
		int myint = 0;
		printf("enter an integer input\n");
		scanf("%d", &myint);
		printf("you entered an integer value %d\n", myint);
	}
	else if (mode == 1)
	{
		float myfloat = 0.0;
		printf("enter a float input\n");
		scanf("%f", &myfloat);
		printf("you entered a float value %.2f\n", myfloat);
	}
	else if (mode == 2)
	{
		char mychar[128] = {0};
		printf("enter a string input\n");
		scanf("%s", &mychar[0]);
		printf("you entered a string value %s", mychar);
	}
	else
	{
		printf("unsupported mode\n");
		return;
	}
	mode++;
}
int main(void)
{
	promptuser();
	promptuser();
	promptuser();
	return 0;
}

In the above example, int mode is declared as static, which means its value will retain at subsequent calls to the same function, promptuser(), without being redeclared and initialized to 0. This static variable basically alters the behavior of this function in subsequent calls to it without using any input arguments.

The first call of the function prompts the user to enter an integer value, then static variable mode is incremented by 1 (mode++). The second call of the function prompts the user to enter a flat value instead because the last value of mode is 1. The static variable of mode is then incremented by 1 again. At the third call, it now expects user to input a string and also increments mode.

At this point on, this function will always print “unsupported mode” at subsequent calls, because mode will be 3 at this point. This behavior will stay like this until the program exits.

Static Global Variable

When static keyword is used to declare a global variable outside of a function, it basically limits the scope of this variable to be accessible within this source file. Other source files (if you have) cannot access static variable declared inside other source files. For example.

If a global variable is declared without static, the same variable can be accessed from else where (from other source files) via the extern keyword.

main_file.c:

#include <stdio.h>
#include "other_file.h"

int myglobalint = 300;
/* static will cause other_file.c to have compilation error */
// static int myglobalint = 300; 

int main(void)
{
	printf("myglobalint is %d\n",
		myglobalint);
	return 0;
}

other_file.h:

void somefunc(void);

other_file.c:

#include "other_file.h"
extern int myglobalint;

void somefunc(void)
{
	printf("myglobalint is %d\n",
		myglobalint);
}

In the example above, myglobalint is declared without a static keyword, so it is possible to access this variable from another source file (other_file.c) using the extern keyword, which tells “other_file.c” that the varialbe myglobalint is declared somewhere.

If myglobalint is declared with static keyword, then the example above will result in compilation error on “extern int myglobalint” line because the compiler will not be able to find the original declaration of it. Or I should say it will be hidden by the static keyword in C.

Static Function

When a function declares with a static keyword, it also means that this function is only accessible within the source file that defines it. Functions in other source files cannot access this static function declared in a different source file. For example:

main_file.c:

#include <stdio.h>
#include "other_file.h"

int main(void)
{
	somefunc();
	return 0;
}

other_file.h:

void somefunc(void);
/* we don't declare helper1 and helper2 here
 * because they are not be to made
 * accessible outside of this source 
 * file
 */

other_file.c:

#include "other_file.h"
static int helper1(void);
static int helper2(void);
void somefunc(void)
{
	printf("sum of helpers = %d\n",
		(helper1() + helper2()));
}

static int helper1(void)
{
	return 100;
}
static int helper2(void)
{
	return 300;
}

In the above example, the function somefunc() located in other_file.c is called from the main() function. This function relies on 2 other functions (helper1 and helper2) to complete its task. These 2 helper functions are meant to be used locally by the function somefunc() and not meant to be called by anything outside of it. So, it is best that we declare these 2 functions as static to limit their scope.

Best Practices

Now you know what static does, what is the best practice here?

  • Use static variable inside a function when your logic requires it. There is not much best practice for this context
  • When creating a new C and header file pairs, think about what kind of functions to expose (ones that will have a declaration in the header files). Any other functions acting as helper functions shall be declared as static in the source file (not header file).
  • If a function is not meant to be exposed to other source files to call, it shall be declared as static function.
  • Same with a global variable. It this global variable is not meant to be accessible (via extern keyword) from other source files, then it shall be declared as a static global variable.

Other Recommended Reads

Join the conversation

Your email address will not be published. Required fields are marked *