Thread: global variable question?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    global variable question?

    File a.c
    Code:
    int a = 5;

    file b.c
    Code:
    #include <stdio.h>int main()
    {
        printf("%d", a);
        return 0;
    }
    My understanding was that if a file is within a project (a.c) and a variable was declared outside the scope of any of the functions within that file that that variable would be global....Am I incorrect here?

    Certainly when I place
    Code:
    extern int a;
    before main the code compiles

    Is there no such thing as a global variable that crosses file boundaries?
    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    23
    Is there no such thing as a global variable that crosses file boundaries?
    Nope. You have to declare it with extern in every compiled file (after preprocessing, so if you just declare it in a header you include in every file, you're OK).

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Fair enough.....let me ask you this....is it true that in file a.c where int a =5; is defined (which is outside all functions within that file) that this variable will not be destroyed but be retained in memory for the life of the program? I am trying to understand what does 'global' really mean....Seems to me it means that it is a variable that will not get destroyed rather than a variable that is accessible to all files....Does this sound correct?

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    23
    Yes, in fact, global variables are stored in a separate part of memory on their own. Note that a global variable isn't the only kind of "a variable that will not get destroyed", but it's the only kind of variable that's accessible from within all functions in a file.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by ridgerunnersjw View Post
    Seems to me it means that it is a variable that will not get destroyed rather than a variable that is accessible to all files....Does this sound correct?
    Nope. A "global" is a variable that is accessible throughout the program, in all translation units. Although a global never gets "destroyed" until the program ends (when else would it possibly be destroyed?), this is also true of static variables, whether declared outside or inside a function.
    Code:
    // foo.c
    int global_var = 42;         // visible in all translation units
    static int not_global = 21;  // only visible in this translation unit
    
    void f() {
        static int in_a_func = 1; // only visible in this function
        //...
    }
    
    
    // bar.c
    extern int global_var;  // tell this translation unit about the existence of the global
                            // since it cannot magically know about it at the compile stage.
    extern int not_global;  // won't work!
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    As erikkonstas points out ....it is accessible throughout the program WITH the help of extern

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by ridgerunnersjw View Post
    As erikkonstas points out ....it is accessible throughout the program WITH the help of extern
    WTF?
    I didn't realize you had a learning disability.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    23
    Quote Originally Posted by ridgerunnersjw View Post
    As erikkonstas points out ....it is accessible throughout the program WITH the help of extern
    Um I didn't say anything different than john.c...?

  9. #9
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Well, at this point, here is probably all you need to know about global variables - do not use.

  10. #10
    Registered User
    Join Date
    Apr 2021
    Posts
    136
    The term used in C programming is "file scope." Variables declared outside of all functions, and functions themselves, are considered to be "objects with file scope."

    However, there's a little more to it than that.

    C has two ideas you have to keep in mind: scope and linkage. They are different, but they interact with each other.

    C defines three categories of linkage: external linkage, internal linkage, and no linkage. The first two are fairly simple: a symbol with file scope and external linkage is visible to other files. A symbol with file scope and internal linkage is not visible to other files but is visible inside the file where it is defined/declared. A symbol with file scope and no linkage is also not visible to other files. The main difference between no linkage and internal linkage is magic. Things like typedefs have no linkage -- mainly object types that "exist" in the compiler but "don't exist" when the object file is written. NOTE: beware of C++. There are a lot more different kinds of objects that have no linkage in C++ (like const objects). So rules that you read which are written for C++ may be false for C.

    By default, a symbol that is defined at file scope has external linkage:

    Code:
        int A = 0;
    
        void foo(void) {
         printf("foo!\n");
        }
    Both the variable 'A' and the function 'foo' have file scope and external linkage here.

    You can declare that a file scope symbol with external linkage is defined someplace else using the extern keyword:

    Code:
    extern int A;
    extern void foo(void);
    This is common in header files.

    You can "just declare" a function, and it's okay to define it later. Likewise, if you declare a variable but do not initialize the declaration, it's similar to an extern (there are some finicky details, though):

    Code:
    int A;   // declared, not initialized
    void foo(void); // prototype declaration, not a definition
    Finally, you can force internal linkage using the static keyword. This makes the declared/defined symbols invisible from outside (sort of):

    Code:
    static int A = 0;  // defined, but with internal linkage
    
    static void foo(void) {
        printf("foo, but only from within this file!\n");
    }
    Note: "static" and "linkage" can be complicated because this all relates to the object file format and CPU hardware and operating system being used. The objective in C is to accomplish that which is promised: to make symbols visible/invisible to other translation units. However, in a large enough program it may be impossible to write an object module without creating an entry for static symbols. Thus, a symbol marked as static may still get an entry in the object file's symbol data, and so might be visible to other tools. Usually in this case the compiler adopts a convention, like "extern symbols have a leading underscore while static symbols don't" that helps to avoid conflicts, but the symbol might still be visible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding global variable in C
    By ThomasWink in forum C Programming
    Replies: 12
    Last Post: 01-06-2017, 10:23 AM
  2. Quick question about global variable...
    By JSlam in forum C Programming
    Replies: 3
    Last Post: 07-10-2011, 07:17 PM
  3. OOP question (global variable?)
    By VikingBow in forum C# Programming
    Replies: 5
    Last Post: 12-26-2007, 05:48 PM
  4. Global variable question
    By csisz3r in forum C Programming
    Replies: 10
    Last Post: 09-19-2005, 07:19 AM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM

Tags for this Thread