Results 1 to 6 of 6

Thread: Declaring global variables inside a function, in C?

  1. #1
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Declaring global variables inside a function, in C?

    This is a very dumb question but haven't still found answer.
    Keyboard not found!

    Press any key to continue...

  2. #2
    Join Date
    Jul 2005
    Location
    Belgium
    Beans
    258
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Declaring global variables inside a function, in C?

    Declare them static, e.g.
    Code:
    static int i;
    What exactly is your purpose?

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Declaring global variables inside a function, in C?

    Not a dumb question at all.

    The answer is that it cannot be done. The variable will only have scope within the function.

    Global variables must be declared outside of a function scope (and before you think otherwise, main() is a function too). Globals are declared within header files or within .c modules.

    P.S. I should point out that when developing a project with multiple .c modules, one of them must instantiate the global variable; the other(s) must declare it as an "extern" variable. Otherwise if two or more modules each have their own instantiations of the variable, the compiler will not be happy with that.
    Last edited by dwhitney67; November 11th, 2008 at 04:47 PM.

  4. #4
    Join Date
    Nov 2008
    Beans
    10

    Re: Declaring global variables inside a function, in C?

    Why do you wish to declare a global variable inside a function. The variable wouldn't be known to other functions.

    See also http://www.cplusplus.com/doc/tutorial/variables.html

    It would be easier to declare the variable outside the function and give it a value inside it. Or even better take a pointer outside the function with the value NULL and inside the function set it to the correct value.

  5. #5
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: Declaring global variables inside a function, in C?

    I wanted to have an initializition function which declares some variables and makes them global, because I thought it's one keyword which makes them global.

    Doesn't matter me much. Thanks.
    Keyboard not found!

    Press any key to continue...

  6. #6
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Declaring global variables inside a function, in C?

    Quote Originally Posted by crazyfuturamanoob View Post
    I wanted to have an initializition function which declares some variables and makes them global, because I thought it's one keyword which makes them global.

    Doesn't matter me much. Thanks.
    You could declare the variables as global, then use a function to give them a value.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •