Thread: static variables

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    static variables

    The following code gave error on gcc compiler

    Code:
    int main()
    {
    extern int a;
    printf("%d",a);
    return 0;
    }
    static int a=5;
    But when static is removed from int a then it runs with no errors..Can someone please clear the doubt??

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    extern means that the variable will be defined elsewhere. static (at least, at file scope) limits visibility to subsequent code in this source file.

    With the static keyword, the definition of variable a is not visible to main(), so does not get associated with the extern declaration.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Thank you but I think static variables have internal linkage that is available to same file and not to outside files..

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Saurabh Mehta View Post
    Thank you but I think static variables have internal linkage that is available to same file and not to outside files..
    You are mistaken.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You are mistaken.
    No. He is not.

    A variable with a `static' storage specifier defined at global scope has internal linkage.

    [Edit]
    To be clear, the `extern' keyword isn't a storage specifier in that external linkage is the default for global variables, but it does change the visibility of the variable for the compiler as it serves as a declaration which in C, and C++, is necessary.

    In other words, you are essentially telling the compiler two different things about the same variable.

    The "solution", for lack of a better word, is to put the static variable above `main' so that it serves as both a declaration and a definition.

    If for some reason that is not an option, there are alternatives, but I will not explain them until you give a very good reason.
    [/Edit]

    Soma
    Last edited by phantomotap; 03-19-2013 at 06:15 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by grumpy View Post
    You are mistaken.
    Then why does removing static gives no error although it is defined after main()..??

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by phantomotap View Post
    No. He is not.

    A variable with a `static' storage specifier defined at global scope has internal linkage.

    Yes, the variable has internal linkage. But he is mistaken in interpreting that as meaning an extern declaration in the same file will correspond to that static definition (aka "available").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by grumpy View Post
    Yes, the variable has internal linkage. But he is mistaken in interpreting that as meaning an extern declaration in the same file will correspond to that static definition (aka "available").
    I didnt mention 'extern' in my previous comment No problems but now I have another doubt the following code gave error as "non static declaration of 'a' follows static declaration"
    Code:
    static int a;
    a=7; 
    int main()
    {
    
    
    printf("%d",a);
    return 0;
    }
    Can someone please clear the confusion and what does this error mean??

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Then why does removing static gives no error although it is defined after main()..??
    See my post: in such a case as that, the `extern' line serves as a declaration to the compiler that an appropriate variable exists.

    And again, that `static' storage class doesn't work here because the compiler is expecting a variable with external linkage because that is exactly what you told it to expect.

    But he is mistaken in interpreting that as meaning an extern declaration in the same file will correspond to that static definition (aka "available").
    Yes.

    The problem though is that he doesn't understand what `extern' is doing.

    Telling him that he is mistaken about the one part he apparently understands isn't going to be helpful in teaching him about `extern'.

    [Edit]
    Can someone please clear the confusion and what does this error mean??
    This is going to get ugly.

    *shrug*

    Actually, I'm not going to explain all the details. You would not understand them.

    Here is the minimum: you can't normally have instructions at global scope.

    Yes, assignment is an instruction. A definition that has an `=' as part of the expression isn't really an assignment.

    Basically, the assignment line is the same, conceptually, as putting "putc('!')" at global scope. You can't do that.

    You are getting that particular error because C has a heritage that allows implicit declaration of variables. I'm not going to get into that, but if you change the name of the variable, you'll kind of see the evidence.
    [/Edit]

    Soma
    Last edited by phantomotap; 03-19-2013 at 06:26 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The way I see it:
    extern is a hint for the linker to search for an exported symbol in all the objects and libs.
    static means that the symbol will not be exported at all ( linker will not know about it)
    in the original code there is a conflict because a is not an exported symbol -> ERROR

    my error messages
    Code:
    t.c:7:12: error: static declaration of a follows non-static declaration
    t.c:3:12: note: previous declaration of a was here
    kurt

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    extern is a hint for the linker to search for an exported symbol in all the objects and libs.
    This is wrong. The linker never sees `extern'. The linker either sees a name with external linkage or it doesn't.

    If you have multiple variables with external linkage under the same name you'll get multiple definition errors.

    The `extern' is saying to the compiler that a variable having external linkage under the given name exists.

    [Edit]
    If the name that you told the compiler would be available isn't found by the linker you will also get a linker error.
    [/Edit]

    Soma

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @phantomotap thank you for your answer but in my previous question when i run the code by removing static from the global variable it gave no errors why is that so??

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because C is weird. If you have
    Code:
    int a;
    a = 7;
    at the global scope, the first line is a so-called "tentative definition". This construct is an unholy hack in the C standard to maintain compatibility with pre-standard codebases. It means something like "treat this as a definition if you don't see a better candidate, other as a declaration". So when the compiler sees the second line, the equally unholy "implicit int" rule tells it to treat it like "int a = 7;", i.e. a definition with an initializer. Since this is better than the previous line, the tentative definition becomes a declaration.
    A definition with the static storage specifier, as you had before, can never be a tentative definition but will be a full definition, thus the second line is a redefinition and therefore in conflict.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Thank you it was a really great explaination!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variables
    By frs in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2010, 08:44 AM
  2. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  3. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 09:21 AM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM

Tags for this Thread