Global variable in C

Asked

Viewed 29 times

1

Good afternoon,

I need to use a global variable that depends on a condition in a code.

I have the following documents Code2.cpp and Code3.cpp, both contain the Code1.h.

I declared an extern on Code1.h:

extern int VARGLOBAL;

And in Code2.cpp I declared the variable:

in VARGLOBAL = 0;

But I have a Case here in Code2, which can change this VARGLOBAL.

Case XXXX:
VARGLOBAL = 1;
break;

And in Code3.cpp I have a condition:

If ( VARGLOBAL == 1)

(faca 1)

else

(faca 2)

Here comes the doubt, my VARGLOBAL is always "0" and enters the condition "Else" (knife 2), even the Case happening (I’m sure it happened).

What should I do? Can you help me?

  • 1

    Hello @Willian Henrique welcome to Stack Overflow! Be more specific and detailed in your question, give us a reproducible example of your code so we can help you.

  • You probably don’t need this and the mistake is something else, even if it’s not this, we have no way of knowing because a lot of information is missing.

  • Hello, thank you! I don’t know how to be more specific :/ But, speaking otherwise, I have a project, which has these Code3.cpp, code2.cpp and code1.h. To use a global variable I defined it in this code1.h because the two cpp files have them included. In code2 I defined it as 0, however, there is a case that runs, the times I wanted to change the variable from 0 to 1. However this does not happen. The case runs and the global variable remains 0, as defined above.

No answers

Browser other questions tagged

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