I need to declare a global variable which is only available if a certain function is called.
If that function is not called than that variable should not be available.
void function()
{
// if this function is called then declare int a=10;
// as global so other function can use this
}
How can I do such a thing in c?
C is not a dynamic language – all declared variables exist (subject to scoping rules) at all times.
You cannot test whether a variable has been declared, that’s the compilers job, and it’ll give you an error if you try to use one that’s not in scope.
Global variables have space allocated for them (in the “data” segment) automatically when the program is first loaded.
Hence you can only test whether the variable has changed from its original assigned value.