Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 908789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:46:19+00:00 2026-05-15T16:46:19+00:00

I’m using C (not C++) and I’m unsure how to avoid using global variables.

  • 0

I’m using C (not C++) and I’m unsure how to avoid using global variables.

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.

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?

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++.

Thanks.

Edit

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.

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.

However, when unhooking the API function, I have to write back the original memory/machine code.

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.

// 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 );

Sorry if that’s confusing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:46:20+00:00Added an answer on May 15, 2026 at 4:46 pm

    “How do really big applications avoid the use of global variables?”

    1. Use static variables. If a function needs to remember something between calls, use this versus global variables. Example:

      int running_total (int num) {
          static int sum  = 0;
          sum             += num;
          return sum;
      }
      
    2. Pass data via parameters, so that the value is defined one place, maybe main() and passed to where it is needed.

    3. If all else fails, go ahead and use a global but try and mitigate potential problems.

      1. At a minimum, use naming conventions to minimize potential conflicts. EG: Gbl_MyApp_DeveloperName. So all global variables would start with the Gbl_MyApp_ part — where “MyApp” was something descriptive of your app.
      2. Try to group functions by purpose, so that everything that needs a given global is in the same file (within reason). Then globals can be defined and restricted to that file (beware the extern keyword).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sounds like you have magic quotes enabled. Either disable them,… May 15, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer www.codebarrel.com it has everything you asked for May 15, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer I think form_validation callbacks do need to return a bool.… May 15, 2026 at 6:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.