04
Jan
Key Differences Aspect Global Variable Static Variable Scope Accessible throughout program Limited to file, function, or class scope Visibility Shared across files (extern) Restricted to declaring scope Lifetime Entire program duration Entire program duration Example Code Global Variable // globals.cpp int globalVar = 42; // Global variable // main.cpp #include <iostream> extern int globalVar; // Declaration for the global variable from another file void modifyGlobal() { globalVar++; // Accessible across files } int main() { modifyGlobal(); std::cout << "Global Variable: " << globalVar << std::endl; // Prints: 43 return 0; } Enter fullscreen mode Exit fullscreen mode File-Scope Static Variable…