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.
C++
int c = 1;
int fun1()
{
}
void fun2()
{
}
void main()
{
c = 0;
if (c == 1)
cout << "c is 1" << endl
else
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 a multithreaded 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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!