How to make a local variable global in C (or C++)?

Asked

Viewed 2,433 times

1

I can turn a local variable into a global variable, even though it’s within a function? In C or even C++ if it doesn’t exist in C. I’ve seen something like this in Java, but I don’t know exactly how it works.

  • 4

    Why not just declare the variable outside?

  • Because I need it to keep a value of a function. Being that the user who will enter this value

  • I don’t understand. This doesn’t stop the variable from being global. Also, why not just return the read value?

  • Thus there is a function that asks the user to inform the size of a vector and this size must be accessible from all other functions.

  • 5

    @Marcosadrian then just do what Pablo said, declare out of office. It would be nicer if you put the code in the question, even so we can help you not snipe. It makes it easier for you to explain what the real problem you want to solve is, and not to limit yourself to the way you think the solution is. Even, this business of wanting solution in C and C++ gives a strange impression. Deciding which language to use based on this particular issue is a sign of a problem.

  • You do not need to declare a variable only at the location where it will receive a value. The variable can be global (declared from the outside) and you assign the value to it in this function. As long as you call this function before the others, you won’t have a problem.

Show 1 more comment

2 answers

1

Do as everyone has said, or even better, declare outside the scope of the function and as Static.

static int variavel=0;

void X()
{
      variavel++;
}
int main()
{
     cout << variavel; 
     variavel ++;
     cout << variavel;
     X();
     cout << variavel;
     return 0;
}

0

The only way (as far as I know) for you to have a global variable in C is to declare it out of any function, like any other global variable.

I imagine what you want is to declare a local variable with the behavior of a global variable, with the limitations of scope. For this use the word static.

void funcao(){
    static int k = 0;
    k++;
    printf("%d\n", k);
}

Calling the function 3 times will print:

1
2
3

The explanation for this is due to the fact that when declaring a local variable as Static, it will be stored in another memory location. Whenever there is a function call the call is generated call stack, a space is reserved to store the values of the local variables and, when leaving the function, the region is released. When using Static, the variable is allocated to another location.

There is an interesting reading on function call stack if you are interested: Bufferoverflow

Recommend: Static: The Multipurpose Keyword

  • Exactly this was my doubt Murillo Henrique, make her behave like a global even being within a function. But with that I can access it from any function ?

  • Another thing also, this implementation works in C Language ?

  • No, you will not be able to access from another location, just within the function. The only thing that changes is that the value of the variable is not lost when leaving the function because it is in another memory location and not in the function call stack. The example I gave works in C, remembering that if you use the word Static in a global variable or function you will be restricting access to it for other files, the word Static has different uses depending on where it is used

Browser other questions tagged

You are not signed in. Login or sign up in order to post.