Variable Scope

This is a discussion on Variable Scope within the C Programming forums, part of the General Programming Boards category; Hi I was just wondeing if it was possible to create a variable inside a function but give it a ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Variable Scope

    Hi I was just wondeing if it was possible to create a variable inside a function but give it a global scope.

    For example...
    Code:
    int main()
    {
    
    funtion();
    
    x = 2;
    }
    Code:
    void function()
    {
    int x;
    }
    I know the easy way would be to just put it in main, but this is more for my curiosity as such

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    No, you can't do that. Global variables are allocated on the heap while local variables are allocated on the stack. When your function returns, the stack space used by the function is free and can be used by the next function which will be called or occupied by other variables in your code. Global data on the other hand is not popped out in this fashion when functions return.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by PING View Post
    No, you can't do that. Global variables are allocated on the heap while local variables are allocated on the stack. When your function returns, the stack space used by the function is free and can be used by the next function which will be called or occupied by other variables in your code. Global data on the other hand is not popped out in this fashion when functions return.
    Whilst you are right that local variables are on the stack, and thus disappear when the called function returns, the global variables actually get their space allocated at runtime in a region called "Data" (or some similar name, differnet OS/Compiler variants vary a bit). The heap is used for malloc in C and new in C++ to create permanent space for variables when the application is running.

    Note also that there is the possibility of having a "local data space", which is a "static local" - e.g.
    Code:
    void function(void)
    {
        static int x = 7;
      // change x. 
    }
    In this case, x is not on the stack, it is a global variable, but only visible within this function. The purpose of this is to "remember" things from one call to another, for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by Matty_Alan View Post
    Hi I was just wondeing if it was possible to create a variable inside a function but give it a global scope.
    No, it's not possible, nor should it be, because it would make it global, thus making it no different from a global variable.
    If you want functions to communicate with each other, they can pass arguments between each other, or they can return values.

    T* <--- return type
    function_name
    (
    T arg1 <--- Parameter (argument)
    );

    T* function_name(T arg1);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 02:25 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  5. variable in file scope ???
    By howhy in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 04:46 AM

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