What exactly are global variables?

My professor specifically told us not to use global variables for our assignment. From my understanding, a global variable is something like an 'int x =2;' outside of a function.
Do I have any global variables in my code?
Last edited on
I don't see one in your code.
they can be accessed by any function, class, anything. Its hard to tell what the current value is, and to debug, and more. They cause a lot of trouble in large programs.


#include <cstdio>

int x; //GLOBAL


void foo()
{
x = 2;
}

int main()
{

x = 3;
return 0;
}


Last edited on
There are globals, but they are there for a good reason. The std::cin and std::cout.
Topic archived. No new replies allowed.