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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:42:15+00:00 2026-05-31T06:42:15+00:00

Have a look at the following code snippet… File1.h void somefunc(int); File1.c #include File1.h

  • 0

Have a look at the following code snippet…

File1.h

void somefunc(int);

File1.c

#include "File1.h"

extern int var;

void somefunc(int x)
{
   ......
   var ++;
   etc, etc,
   ....
   return;
}

File2.h

static int var;

void someotherfunc(int);

File2.c

#include "File2.h"
#include "File1.h"

int var;

void someotherfunc(int z)
{
   z = etc etc;
   var --;
   ......
   somefunc(z);
   .....
   return;
}

The above four files compile without any problem.
The problem occurs when i try to initialize the variable ‘var’.
If the ‘var’ is initialized in the File2.c where it is a global variable, the code compiles without any problems. But when i try to initialize the static variable in File2.h, the compiler throws an error saying ‘the variable ‘var’ in File1.c is undefined’. Can someone please tell what is happening here.

I was just trying to understand the concept of static variables and came upon this confusion. Any help would be appreciated.

  • 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-31T06:42:17+00:00Added an answer on May 31, 2026 at 6:42 am

    static int var;

    This gives var internal linkage in the File2.c translation unit, whatever might follow (yes, even if the extern declaration follows).

    So if the first declaration seen is static int var, in that translation unit var will forever be internal, thus inaccessible to other translation units.

    6.2.2-4

    For an identifier declared with the storage-class specifier extern
    [File1.h] in a scope in which a prior declaration of that
    identifier is visible [the one in File2.h] if the prior declaration
    specifies internal or external linkage [it specifies internal], the linkage of the identifier at > the later declaration is the same as the linkage specified at the
    prior declaration
    .

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

Sidebar

Related Questions

Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {
Please have a look on the following code - var abc_text = Hello; var
Have a look at following code: @Local public interface MyService { void printMessage(); }
In my prism module, I have the following code snippet: using (var manifestResourceStream =
Have a look at the following code you are not required to read the
Have a look here: In the following code, what would be the type of
am new here. i have a slight problem; PLease look at the following code
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I have the following SQL design issue. The code below might look a little
I have the following C Header/Code Example: Header file struct category_info { int id;

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.