Provide the correct answer(s) in the blank(s) Global variables are declared outside of any function. A global variable is visible to any every function and can be used by any piece of code. Unlike local variable, global variables retain their values between function calls and throughout the program execution. Let us consider an example: #include < stdio.h > int a = 20; // Global declaration void test(); void main() { printf("In main() function a = %d\n", a); // Prints 20 test(); a = a + 15; // Uses global variable printf("In main() function a = %d\n", a); // Prints 55 } void test() { a = a + 20; // Uses global variable printf("In test() function a = %d\n", a); // Prints 40 } In the above code the global variable a is declared outside of all the functions. So, the variable a can be accessed in every function. Operating System calls the main() function at the time of execution. the variable a has no local declaration, so it access the global variable a. In test() function also there is no local declaration of variable a, the variable a gets access from the global. The global variables are destroyed only after completion of execution of entire program. What is the output for the following code #include <stdio.h> int a = 10; void test(); void main() { printf("In main() function a = %d\n", a); test(); a = a + 10; printf("In main() function a = %d\n", a); } void test() { a = a + 20; printf("In test() function a = %d\n", a); }
Question
Provide the correct answer(s) in the blank(s) Global variables are declared outside of any function.
A global variable is visible to any every function and can be used by any piece of code.
Unlike local variable, global variables retain their values between function calls and throughout the program execution.
Let us consider an example: #include < stdio.h > int a = 20; // Global declaration void test(); void main() { printf("In main() function a = %d\n", a); // Prints 20 test(); a = a + 15; // Uses global variable printf("In main() function a = %d\n", a); // Prints 55 } void test() { a = a + 20; // Uses global variable printf("In test() function a = %d\n", a); // Prints 40 } In the above code the global variable a is declared outside of all the functions. So, the variable a can be accessed in every function.
Operating System calls the main() function at the time of execution. the variable a has no local declaration, so it access the global variable a.
In test() function also there is no local declaration of variable a, the variable a gets access from the global.
The global variables are destroyed only after completion of execution of entire program.
What is the output for the following code #include <stdio.h> int a = 10; void test(); void main() { printf("In main() function a = %d\n", a); test(); a = a + 10; printf("In main() function a = %d\n", a); } void test() { a = a + 20; printf("In test() function a = %d\n", a); }
Solution
The output for the provided code will be:
In main() function a = 10
In test() function a = 30
In main() function a = 40
Explanation:
-
The global variable 'a' is initialized with the value 10.
-
The main() function is called first, where it prints the current value of 'a', which is 10.
-
Then, the test() function is called. Inside this function, 20 is added to the current value of 'a', making 'a' equal to 30. This new value is then printed.
-
After the test() function finishes execution, control returns to the main() function. Here, 10 is added to the current value of 'a' (which is now 30), making 'a' equal to 40. This final value is then printed.
Similar Questions
Which one is false regarding global variables?Global variables can only be read inside the function declaring the variable as global inside the function.Global variables remain in memory till the end of the programGlobal variables are those which are declared in global scope.None of the above
define Local and Global variables and the difference between them
Where are variables with global scope accessible?
Which of the following is true about Python's global keyword?AIt can only be used inside functionsBIt makes a variable local to the functionCIt declares a variable as globalDIt prevents the use of global variables
What is true about the globals() function in Python?It returns a list of all global variables in the current scope.It deletes all global variables.It returns a dictionary of all global variables in the current module.It creates new global variables
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.