answersLogoWhite

0


Best Answer

When you acess a global variable inside main function you must use the same name, because the variable declared as global can be accessed by any function/procedure on the class where it was defined.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you access global variable from within the main function when the local variable is of the same name?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a global declaration?

A global declaration of a function or variable is a declaration that is at the global scope of the program, not inside another function. This means that the name will be visible within all functions in the program.


What is global variable in C programming?

A global variable is a variable that is declared at global scope, rather than file, namespace, function, class or nested scope. Global variables are usually declared with external linkage within a header and initialised in one (and only one) source file. Any file that includes the header (which includes the source file that initialised the global variable) then has unrestricted access to the variable. It is globally visible and any code can alter it. Global variables should be used sparingly and only when absolutely necessary. If the vast majority of the functions in your program require access to a particular variable, then a global variable makes perfect sense and is by far the simplest solution. However, a variable that is only used by a handful of functions can hardly be described as a global entity, thus it has no place within the global namespace and should be scoped to those functions that actually require it instead.


What are local variables and global variables?

Local variables: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. Global variables: These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


How do you declare local and global variables in pseudo code?

Pseudocode is not a programming language (it's specifically intended for human interpretation), so there is no need to declare variables, you simply define them as and when you require them. For instance: Let x = 42 Let y = x * 2

Related questions

What is a global declaration?

A global declaration of a function or variable is a declaration that is at the global scope of the program, not inside another function. This means that the name will be visible within all functions in the program.


What is the difference between global variable and extern variable?

Global variables are non-local variables. That is, variables that are not defined in a function or class. They are globally accessible to all code in the same translation unit. External variables are global variables that have external linkage; they are accessible across translation units.


What is global variable in C programming?

A global variable is a variable that is declared at global scope, rather than file, namespace, function, class or nested scope. Global variables are usually declared with external linkage within a header and initialised in one (and only one) source file. Any file that includes the header (which includes the source file that initialised the global variable) then has unrestricted access to the variable. It is globally visible and any code can alter it. Global variables should be used sparingly and only when absolutely necessary. If the vast majority of the functions in your program require access to a particular variable, then a global variable makes perfect sense and is by far the simplest solution. However, a variable that is only used by a handful of functions can hardly be described as a global entity, thus it has no place within the global namespace and should be scoped to those functions that actually require it instead.


What are local variables and global variables?

Local variables: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. Global variables: These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What do you call a variable of a function that is active only within the procedure or its function?

local variable


How do you declare local and global variables in pseudo code?

Pseudocode is not a programming language (it's specifically intended for human interpretation), so there is no need to declare variables, you simply define them as and when you require them. For instance: Let x = 42 Let y = x * 2


Why global variables need to be kept to a minimum?

Polluting the global namespace with variables that have no reason to be global is never a good idea. Aside from anything else, the programmer has no control over what code may access a global, and global variable declarations may be spread over many files or hidden within headers, making it difficult to get an overview of every global. Globals are not necessarily a bad thing; if a concept is global in nature then of course make it global. But don't make variables global in the mistaken belief that it is somehow convenient. If a variable really needs to be shared amongst a specific set of functions then there are far better ways of doing so without making the variable global to all functions. Declaring the variable locally in a function (even the main function as a static variable) and passing it to the functions that operate upon it, or by encapsulating the variable in a class along with the functions that operate upon it are far better options than making a variable globally accessible. Keeping variables as close to the code that operates upon them makes your code easier to both read and maintain.


Difference between global and local variables?

The scope or the life span. The life span is from the birth (allocation) to death (deallocation). The life span of a global variable starts when the application is invoked. It dies when the application terminated. During the execution of the application , this global variable is available to any program unit within that application. The life span of a local variable, well, only locally. "Local" means a component, a method (subroutine), or even within a bracket ({} or BEGIN-END) of a statement. Outside of that block of codes, that local variable does not exist, hence cannot be accessed or referenced. Also, the same name of that local variable may be declared again in another local area without conflict, nor memory of the previous one. For languages similar to C: for (int i = 0; i < 100; i++) { // i can be referenced within { }, and within the for() control itself } for (int i = -1; i > -200; i--) { // this i is different from the i in the previous for-loop } Some computer language would allow you to declare a local variable with the same name as the global one (for example, there are global::X and local::X, but the code only refer as X). Most of the computer language would replace X with local::X at compile time, some would replace it with global::X) Some languages may not have the notion of local variables, everything is global, and some would not have the notion of global variable, but would provide a mean to act like one.


Why can't a global variable can be viewed only within the module in which its declared?

Because that's what global means.


What is the difference between local variable and global variable in Embedded C?

It's simple. A global variable has a scope through out out the C program. It can be accessed anywhere from any function or etc. A local variable on the other hand, is local to it's container only and can not be accessed outside of it's container. For example a function has variable sum then sum is only accessible within the function and not anywhere else.


What is the difference between local and global variable of C language?

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.