0% found this document useful (0 votes)
51 views5 pages

Static Keyword

Uploaded by

tikoya2311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Static Keyword

Uploaded by

tikoya2311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Static Variables in C

Static variables have the property of preserving their value even after they are out of their scope!
Hence, a static variable preserves its previous value in its previous scope and is not initialized again in
the new scope.

Syntax:
static data_type var_name = var_value;

Following are some interesting facts about static variables in C:

1. A static int variable remains in memory while the program is running.


2. Static variables are allocated memory in the data segment, not the stack segment.
3. Static variables (like global variables) are initialized as 0 if not initialized explicitly.
4. In C, static variables can only be initialized using constant literals. For example, the following
program fails in the compilation.

#include<stdio.h>
int initializer(void)
{
return 50;
}

int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}
Output

In function 'main':
9:5: error: initializer element is not constant
static int i = initializer();
^

Note: Please note that this condition doesn’t hold in C++. So if you save the program as a
C++ program, it would compile and run fine.
5. Static global variables and functions are also possible in C/C++. The purpose of these is to limit
the scope of a variable or function to a file.
6. Static variables should not be declared inside a structure. The reason is C compiler requires the
entire structure elements to be placed together (i.e.) memory allocation for structure members
should be contiguous. It is possible to declare structure inside the function (stack segment) or
allocate memory dynamically(heap segment) or it can be even global (BSS or data segment).
Whatever might be the case, all structure members should reside in the same memory segment
because the value for the structure element is fetched by counting the offset of the element from
the beginning address of the structure. Separating out one member alone to a data segment
defeats the purpose of structure and it is possible to have an entire structure as static.

Static Keyword in C++


The static keyword has different meanings when used with different types. We can use static keywords
with:

Static Variables: Variables in a function, Variables in a class


Static Members of Class: Class objects and Functions in a class

Static Variables
Static variables in a Function: When a variable is declared as static, space for it gets allocated
for the lifetime of the program. Even if the function is called multiple times, space for the static
variable is allocated only once and the value of the variable in the previous call gets carried
through the next function call.
Static variables in a class: The static variables in a class are shared by the objects. There can
not be multiple copies of the same static variables for different objects. Also because of this
reason static variables can not be initialized using constructors. A static variable inside a class
should be initialized explicitly by the user using the class name and scope resolution operator
outside the class.

Static Members of Class


Class objects as static: Just like variables, objects also when declared as static have a scope till
the lifetime of the program.
// CPP program to illustrate
// when not using static keyword
#include <iostream>
using namespace std;

class GfG {
int i;

public:
GfG()
{
i = 0;
cout << "Inside Constructor\n";
}
~GfG() { cout << "Inside Destructor\n"; }
};

int main()
{
int x = 0;
if (x == 0) {
GfG obj;
}
cout << "End of main\n";
}
Output
Inside Constructor
Inside Destructor
End of main

In the above program, the object is declared inside the if block as non-static. So, the scope of a
variable is inside the if block only. So when the object is created the constructor is invoked and soon
as the control of if block gets over the destructor is invoked as the scope of the object is inside the if
block only where it is declared.
// CPP program to illustrate
// class objects as static
#include <iostream>
using namespace std;

class GfG {
int i = 0;

public:
GfG()
{
i = 0;
cout << "Inside Constructor\n";
}

~GfG() { cout << "Inside Destructor\n"; }


};

int main()
{
int x = 0;
if (x == 0) {
static GfG obj;
}
cout << "End of main\n";
}
Output
Inside Constructor
End of main
Inside Destructor

Now the destructor is invoked after the end of the main. This happened because the scope of static
objects is throughout the lifetime of the program.

Static functions in a class: Just like the static data members or static variables inside the class,
static member functions also do not depend on the object of the class. We are allowed to invoke a
static member function using the object and the ‘.’ operator but it is recommended to invoke the
static members using the class name and the scope resolution operator. Static member functions
are allowed to access only the static data members or other static member functions, they can not
access the non-static data members or member functions of the class.
Can Static Functions Be Virtual in C++?
In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you
have a pointer or reference to an instance of a class. Static functions aren’t tied to the instance of a
class but they are tied to the class. C++ doesn’t have pointers-to-class, so there is no scenario in
which you could invoke a static function virtually.

Some interesting facts about static member functions in C++


1. static member functions do not have this pointer.

#include<iostream>
class Test {
static Test * fun() {
return this; // compiler error
}
};

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

2. A static member function cannot be virtual.


3. Member function declarations with the same name and the name parameter-type-list cannot be
overloaded if any of them is a static member function declaration.

#include<iostream>
class Test {
static void fun() {}
void fun() {} // compiler error
};

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

4. A static member function can not be declared const, volatile, or const volatile.

You might also like