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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:28:55+00:00 2026-05-31T12:28:55+00:00

#include <stdio.h> static i = 5; int main() { extern int i; printf(%d\n,i); return

  • 0
#include <stdio.h>

static i = 5;

int main()
{
extern int i;
printf("%d\n",i);
return 0;
}

Can someone give any use-case for declaring a static variable as extern inside a function block?

NEW:
Why is this not allowed?

int main()
{
static i = 5;
extern int i;
printf("%d\n",i);
return 0;
}
  • 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-31T12:28:56+00:00Added an answer on May 31, 2026 at 12:28 pm

    this is useful when you need to access a variable that resides within another translation unit, without exposing the external variable globally (for a few reasons, like name collision, or that the the variable shouldn’t be directly accessed, so static was used to limit its scope, but that TU’s header still needs access).

    As an example, lets say we have a translation unit foo.c, it contains:

    //foo.c
    static int i = 0;
    

    i shouldn’t be changed or directly accessed outside foo.c, however, foo.h comes along requiring access to i for an inline function, but i shouldn’t be exposed to any translation unit using foo.h, so we can use extern at functional level, to expose it only during the scope of IncI, the inline function requiring the use of i:

    //foo.h
    inline void IncI(int val)
    {
        extern int i;
        i += val;
    }
    

    Your second example is ‘disallowed’ because the compiler thinks you are trying to bind two different variables to the same symbol name, ie: it creates the static i at local scope, but searches for the extern int i at global scope, but doesn’t find it, because static i as at the function scope. a more clever compiler would just fix the linkage to the static i, whether or not this follows standards I wouldn’t know.


    Now that I have a C standards document to work from (shame on me I know…), we can see what the official stance is (in C99):

    6.2.2 Linkages of identifiers

    Section 3:

    If the declaration of a file scope identifier for an object or a function contains the storageclass
    specifier static, the identifier has internal linkage.

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

    thus, because static will cause internal linkage, the extern will bring that linkage into the current scope. there is also a footnote stating that this may cause hiding of variables:

    23) As specified in 6.2.1, the later declaration might hide the prior declaration.

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

Sidebar

Related Questions

#include<stdio.h> int main() { static int x; if(x == 10) printf(\n thanks...); x++; return
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main(void) { static int i=i++, j=j++, k=k++; printf(i = %d j =
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
#include <stdio.h> void main() { int p[]={0,1,2,3,4}; int *a[]={p,p+1,p+2,p+3,p+4}; printf(%u %u %u,a,*a,*(*a)); } What
#include<stdio.h> int fact(int i); void main() { int j; j=fact(4); printf(%d,j); } int fact(int
#include <stdio.h> int main (int argc, const char * argv[]) { static struct item
I tried the following code #include <stdio.h> int main(void) { typedef static int sint;
#include <stdio.h> const int str[1000] = {0}; int main(void) { printf(arr is %d\n, str[0]);
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits

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.