title: C - program structure (avoiding global variables, includes, etc.)
body: <p>I'm using C (not C++) and I'm unsure how to avoid using global variables.</p>
  
  <p>I have a pretty decent grasp on C, its syntax, and how to write a basic application, but I'm not sure of the proper way to structure the program.</p>
  
  <p>How do really big applications avoid the use of global variables? I'm pretty sure there will always need to be at least some, but for big games and other applications written in C, what is the best way to do it?</p>
  
  <p>Is there any good, open-source software written strictly in C that I could look at? I can't think of any off the top of my head, most of them seem to be in C++.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here's an example of where I would use a global variable in a simple API hooking application, which is just a DLL inside another process.</p>
  
  <p>This application, specifically, hooks API functions used in another application. It does this by using WriteProcessMemory to overwrite the call to the original, and make it a call to my DLL instead.</p>
  
  <p>However, when <strong>un</strong>hooking the API function, I have to write back the original memory/machine code.</p>
  
  <p>So, I need to maintain a simple byte array for that machine code, one for each API function that is hooked, and there are a lot.</p>
  
  <pre><code>// Global variable to store original assembly code (6 bytes)
  BYTE g_MessageBoxA[6];
  
  // Hook the API function
  HookAPIFunction ( "user32.dll", "MessageBoxA", MyNewFunction, g_MessageBoxA );
  
  // Later on, unhook the function
  UnHookAPIFunction ( "user32.dll", "MessageBoxA", g_MessageBoxA );
  </code></pre>
  
  <p>Sorry if that's confusing.</p>