Jeegar Patel
Jeegar Patel

Reputation: 27240

How to declare a global variable which is present in only one function?

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?

Upvotes: 0

Views: 4974

Answers (6)

Amit Singh Tomar
Amit Singh Tomar

Reputation: 8608

There is no way You can restrict scope of global variable to one function in file ,It is available to all the functions in a file ,However You can restrict the scope of global variable to a file only, by using static keyword with global variable name.

   file1.c //1st file                     file2.h //2nd file

   #include<file2.h>                     
   main()                                static int a;
   {
   fun();                                 fun()
   }                                      {
                                           printf("%d",a);
                                          }

In the example you do have two files file1.c and file2.h ,Now variable a is available to function fun() only in 1st file.

Upvotes: 1

Veger
Veger

Reputation: 37905

You can define a static variable inside a function, then it is only available in that function (and keeps its value between multiple calls):

int increment() {
  static int x = 0; // Initialize with 0
  x++;
  return x;
}

void main() {
  printf("%d\n", increment());
  printf("%d\n", increment());
  printf("%d\n", increment());

  // (unfortunately) x is not available here...
}

Returns:
1
2
3

Each time the increment() function is called, it will return a higher number.

It is not possible to use variables outside it scope. So you could either define a variable in the 'global scope' (as demonstrated by Kerrek SB) or a static variable in a function (or any other scope) (as demonstrated above). If any of these possibilities are not to applicable for your situation, I am afraid you should (drastically) modify the structure of you application...

Upvotes: 3

Anuj Patel
Anuj Patel

Reputation: 17879

declare it global as static and dont initialize it. as soon as the function is called initialize it inside the function

i.e.

static int a;

void main()
{
  // Some code here
}

void function()
{
  a=10; 
  // Some code here as well

}

Upvotes: 0

Alnitak
Alnitak

Reputation: 340055

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.

Upvotes: 2

sud03r
sud03r

Reputation: 19799

It is recommended to avoid use of global variables as far as possible. In your particular case what you can do is simply:

function(int* toModify)
{
    *toModify = 10;
    // do sth
}

Now your other functions can use the modified value.

However, if you are keen on using a global variable, you have to use two global vars,

int gIsFuncCalled = 0;
int gToModify = 0;

void function() 
{ 
    gIsFuncCalled = 1;
    gToModify = 10;
}

Now you can use gToModify conditionally using gIsFuncCalled

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477640

C doesn't work like that - a global variable is allocated at load time and exists for the entire duration of the program, independent of the runtime behaviour. If you really must know whether the variable has been "set", you could include a separate flag:

int global_a;
int global_a_has_been_set = 0;

void f()
{
  global_a = 10;
  global_a_has_been_set = 1;
}

void elsewhere()
{
  if (global_a_has_been_set != 0) { /* ... */ }
}

If you know that your variable can only be non-negative, then you could alternatively use a special sentinel value like -1 to indicate that the variable hasn't been "set" yet.

Most likely though is that you should rework your design so that you already know by other means whether or not you need the variable.

Upvotes: 4

Related Questions