Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Global Variable in C

The variables that are declared outside the given function are known as global variables. These do not stay limited to a specific function- which means that one can use any given function to not only access but also 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.

In this article, we will take a closer look at Global Variable in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Use of the Global Variable in C

The global variables get defined outside any function- usually at the very 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 declare a program, its global variable will be available for use throughout the running of the entire program.

Example of Global Variable in C

Here is a program that shows how we use global variables practically:

#include<stdio.h>

void func_a();

void func_b();

int x, y = 10; // declaration and initialization of global variables here

int main()

{

printf(“The Global x is = %d\n”, x);

printf(“The Global y is = %d\n\n”, y);

func_a();

func_b();

// a signal to the operating system that the program ran fine here

return 0;

}

void func_a()

{

printf(“From the func_a() the Global x is = %d\n”, x);

printf(“From the func_a() the Global y is = %d\n\n”, y);

}

void func_b()

{

int x = 5;

printf(“Inside the func_b() x is = %d\n”, x);

}

Output

The output obtained here will be:

The Global x is = 0

The Global y is = 10

From the func_a() the Global x is = 0

From the func_a() the Global y is = 10

Inside the func_b() x is = 5

You can notice that in line 4, x and y get declared as two of the global variables of the type int. Here, the variable x will get initialized automatically to 0. Then one can use variables like x and y inside any of the given functions. Also notice that inside the func_2() function, there exists a local variable that has the same name as the global variable. When we have a conflict between the local and global variables, then the precedence is for the local variable. That’s why the value of the local variable x gets printed inside the func().

The global variables, unlike the local variables, do not get destroyed with the end of a function. These are available for any function till the end of the execution of the program- thus named global.

How Does One Use Global Variables in C?

The global variables solve some very specific problems as it makes the declaration of the variable universal. This is the reason why any function present anywhere in a program becomes capable of accessing the variable. Thus, we do not need to pass or return it from a function.

To understand how we declare and use a global variable, you can toss around your age. The age and float global variables get affected by both the functions. We can pass them through the function, but cannot get their values in return (the C functions can return only a single value). Thus, in such cases, we use the global variable as a viable solution.

Redeclaration of Global Variable in C

The C language allows the redeclaration of the global variable. It means that this variable can get declared again when the first declaration doesn’t lead to the initialization of the variable.

Look at the two programs given below:

// First Program

int main()

{

int a;

int a = 7;

printf(“%d”, a);

return 0;

}

The output obtained in the C language will be:

redeclaration of ‘a’ with no linkage

// Second Program

int a;

int a = 7;

int main()

{

printf(“%d”, a);

return 0;

}

The output obtained in the C language will be:

7

It is possible because the second program works pretty well in the C language even if the first one fails during compilation. On the other hand, in the C++ language, both of these programs fail during the time of compilation.

Let us look at another program below that fails in C, as the initialization of the global variable occurs in the first declaration itself.

int a = 15;

int a = 30;

int main()

{

printf(“%d”, a);

return 0;

}

The output obtained here would be:

error: redefinition of ‘a’

Practice Problems on Global Variable in C

1. Which of these is a global variable in the given code?

#include<stdio.h>

int x=50, y=40;

void main()

{

printf(“x = %d and y=%d”,x,y);

}

A. x

B. y

C. Both

D. None

Answer – C. Both

2. The statement “When a variable gets assigned a value inside a function, it becomes a global variable automatically” is:

A. False

B. True

C. Undefined

Answer – A. False

When a variable gets assigned a value inside a function, it becomes a local variable automatically. Thus, the statement mentioned above is false.

3. Which of these statements is true for global variables in C?

1. All the functions that are present in a program can access the global variables present in the program.

2. It only requires a single declaration.

3. Global variables are very helpful if all the functions present in a program need to access the same set of data.

A. 1 and 3

B. 2 and 3

C. 1, 2, 3

D. 1 and 2

Answer – C. 1, 2, and 3


FAQs

Q1

Is it mandatory to declare a global variable during the beginning of a program?

Yes, it is. This way, all the functions get access to the variable throughout the time when that program is in running.

Q2

Can we declare the global function outside the block?

The declaration of a global variable occurs only outside the blocks or functions.

Q3

What is the difference between a local and a global variable?

The primary difference is that the declaration of a local variable occurs inside a function while that of a global variable occurs outside of it. Due to this, we lose the local variables once the function in which we executed it terminates. But the global variable stays intact, and we only lose them once the execution of the entire program terminates.

Keep learning and stay tuned to get the latest updates on GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*