True Global Variables

This is a discussion on True Global Variables within the C Programming forums, part of the General Programming Boards category; I want to be able to access a variable in one .c file that I declare in another .c file ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    True Global Variables

    I want to be able to access a variable in one .c file that I declare in another .c file the first #includes the second). How do I declare it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Quote Originally Posted by Bladactania
    I want to be able to access a variable in one .c file that I declare in another .c file the first #includes the second). How do I declare it?
    You declare it as extern in the various header files and/or source file and define it in exactly one source file.

    You do know that it is usually a good idea to avoid global variables, right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yes... It's not a permanent solution, just a temporary one.

    One question though, is it ok to use a "global" variable within one source file?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Quote Originally Posted by Bladactania
    One question though, is it ok to use a "global" variable within one source file?
    Maybe, in the same sense that it may be okay to use a global variable across different source files. Why do you need to use the global variable in the first place? A temporary solution can become a permanent solution that might actually be problematic in the future.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Well, I'm in the process of splitting a large source file into smaller source files to make it more organized. The original programmer used a large number of global scope variables that were used by the individual functions. Once I split them up, I want to make sure that I've cut/paste them correctly before I start making changes to the functions (adding parameters) so I know where any errors are coming from. So I want to make his globals global across the entire project for this reason.

    As far as using globals within one source file, I generally prefer to declar my constants at the very top of the source for easy editing. I suppose I could declare them within main.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Quote Originally Posted by Bladactania
    As far as using globals within one source file, I generally prefer to declar my constants at the very top of the source for easy editing.
    Oh, but global constants are not global variables. They are far less problematic to trace than global variables since they do not add state to the program (you can be pretty sure that the constant has the particular value.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I see. So global constants ok, global variables bad...

    Can you pass by reference in C or must I pass pointers?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Quote Originally Posted by Bladactania
    Can you pass by reference in C or must I pass pointers?
    No passing by reference in the language itself; pointers are used to simulate pass by reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by Bladactania View Post
    Yes... It's not a permanent solution, just a temporary one.
    Once your code grows and you start referencing this global variable all over the place, you'll find it becomes a lot less temporary

    One question though, is it ok to use a "global" variable within one source file?
    As usual, not preferred, but if you must use a file scope variable in a single module at least make it static.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Quote Originally Posted by brewbuck View Post
    Once your code grows and you start referencing this global variable all over the place, you'll find it becomes a lot less temporary
    Yeah I know what you mean. In this case though, the variables are already used all over the place, I'm in the procesws of editing someone elses code.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    What I usually use global variables for is when I have several small functions that are only a few lines long that all use the same variables. It doesn't sit well with me when the function declaration is longer than the function itself.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The usual cure against having a lot of parameters is to keep related variables together in a struct.

    Code:
    typedef struct
    {
        char* first_name;
        char* last_name;
        int id;
    } Employee;
    
    void do_something(Employee* emp);
    void interact(Employee* first, Employee* second);
    
    //vs.
    
    void do_something(char* first_name, char* last_name, int id);
    void interact(char* first_name_a, char* last_name_a, int id_a, char* first_name_b, char* last_name_b, int id_b);
    However, it is good to know (at the place of call) that these function work with the data of this or that employee, rather than limiting the usage only to some globally visible ones. If you know that the function is correct and it doesn't rely on globals, nothing you do later with other code breaks this function. You know what goes in and what will come out since it is tested (and it is easier to ensure that each single function in isolation is OK than to ensure that for a program where every part depends on every other part). Once tested, you can always just step over it in the debugger.
    Last edited by anon; 02-11-2009 at 11:54 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You know, that's a good idea! Thanks!

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm in the procesws of editing someone elses code
    I hear that! At work we're creating a new front-end to an already existing program with absolutely horrible architecture. But because we were specifically told to keep the design the same, our code is hideous...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  3. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  4. General global bool variables?
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 08:16 PM
  5. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21