global variable and prototype

This is a discussion on global variable and prototype within the C++ Programming forums, part of the General Programming Boards category; I have two different codes here: 1) Code: #include <iostream> using namespace std; short Test; //This is the global variable ...

  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
    22,303
    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.
    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 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
    22,303
    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.
    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

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, 05: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, 06:36 AM
  3. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 04: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, 09:45 PM

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