Thread: How to declare a global variable in Main()

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    How to declare a global variable in Main()

    Please help! Can someone tells me how to declare a globle variable in Main()? I really appreciate!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A global variable is a variable which is defined outside all functions. Hope the example explains:

    Code:
    #include <stdio.h>
    
    int global_var = 5;
    
    int main ()
    {
        printf ("Global variable global_var has value %d\n", global_var);
        return 0;
    }
    A variable declared in a function is only known by that function and therefore called local.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you Shiro. I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  2. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 02:30 PM
  3. Refering to a global variable in a different object file
    By Canadian0469 in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 08:53 PM
  4. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  5. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM