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

  • SEARCH
  • Home
  • 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 465693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:24:12+00:00 2026-05-12T23:24:12+00:00

How does the following example usage of extern specifer behave. We have a global

  • 0

How does the following example usage of extern specifer behave.

We have a global variable int x both in files one.c and two.c
We want to use these in three.c so have declared this variable in three.c as

extern int x;

What would happen when we compile and link these files?

My answer is: compilation of all these files should succeed, however the linker should flag an error at linking, due to multiple declarations of x.
Would there be any difference in behavior in C++ ?

Is these any way to refer to int x (in three.c) simultaneously from both files, in C and C++.
In C++, I guess we can use namespaces to acheive this. Right?

  • 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-12T23:24:12+00:00Added an answer on May 12, 2026 at 11:24 pm

    By default, global variables have external linkage, which means that they can be used by other source files (or “translation units”). If you instead declare your global variables with the static keyword, they will have internal linkage, meaning they will not be usable by other source files.

    For variables with external linkage, you can’t have multiple variables with the same name, or the linker will complain. You can have two variables with the same name, though, as long as at least one has internal linkage, and of course you can’t reference both of them in the same source file.

    An extern declaration is just saying to the compiler “here is the name of some variable with external linkage defined in another translation unit,” allowing you to refer to that variable.

    C++ is exactly the same, except for the addition of namespaces. If global variables are put inside a namespace, then they can have the same name without linker errors, provided they are in different namespaces. Of course, all references to those variables then have to either refer to the full name namespace::var_name, or use a using declaration to establish a local namespace context.

    C++ also has anonymous namespaces, which are entirely equivalent to using the static keyword for global variables in C: all variables and functions declared inside an anonymous namespace have internal linkage.

    So, to answer your original question, you are right — compilation would succeed, but linking would fail, due to multiple definitions of the variable x with external linkage (specifically, from the translation units one.c and two.c).

    From three.c, there is no way to refer simultaneously to both variables x. You’ll need to rename x in one or both modules, or switch to C++ and put at least one x inside a namespace.

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

Sidebar

Related Questions

How does one go about adding a click handler in the following example? I
What does the following mean? Class.Function(variable := 1 + 1) What is this operator
Why does the following behave unexpectedly in Python? >>> a = 256 >>> b
Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);
Why does the following code not work as I was expecting? <?php $data =
Why does the following method hang? public void pipe(Reader in, Writer out) { CharBuffer
What does the following code do in C/C++? if (blah(), 5) { //do something
What does the following code do? A link to something in the PHP manual
What does the following error mean? Geeneration of designer file failed: Exception from HRESULT:
Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time?

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.