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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:53:41+00:00 2026-05-25T02:53:41+00:00

Consider these examples: static int a; extern int a; //OK — what linkage does

  • 0

Consider these examples:

static int a;
extern int a; //OK -- what linkage does the a have now?

static int a;
int a; //ERROR

extern int a;
static int a; //ERROR

int a;
static int a; //ERROR

extern int a;
int a; //OK as expected

int a;
extern int a; //OK as expected

Why was it OK in the first example but not in the second?

As far as file-scope variables (global-scope) are concerned, these have external linkage and a static duration when no keyword is specified.

Thank you

AFAIK, linkage and storage duration for functions is a bit different.

EDIT:
I’ve tried compiling using gcc 4.5.2 -Wall -pedantic –std=c99

More on: http://c-faq.com/decl/static.jd.html You can see that the 1st example works there too but 2nd doesn’t. However, I don’t see what makes them so different.

  • 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-25T02:53:42+00:00Added an answer on May 25, 2026 at 2:53 am

    The answer to your first question is found in §6.2.2 of the C standard:

    4 For an identifier declared with the storage-class specifier extern
    in a scope in which a prior declaration of that identifier is visible,
    if the prior declaration specifies internal or external linkage, the
    linkage of the identifier at the later declaration is the same as the
    linkage specified at the prior declaration. If no prior declaration is
    visible, or if the prior declaration specifies no linkage, then the
    identifier has external linkage.

    So the linkage of a is internal.

    For your second question, the second sentence of the immediately following paragraph is apropos:

    5 If the declaration of an identifier for a function has no
    storage-class specifier, its linkage is determined exactly as if it
    were declared with the storage-class specifier extern. If the
    declaration of an identifier for an object has file scope and no
    storage-class specifier, its linkage is external.

    Because a is an object, not a function, the declaration int a; with no storage-class specifier gives a external linkage. The same section then has this to say:

    7 If, within a translation unit, the same identifier appears with both
    internal and external linkage, the behavior is undefined.

    Since, in your second example, a appears with both internal and external linkage, this paragraph is triggered. One (particularly helpful) manifestation of undefined behaviour is the error that your compiler is producing.

    All of your examples can be understood by these rules:

    1. int a; always declares a with external linkage;
    2. static int a; always declares a with internal linkage;
    3. extern int a; declares a with whatever linkage it already had, or external linkage if it had none;
    4. Two declarations of a in the same scope with different linkage give undefined behaviour.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider these examples using print in Python: >>> for i in range(4): print('.') .
Let's consider the below example. There, I have: target MAIN calls target t and
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Consider I have a these two properties: public class Test { [Required(ErrorMessage = Please
I have a queston regarding double-checked locking. Consider this example: public class Singleton {
Consider following two examples: Example 1: <xs:import namespace=http://example.com/ns schemaLocation=test.xsd/> Example2: <sample:Data Test=true xsi:schemaLocation=http://example.com/test.xsd> How
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider these classes. class Base { ... }; class Derived : public Base {
Consider these 3 table structures. Which will perform these queries the best. Structure 1
Consider these two classes mapped to the same table. One is readonly via mutable=false.

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.