Global variables in C

Global variables are variables declared outside a function. Unlike local variables and static variables, a global variable is not declared inside a function.

Properties of a global variable

  1. Global variables are allocated within data segment of program instead of C stack.
  2. Memory for global variable is allocated once and persists throughout the program.
  3. They are accessible to all function of the same and other programs (using extern).

Note: Both static and global variables gets their memory within data segment and persists throughout the program. However, a global variable is accessible to all functions of same as well as other program. Whereas, static variables are accessible only to the same function.

Based on scope global variables are categorized in two categories.

  1. Global scope
  2. Static file scope

Global scope

By default, global variables are of global scope. Which means we can access a global variable everywhere in same as well as other C programs (using extern).

Example program to use global scope variables

First let us create a C program that contains only global variables, save the below program with name global.c.

/**
 * Global variable declarations
 */
int num1;
int num2;

Next, let us write main program to test above global variables in different program, save the below program with name main.c.

#include <stdio.h>

/* Link global variable declared in global.c to this program */
extern int num1, num2;

int main()
{
    // Access a global scope variable 
    num1 = 10;
    num2 = 20;

    printf("num1=%d, num2=%d", num1, num2);

    return 0;
}

Output -

gcc global.c main.c -o main
main
num1=10, num2=20

Static scope

Global scope variables are accessible to all functions of same as well as other program. In programming, there exists situations when you want to restrict access of a global variable only to all functions of the same program.

Static scope global variables has all properties of a global variable, except they are accessible only to all functions of same program. They are declared with static keyword.

Example program to use static scope global variable

Let us re-write the above program with little modifications and convert global scope variables to static scope global variables.

First modify global.c program with static scope global variables.

/**
 * Static scope global variable declarations
 */
static int num1;
static int num2;

I will leave main.c program as it is and compile the two programs.

#include <stdio.h>

/* Link global variable declared in global.c to this program */
extern int num1, num2;

int main()
{
    // Access a global scope variable 
    num1 = 10;
    num2 = 20;

    printf("num1=%d, num2=%d", num1, num2);

    return 0;
}

The C compiler reports errors on compilation of main.c. Since we are binding static scope global variables num1 and num2 to main.c program. Hence, compiler reports errors as,

undefined reference to `num1'
undefined reference to `num2'

When to use a global variable?

You should try to minimize the use of a global variable as much as you can. A global variable has highest scope. Which increases difficulty level to debug and maintain code, since many functions modifies the same variable.

However, global variables are best suited when most of your functions share common variables.

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>