Static is multipurpose keyword, we can use static in context of variable, function and classes.
Here we discuss about the static variable.
__________________________________________________________________
//In this program we are not using static variable
#include<stdio.h>
#include<conio.h>
void show()
{
int i=0; //Note, we are not declaring 'i' as a static variable here
i++; //variable 'i' increased by 1
printf("output is %d\n" ,i);
}
void main()
{
show(); //here we are calling show 3 times
show();
show();
}
output:
output is 1
output is 1
output is 1
You can see that output is 1, although we are calling show function 3 time.
That because the variable i not retaining the value.
//using STATIC variable
#include<stdio.h>
#include<conio.h>
int show()
{
static int i=0; //here we use static variable
i++;
printf("output is %d\n" ,i);
}
void main()
{
show();
show();
show();
}
output:
output is 1
output is 2
output is 3
__________________________________________________________________
//In this program we want that user have only 4 chance to enter right password
#include<iostream>
#include<string>
#include<stdlib.h> //for exit(0);
using namespace std;
int show()
{
static int i=0; //here we use static variable
// int i=0;
string password;
i++;
if(i<=3)
{
cout << "\nenter your password: ";
cin >> password;
if (password == "foobar")
{
cout << "\nwelcome you log in!\n";
exit(0);
}
}
else
{
cout << "recover your password";
}
}
int main()
{
show(); //here we can use the loop also
show();
show();
show();
}
Here we discuss about the static variable.
__________________________________________________________________
//In this program we are not using static variable
#include<stdio.h>
#include<conio.h>
void show()
{
int i=0; //Note, we are not declaring 'i' as a static variable here
i++; //variable 'i' increased by 1
printf("output is %d\n" ,i);
}
void main()
{
show(); //here we are calling show 3 times
show();
show();
}
output:
output is 1
output is 1
output is 1
You can see that output is 1, although we are calling show function 3 time.
That because the variable i not retaining the value.
- So to retain the value we use this variable as a static variable. (see next program)
- Static variable is useful when we want to count the no of attempt performed by the client. (see last program)
//using STATIC variable
#include<stdio.h>
#include<conio.h>
int show()
{
static int i=0; //here we use static variable
i++;
printf("output is %d\n" ,i);
}
void main()
{
show();
show();
show();
}
output:
output is 1
output is 2
output is 3
__________________________________________________________________
//In this program we want that user have only 4 chance to enter right password
#include<iostream>
#include<string>
#include<stdlib.h> //for exit(0);
using namespace std;
int show()
{
static int i=0; //here we use static variable
// int i=0;
string password;
i++;
if(i<=3)
{
cout << "\nenter your password: ";
cin >> password;
if (password == "foobar")
{
cout << "\nwelcome you log in!\n";
exit(0);
}
}
else
{
cout << "recover your password";
}
}
int main()
{
show(); //here we can use the loop also
show();
show();
show();
}
No comments:
Post a Comment