Thread: Declaring Global Variables

  1. #1
    Registered User
    Join Date
    Jan 2008

    Declaring Global Variables


    I'm programming with Visual Studio Express Edition 2005 and my source code consists of many separate header files (.h) and .cpp that describe the various classes. I would like, then, to declare a global variable that would work on all of those classes and also on my main.
    Does anybody know how to do that?


  2. #2
    Join Date
    Oct 2007
    Inside my computer
    I suggest you avoid global variables and pass them through references between functions instead.
    But if you really must use a global variable, then define it is a .cpp file, then put an extern inside a header file and include that header where you want to use the global variable:

    int a;
    extern int a;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Thanks, Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  2. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. Global variables, or...
    By Vber in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 03:49 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM