Declaring a global variable inside a function

Is it possible to declare a global variable in a function? If so, how?

Author: Vlad from Moscow, 2017-04-05

4 answers

You can declare a global variable in a function, that is, a variable with an external or internal binding. And, here, it is impossible to define such a variable, since a global variable is a variable defined outside of a function.

For example,

#include <stdio.h>

int x;

void f( int i )
{
    extern int x;
    x = i;
}   


int main(void) 
{
    printf( "x = %d\n", x );
    f( 10 );
    printf( "x = %d\n", x );

    return 0;
}

Program output to the console

x = 0
x = 10

In this program, the function f declares the global variable x

extern int x;
 5
Author: Vlad from Moscow, 2017-04-05 08:12:05

Declare or define?
Perhaps I am somewhat confused in the terminology, then let me be corrected, but if I understand correctly, then

int x = 5;  // Определение глобальной переменной


int main(int argc, const char * argv[])
{
    extern int x;     // Ее объявление, локальное для функции main()
    printf("%d\n",x);
}

Moreover, they can be located in different files.

 3
Author: Harry, 2017-04-05 08:09:28

No, it's not possible.

Everything that is declared in the function is visible only in it. This is the basic principle of structured programming, called "scope".

Only global variables have interprocedural access. It is highly desirable to use static-variables, at the level of .c-file, but in extreme cases it is permissible to use truly global variables, at the level of the entire program.

Certainly, may:

  1. Assign the modifier static to the local variable, then it will survive the exit from the function due to the special meaning of this keyword for local variables;

  2. Return a pointer to this variable from the function.

  3. Pass this pointer inside another function, which will store it inside its static local variable.

And it will work, since static local variables are not stored on the stack. However, it is better to move this variable to a structure that stores the state between calls, and pass this structure to both functions by pointer. Then:

  1. The first function will write data to an instance of the structure (initialize it);

  2. The second function will simply read and write to this structure.

So that outsiders can't to interfere with the array, use the "transparent pointer" design pattern: put both functions and the structure definition in a separate .c file, and provide the rest of the program with prototypes of functions and a brief structure definition sufficient to declare pointers to it (typedef struct MyStruct;).

Note that in any case, you will have to pass something as a parameter to the second function.

 2
Author: Arhadthedev, 2017-04-06 03:10:22

Reading the AUTHORS of the language:

Objects that are declared outside of all blocks at the same level as function definitions are always static, and they become global to the entire system. programs.

"C programming language" - Brian W. Kernighan, Dennis M. Ritchie

 1
Author: Sergey, 2017-04-06 02:25:51