Static variable keep their values and are not destroyed even after they go out of scope.
Eg:
int ReturnNumber()
{
Static int n_id = 0;
return n_id++;
}
int main()
{
std::cout<<ReturnNumber();
std::cout<<ReturnNumber();
std::cout<<ReturnNumber();
return 0;
}
Output: 0 1 2.
Const:
The const keyword specifies that variable's value is constant and tells the compiler to prevent the programmer from modifying it.
Eg: const int n = 5;
n++; // Error
n=6; // Error.
----
Eg:
int ReturnNumber()
{
Static int n_id = 0;
return n_id++;
}
int main()
{
std::cout<<ReturnNumber();
std::cout<<ReturnNumber();
std::cout<<ReturnNumber();
return 0;
}
Output: 0 1 2.
Const:
The const keyword specifies that variable's value is constant and tells the compiler to prevent the programmer from modifying it.
Eg: const int n = 5;
n++; // Error
n=6; // Error.
----