Global Variables in C

By Anjnee Bhatnagar|Updated : June 16th, 2022

Global variables in C: A variable in a programming language is of significant use. Every programming language has different types of variables and their usage and syntaxes to be followed in order to make efficient use of the memory. We have different types of variables like the local variables, static variables, and global variables in the C language.

In this article, we will learn about the meaning of a variable in terms of programming languages, the definition of global variables in C, and the use of the global variables in C.

Table of Content

What is a Variable?

A variable is nothing but a name for a memory location. A variable also called an identifier, has the range and scope according to the data type it is mentioned for. Every variable has a data type associated with it to specify the range and size, etc.

In C, there are majorly three types of variables based on the scope they are local variables, static variables, and global variables. A local variable has local scope within the block it is declared in. A static variable creates memory at the compile time and retains its value throughout the program execution. A global variable is one that is declared outside of any block or function. Now let us dive deep into the meaning of global variables in C.

Define a Global Variable in C?

A global variable in C is defined as one that is declared outside of any block or function. The value of a global variable can be changed by any function and any number of times. Its value can be altered and reused many times.

As the name itself suggests, the scope of the global variable is global, or it can be accessed by any member of the program. On the other hand, a local variable has local scope. A global variable is declared usually just before the main function. The default value of a global variable in C is set to zero(0).

Use of Global Variables in C

As we have discussed the definition of global variables, we now know that they are of great use. We define a global variable at the very top of the program. These variables hold their value throughout the lifetime of the program.

Any block or function in the program can access the global variables. This means that once declared, we can use global variables as many times as required, and they are available for use for the entire lifetime of the program.

Syntax and Example for Global Variable in C

The syntax of a global variable is the same as any other variable declaration in C. That is data_type followed by the variable_name;

Example: int value;

However, the global variables are declared outside of any block or function. It is declared just before the main function of the program. The declaration of the global variable will be clear by seeing the following example:

int value=9;

#include<stdio.h>

void main()

{

printf(“%d”, value);

return 0;

}

Output: 9

Comments

write a comment

FAQs on Global Variables in C

  • A global variable in C is a variable that is defined outside of the supplied function. These are not limited to a single function, which means that any function can be used to not only access but also modify global variables.

  • Based on the type and scope, there are mainly three categories of variables in C language, and they are as follows:

    • Local variable: Has local scope and declared within a block of code.
    • Static variable: They create space in the memory at the compile-time only.
    • Global variables: They are declared outside of any function and contain 0 as the default value.
  • Yes, it is mandatory to declare the global variables right at the beginning of the program. Declaring at the starting makes other functions and blocks in the program easier to access the global variables.

  • As the name itself suggests the fundamental difference between the two, a local variable is declared inside a function and has the scope within the function only. In contrast, the global variables are declared outside of any function and have global scope throughout the entire execution of the program.

  • A global variable is declared outside of any function or block of code. It is declared just before the main function. It may be declared before or after the header files declaration. A global variable can be accessed globally in the program.

Follow us for latest updates