Thread: Static variables

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    43

    Question Static variables

    Hello everybody!
    I'm trying to understand static variables.

    I have these files:

    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
    
    int main()
    {
            int a=2;
            return 0;
    }
    example.h
    Code:
    void func();
    example.c
    Code:
    int a=1;
    
    void func()
    {
             ...}
    In this case, example.c contains the var a (not static) and so main.c can see it and forbids the creation of a new variable with the same name, by giving the error:

    obj\Debug\main.o:main.c: (.data+0x0): multiple definition of `a'
    obj\Debug\example.o:example.c: (.data+0x0): first defined here

    If I edit the file example.c as follows:
    Code:
    static int a=1;
    
    void func()
    {
             ...
    }
    It doesn't give any error anymore because the variable inside example.c is "private", that is its scope is the file example.c.

    But, if I remove the static keyword, the variable should be visible within main.c, so if I do something like this:
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
    
    int main()
    {
            printf("%d",a);
            return 0;
    }
    It says me that a is undeclared.
    main.c|7|error: 'a' undeclared (first use in this function)|

    So doesn't it see it? If yes, why can't I create a variable with the same name? Or, better, why can't I reassign it? Because if I do something like this it works:
    example.c
    Code:
    int a=1;
    
    void func()
    {
             ...
    }

    main.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
    
    int a;
    //a=3; -> error!
    int main()
    {
            printf("%d",a); //shows 1
            return 0;
    }
    Last edited by sleax; 11-19-2015 at 02:57 PM.

  2. #2
    Registered User
    Join Date
    Oct 2015
    Posts
    28
    You'll need to insert the following line into example.h (or main.c).

    extern int a;

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    43
    But, whether I do this:

    example.c

    Code:
    int a=1;
    
    void func()
    {
             ...
    }

    main.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
    
    int a;
    //a=3; -> error!
    int main()
    {
            printf("%d",a); //shows 1
            return 0;
    }
    or this:
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
    
    extern int a;
    //a=3; -> error!
    int main()
    {
            printf("%d",a); //shows 1
            return 0;
    }
    It has the same effects. So what changes?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't use the assignment operator outside of functions, except when you initially declare the variable, put that assignment into main().
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "example.h"
     
    extern int a;
    //a=3; -> error! Yes this is indeed an error.
    int main()
    {
            a = 30000; // No error here.
            printf("%d",a); //shows 1
            return 0;
    }
    By the way your question is really about global variables not about static.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    43
    Ok, now it's clear. Just one thing:
    I can update the value of a also without using the extern keyword. Why?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you just do a=3; on a line by itself (i.e. remove the // ), then it will indeed give an error. That's not an initializer, it's an assignment. You're trying to execute code outside of a function.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by sleax View Post
    Ok, now it's clear. Just one thing:
    I can update the value of a also without using the extern keyword. Why?
    The extern keyword is only necessary at declaration, not for each usage. This is "by design". You only need to declare something (describe it to the compiler) once per scope. This is the same reason you don't have to repeat the type every time you use a variable, like int x; int x++; int x = 3*y + 7; Furthermore, this would confuse the compiler, are you trying to redefine/redeclare something, or simply use it?

    You already told the compiler once that the definition of a (it's location, where the contents are stored) is external to this translation unit. Everything in main.c will use something like a temporary holding address when it's compiled, then when the linker comes along, it will see the real definition of a, and change all the temporary holding addresses in main to the real address of a.

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    43
    I apologize for my last comment. It's all clear. Thank you, guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variables
    By Saurabh Mehta in forum C Programming
    Replies: 13
    Last Post: 03-19-2013, 12:01 PM
  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. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  4. Static variables
    By gsuresh18 in forum C Programming
    Replies: 5
    Last Post: 08-11-2003, 08:32 AM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM

Tags for this Thread