How to access global variable if there is a local variable with same name in C/ C++?

Many newbies ask these questions, “How to access global variable if there is a local variable with the same name in C/ C++?”, “What happens if the local and global variable has the same name?” or “Can a local and a global variable have the same name in C”. So in this post, I will give the answer to the question how do you access a global variable from inside a function in case you have a variable with the same name inside that function?

Before giving the answer to the question I want to give a small introduction of a local and global variable.

Local variable in C/C++:

A local variable is a variable whose scope lies inside a function or a block in which they are declared. It could be static or non-static. Consider the below example,

/*
a, b and x, y are
local variable
*/
void foo(int x, int y)
{
    int a;
    static int b = 0;
}

 

Global variable in C/C++:

A global variable is a variable declared outside the function. A global variable is not limited to any function or file it can be accessed by any function or outside of the file. Consider the below example,

#include<stdio.h>

int data; // global variable

int main()
{
    printf("data = %d\n", data);

    return 0;
}

Output:data = 0

 

I believe you are familiar with the problem which I am going to solve. But still before explaining the solution I want to explain the problem. The problem is that the same variable name in the larger scope is overridden by local variable references in the function or block where it is defined.

The following example shows how a local variable overrides the variable of the same name of larger scope.

#include <stdio.h>

int data = 10;

int main(void)
{
    /*local variable name
    same as global variable*/
    int data = 27;
    printf("local data = : %d\n", data);
    {

        printf("global data = %d\n", data);
    }

    return 0;
}

Output:

local data = : 27
global data = 27

You can see the local variable data  override to the global variable data . But I want to print the global variable besides the local variable. So now let’s see the solution.

 

Solution in C Programming:

 

1. Use the extern specifier:

If you are familiar with the compilation process, then you know that the linker resolves the cross-module references and fixes the addresses. According to C standards, “An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage”.

So here I will use the extern keyword to solve the problem. The extern keyword establishes external linkage which solves our problem.

#include <stdio.h>

int data = 10;

int main(void)
{
    /*local variable name
    same as global variable*/
    int data = 27;
    printf("local data = : %d\n", data);
    {
        extern int data;
        printf("global data = %d\n", data);
    }

    return 0;
}

Output:

local data = : 27
global data = 10

 

2. Using a hack (But not recommended):

 

#include <stdio.h>

int data = 10;

int main()
{
    /*
      pointer hold the address
      of the global data variable
    */
    int * const ptrData = &data;

    /*local variable name
    same as global variable*/
    int data = 27;
    printf("local data = : %d\n", data);
    {
        printf("global data = %d\n", *ptrData);
    }

    return 0;
}

Output:

local data = : 27
global data = 10

 

 

Solution in C++ Programming:

 

Using Scope resolution operator (::):

In C++, we can use the scope resolution operator (::) to access a global variable if we have a local variable with the same name.

#include <stdio.h>

int data = 10;

int main()
{
    /*local variable name
    same as global variable*/
    int data = 6;
    printf("local data = : %d\n", data);
    {
        //calling global data using ::
        printf("global data = %d\n", ::data);
    }

    return 0;
}

Output:

local data = : 6
global data = 10

 

Recommended Posts for you

Leave a Reply

Your email address will not be published. Required fields are marked *