Thread: global variable and prototype

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    17

    global variable and prototype

    I have two different codes here:

    1)
    Code:
    #include <iostream>
    
                      using namespace std;
    
                      short Test; //This is the global variable
    
                      void Testing(); //This is the function
    
                      int main()
                      {
                       blahblahblah
                       }

    2)
    Code:
    #include <iostream>
    
                      using namespace std;
    
                      void Testing(); //This is the function
    
                      short Test; //This is the global variable
                   
                      int main()
                      {
                       blahblahblah
                       }
    Which of the 2 is better? Should the global variable be placed at on top of void Testing or placed below of Testing. I tried it out and both is the same. Is there something which I don't know of? Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Neither, if you can reasonably avoid the global variable. Other than that, the placement of the definition of a global variable with respect to the placement of a function declaration (prototype) does not matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I assume you are asking taking into account what looks better?
    I would then say that if you have a big program with a lot of global variables and lost of function declarations, I would personally (others might like sth else) do this:
    1) Put first the function declaration. Most likely when viewing a code you will want to check the global variables more often, so it is a good idea to be nearer main(). And it just seems more right
    2) It is a good idea, in any case, to put labels. Like:
    /********************
    *** functions *******
    *******************/
    void Test();


    /********************
    *** global variables **
    *******************/
    int gb_a, gb_b;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In a "big" program, the function declarations would be in header files (perhaps within class definitions), with global variables (if any) declared extern and only defined in one source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need explanation of variable arg lists
    By Countfog in forum C Programming
    Replies: 2
    Last Post: 05-04-2008, 04:34 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM