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 6733923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:49:41+00:00 2026-05-26T10:49:41+00:00

Possible Duplicate: Static vs global I’m confused about the differences between global and static

  • 0

Possible Duplicate:
Static vs global

I’m confused about the differences between global and static global variables. If static means that this variable is global only for the same file then why in two different files same name cause a name collisions?

Can someone explain this?

  • 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-26T10:49:41+00:00Added an answer on May 26, 2026 at 10:49 am

    Global variables (not static) are there when you create the .o file available to the linker for use in other files. Therefore, if you have two files like this, you get name collision on a:

    a.c:

    #include <stdio.h>
    
    int a;
    
    int compute(void);
    
    int main()
    {
        a = 1;
        printf("%d %d\n", a, compute());
        return 0;
    }
    

    b.c:

    int a;
    
    int compute(void)
    {
        a = 0;
        return a;
    }
    

    because the linker doesn’t know which of the global as to use.

    However, when you define static globals, you are telling the compiler to keep the variable only for that file and don’t let the linker know about it. So if you add static (in the definition of a) to the two sample codes I wrote, you won’t get name collisions simply because the linker doesn’t even know there is an a in either of the files:

    a.c:

    #include <stdio.h>
    
    static int a;
    
    int compute(void);
    
    int main()
    {
        a = 1;
        printf("%d %d\n", a, compute());
        return 0;
    }
    

    b.c:

    static int a;
    
    int compute(void)
    {
        a = 0;
        return a;
    }
    

    This means that each file works with its own a without knowing about the other ones.


    As a side note, it’s ok to have one of them static and the other not as long as they are in different files. If two declarations are in the same file (read translation unit), one static and one extern, see this answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: Difference between static class and singleton pattern? I'm thinking about the choice
Possible Duplicate: Where are static variables stored (in C/C++)? I am wondering where global
Possible Duplicate: Static class variables in Python What is the Python equivalent of static
Possible Duplicate: Global int variable objective c I would like to create a global
Possible Duplicate: Static extension methods So I know that Extension methods are for object
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
Possible Duplicate: How to capitalize first letter of each sentence? public static string CapitalizeEachWord(this
Possible Duplicate: Static Variables in WCF So I am fairly new to using WCF
Possible Duplicate: Static variables in C++ // x.h int i = 3; // x1.cpp

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.