what is static variable

The static variable is used to refer to the common property of all objects 
(that is not unique for each object), 
e.g., The company name of employees, college name of students, etc. 
Static variable gets memory only once in the class area at the time of 
class loading. Using a static variable makes your program more 
memory efficient (it saves memory). Static variable belongs to the class 
rather than the object.

4.6
5
Bippy 8155 points

                                    #include<iostream>

void Increment() {
	int i = 0;//The life time of variable is limited to the function scope
	i++;
	std::cout << i << std::endl;
};//This will increment i to one and when it will reach the end bracket the lifetime of var will get  destroyed
void IncrementStaticVar() {
	static int i = 0;//The life time of this var is = to program
	i++;
	std::cout << i << std::endl;
}//This will var i increment i till the program ends and i will get destroyed when program ends
int main() {
	Increment();//output 1
	Increment();//output 1
	Increment();//output 1
	IncrementStaticVar();// output 2
	IncrementStaticVar();// output 3
	IncrementStaticVar();// output 4
	IncrementStaticVar();// output 5
	std::cin.get();
}

4.6 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
why static variables are used how to create a static variable static and var what is the static variables static variable use static variable means c++ local static variable examples creating a static variable static variales what is a static varible using static variables purpose of static variables variable static What is a static variable in programming wht is a static variable What does static variable mean? what is a static instance variable What is static variable? when should you use static variables? when we use static vaiable what does static variable do static variable ? value of static variable is cpp static local variable meaning of static variable why we use static variables what are static variables and functions static variables explained why static variable is used what is the purpose of a static variable this with static variable what are static variables how to declare static variable static var when should i create static variable static variables definition static variable with example use of static variables static variable' static variable. static variables: what does a static variable mean static variable definition declare static variable static variable? what is static variables what does static variable mean static variable is why is a static variable called static when to use static variables what is the use of static variable what are the uses static variable static varial what is static variable use static variablles Write a program using static members in C#? C statuc static variable in c example static variable meanin static variable declaration static int in c static variable make variable static c static keyword c static var in c static varible #define static c$ static variable meaning how to use static variables staic variables static variable declaration in c c static variables static c example how to declare a static variable incpp non static variable in java program.cs static variable what is a static function global c++ class key points about static variable static variable in function static int can we extern a static variable how to create a static variable in c how to declare static variable sin c uses of static keyword in oops how to declare a static variable in c++ static variable example Static variables declared as part of the functions have a local scope and local lifetime static variables in c c static keyword static variable in a method java Code static variable ka c static variable 1) A static variable is also called a class variable . static variables static variable c what is static variable static variables C how to work with static int in C c#static variable how to keep a static variable in c STATIC VARIBALE c language static What is a static variable? static variable \ static c static in c c static c using static variables in main static variable in c static variable Which variables can have the same name in a different function?(CO2)(BTL 1)a.Both static variables and Function argumentsb.Global variablesc.Static variablesd.Function arguments what is a static variable global class variable c++ local static variable in c++ local static in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source