The Wayback Machine - https://web.archive.org/web/20240624012729/https://www.geeksforgeeks.org/global-variables-in-c/
Open In App

Global Variables in C

Last Updated : 07 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Variables in C

In a programming language, each variable has a particular scope attached to them. The scope is either local or global. This article will go through global variables, their advantages, and their properties.

The Declaration of a global variable is very similar to that of a local variable. The only difference is that the global variable is declared outside any function. We can take an example by assuming that we have a chair at our house and one in our school/college then we can say that the chair at our home can only be accessed by the people living inside the home but the chair in our college can be used by any student or faculty.

Example:

C




// C program to show declaration of global variable
#include <stdio.h>
 
int x = 5; // global variable
int main() {
 
      int y = 10; // local variable
    return 0;
}


Global variables do not stay limited to a specific function, which means that one can use any given function to access and modify the global variables. The initialization of these variables occurs automatically to 0 during the time of declaration. Also, we generally write the global variables before the main() function.

Use of the Global Variable

The global variables get defined outside any function- usually at the very beginning/top of a program. After this, the variables hold their actual values throughout the lifetime of that program, and one can access them inside any function that gets defined for that program.

As already stated earlier, any function can access a global variable. It means that once you execute a program, its global variable will be available for use throughout the running of the entire program.

Advantages of Global Variable

  • Global variables can be accessed by all the functions present in the program.
  • Only a one-time declaration is required.
  • Global variables are very useful if all the functions are accessing the same data.

Disadvantages of Global Variable

  • The value of a global variable can be changed accidentally as it can be used by any function in the program.
  • If we use a large number of global variables, then there is a high chance of error generation in the program.

Example:

C




// C program to update global variables
#include <stdio.h>
 
int a, b; // global variables
 
void add()
{ // we are adding values of global a and b i.e. 10+15
    printf("%d", a + b);
}
 
int main()
{
    // we are now updating the values of global variables
    // as you can see we dont need to redeclare a and b
    // again
    a = 10;
    b = 15;
    add();
    return 0;
}


Output

25


Previous Article
Next Article

Similar Reads

Can Global Variables be dangerous ?
In a small code, we can track values of global variables. But if the code size grows, they make code less understandable (hence less maintainable). It becomes difficult to track which function modified the value and how. C/C++ Code // A C++ program to demonstrate that a // global variables make long code less // maintainable. int c = 1; int fun1()
2 min read
Initialization of global and static variables in C
Predict the output of following C programs. // PROGRAM 1 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void) { static int *p = (int*)malloc(sizeof(p)); *p = 10; printf(&quot;%d&quot;, *p); } // PROGRAM 2 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int *p = (int*)malloc(sizeof(p)); int main(void) { *p = 10; printf(&quot;%d&quot;
1 min read
Difference between Static variables and Register variables in C
static variables Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; register variables Registers are faster than memory to access
2 min read
How to Access Global Variable if there is a Local Variable with Same Name in C/ C++?
Local Variable: The variable whose scope lies inside a function or a block in which they are declared. Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolutio
2 min read
How Linkers Resolve Global Symbols Defined at Multiple Places?
At compile time, the compiler exports each global symbol to the assembler as either strong or weak, and the assembler encodes this information implicitly in the symbol table of the relocatable object file. Functions and initialized global variables get strong symbols. Uninitialized global variables get weak symbols. For the following example progra
4 min read
Redeclaration of global variable in C
Consider the below two programs: // Program 1 int main() { int x; int x = 5; printf(&quot;%d&quot;, x); return 0; } Output in C: redeclaration of ‘x’ with no linkage // Program 2 int x; int x = 5; int main() { printf(&quot;%d&quot;, x); return 0; } Output in C: 5 In C, the first program fails in compilation, but second program works fine. In C++, b
1 min read
How will you show memory representation of C variables?
Write a C program to show memory representation of C variables like int, float, pointer, etc. Algorithm: Get the address and size of the variable. Typecast the address to char pointer. Now loop for size of the variable and print the value at the typecasted pointer.Program: C/C++ Code #include &amp;lt;stdio.h&amp;gt; typedef unsigned char *byte_poin
1 min read
How are variables scoped in C - Static or Dynamic?
In C, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. For example, output for the below program is 0, i.e., the value returned by f() is not dependent on who is calling it. f() always returns the value of the global variable x
1 min read
What are the default values of static variables in C?
In C, if an object that has static storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a NULL pointer; — if it has an arithmetic type, it is initialized to (positive or unsigned) zero; — if it is an aggregate, every member is initialized (recursively) according to these rules; — if it is a union, the
2 min read
Static Variables in C
Static variables have the property of preserving their value even after they are out of their scope! Hence, a static variable preserves its previous value in its previous scope and is not initialized again in the new scope. Syntax: static data_type var_name = var_value;Following are some interesting facts about static variables in C: 1) A static in
4 min read
Article Tags :
three90RightbarBannerImg