Can Static Variables be Declared in a Header File? The main purpose of the header files is to provide information to the compiler for the compilation process. Header files can be used for global declarations or can be included in multiple source files. The variables declared in the header files are called static variables.
Static variables can be declared in the header files or any other source file. But in the header files, they can only be initialized once while in the source files, they can be initialized any number of times.
Why static variables cannot be declared in the header file?
The reason for this is that the header files are not required to compile the header files.
Static variables declared in the header file can be accessed only by the source files which includes the header file. So, the header files are just used for declaration purposes.
Static variables declared in the header file can be initialized only once in the source files including the header file. In this way, the compiler will generate the same initialization for each time the static variables are accessed.
Static Variables: Static variables can be defined anywhere in the program. They can be defined in header files. Static variables are useful for storing the values that are not required to be changed while running the program.
Non-static Variables: Non-static variables cannot be defined in a header file. These variables are used to change while running the program.
Static variables vs non-static variables:
Static variables have the static scope and are defined once and used multiple times. Non-static variables have the dynamic scope and are defined and used each time the variable is needed. Static variables are declared in the header file and non-static variables are declared in the source file. Static variables can be used in the source file while non-static variables can only be used in the header file. Static variables are more preferred than non-static variables.
Examples:
If we are developing a calculator program then we can declare the variables as static variables. This will make the program more memory efficient.
// Declare static variables
int num1, num2, num3, num4, result;
// Define function
int add(int a, int b)
{
return a + b;
}
// Declare non-static variables
int sum = 0, ans = 0;
// Calling the function
sum = add(num1, num2);
ans = add(num1, num2, num3, num4, result);
If we are declaring a static variable then the scope of the variable is restricted to a particular file only. While declaring the non-static variable the scope of the variable is the whole program.
How to declare static variables in header files?
Declaring the static variables in the header files are done using the keyword ‘static’ before the variable name.
Example: Declaring static variables in the header files
static int first_name;
static int last_name;
static char phone_number[10];
static int age;
static float height;
static int is_female;
2. Declaring static variables in the source files
static int first_name;
static int last_name;
static char phone_number[10];
static int age;
static float height;
static int is_female;