Can Global Variables be dangerous ?

In a small code, we can track values of global variables. But if the code size grows, they make code less understandable (hence less maintainable). It becomes difficult to track which function modified the value and how.





// A CPP program to demonstrate that a 
// global variables make long code less
// maintainable.
int c = 1;
int fun1()
   // 100 lines of code that
   // may modify c and also
   // call fun2()
void fun2()
   // 100 lines of code that
   // may modify c and also
   // call fun1()
void main()
    // 1000 lines of code
    c = 0;
    // 1000 lines of code
    // At this point, it becomes difficult
    // to trace value of c 
    if (c == 1)
        cout << "c is 1" << endl 
        cout << "c is not 1 << endl


  • In above code, we notice one of the biggest problem of global variable that is Debugging. It means if we trying to figure out where that variable c has changed between thousands of lines of code is very difficult job.
  • In multihreaded environment a global variable may change more than once (in different execution orders) and cause more problems.
  • Global variables are generally used for constants. Using them for non-const values is not recommended.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.