Knowee
Questions
Features
Study Tools

define Local and Global variables and the difference between them

Question

define Local and Global variables and the difference between them

🧐 Not the exact question you are looking for?Go ask a question

Solution

Local Variables: Local variables are declared inside a function or block. They are created when the function starts execution and are destroyed as soon as the function is completed. They are not known to the functions outside their own. If you try to access them outside their function or block, the compiler throws an error.

Global Variables: Global variables are declared outside all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

Difference between Local and Global Variables:

  1. Scope: The main difference between local and global variables is that the local variable is declared within a function whereas a global variable is declared outside the function.

  2. Lifetime: Local variables are created when the function is called and destroyed when the function is completed. On the other hand, global variables are created when the program starts and destroyed when the program stops.

  3. Accessibility: Local variables can only be used inside the function or block they are declared in. In contrast, global variables can be used by any part of the program.

  4. Memory: Global variables are stored in a fixed memory location defined by the operating system. Local variables are stored in the stack.

  5. Default Value: Uninitialized global variable stored zero as a default value. But, the local variable stored garbage value if not initialized.

This problem has been solved

Similar Questions

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); }

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

Fill in the blanks. __________are the variables declared inside a function. a. Immediate variables b. Global variables c. Local variables d. None of these

Where are variables with global scope accessible?

Explain global, build-in variables with an example

1/3

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.