Thread: global variables

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    global variables

    hi,

    i have a question about global variables.for example(sorry i just made up its not a good example,the purpose me to understand how it works).i define a global variable its called age;

    1-if i use the age again somewhere else(in a function or in the main body).what the age`s value ?i mean will the program hold its first value(20)?or the value after process ?i mean do the global variables always contain their first value?



    Code:
    #include<iostream>
    
    int age=20;
    
    int main()
    {
    
    age=age+2;
    
    return 0;
    
    }

    thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    the age in main body refers to the global variable age, after the statement age = age + 2; the value after process is 22. If you still use age in somewhere else after process, the value is still 22.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  2. Global variables
    By guillermoh in forum C Programming
    Replies: 1
    Last Post: 02-07-2008, 08:02 PM
  3. Global Variables
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2006, 03:44 AM
  4. non global variables
    By algi in forum C++ Programming
    Replies: 1
    Last Post: 01-10-2005, 01:02 PM
  5. Global Variables?
    By Da-Spit in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2002, 08:05 AM