answersLogoWhite

0


Best Answer

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist.

{ // beginning of a scope, i does not yet exist

int i = 42; // local variable declared, i now exists

} // end of scope, i no longer exists

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In most languages where does a local variables scope begin and end?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


What are the advantages of local variables in programming?

1. Local variables cannot be used by other forms. 2. Cannot be used globally. 3. They can slowdown the compiling process.


Which variable is not destroyed on exit from the function instead its value is presented and becomes available again when the function is next called These variables are declared as?

Local function variables defined static remain in memory at all times. Such variables are only in scope (accessible) when the function itself is in scope.


What is meant by delare or initialize a variable or constant at a lowest possible level?

It probably refers to "scope" (see http://en.wikipedia.org/wiki/Scope_(programming)). In programming languages with lexical scope, variables declared in an outer scope can be used in an inner scope, but variables declared in an inner scope cannot be used in outer scopes. It is considered best practice to declare variables (and constants, which are just variables that don't change) at the innermost scope possible for several reasons: # It makes it most clear what the scope of use is of the variable. # It makes it impossible to mistakenly use it in some other location. # It makes it easier to keep track of what variables exist at any given point in the code. For example, in standard C, nested functions are not allowed. This means that in any function, only two types of variables exist - global variables, and variables declared within that function. This has the advantage of making it easy to understand what any variable refers to.


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.

Related questions

What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


What are the advantages of local variables in programming?

1. Local variables cannot be used by other forms. 2. Cannot be used globally. 3. They can slowdown the compiling process.


Which variable is not destroyed on exit from the function instead its value is presented and becomes available again when the function is next called These variables are declared as?

Local function variables defined static remain in memory at all times. Such variables are only in scope (accessible) when the function itself is in scope.


What is meant by delare or initialize a variable or constant at a lowest possible level?

It probably refers to "scope" (see http://en.wikipedia.org/wiki/Scope_(programming)). In programming languages with lexical scope, variables declared in an outer scope can be used in an inner scope, but variables declared in an inner scope cannot be used in outer scopes. It is considered best practice to declare variables (and constants, which are just variables that don't change) at the innermost scope possible for several reasons: # It makes it most clear what the scope of use is of the variable. # It makes it impossible to mistakenly use it in some other location. # It makes it easier to keep track of what variables exist at any given point in the code. For example, in standard C, nested functions are not allowed. This means that in any function, only two types of variables exist - global variables, and variables declared within that function. This has the advantage of making it easy to understand what any variable refers to.


The language like 'c' allows the use of same name for two different variables Is it possible for lexical analyser to distinguish between such variables?

Using the same name for two different variables in languages like C is possible only within two different scopes, such as outside a block and inside a block... int somevariable; /* outside scope */ { ... int somevariable; /* inside scope */ } In order for a lexical analyzer to distinguish those two variables, it must be able to parse the program and resolve scope the same way the compiler does.


What is the scope of register variables?

The block they are declared in.


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.


What is meant by the phrase one block of code can be nested in another?

It simply means one code block contains another code block. How you achieve this depends on the language, but in many languages we use opening and closing braces to define a code block. function foo () { // start of function block { // start of nested block // ... } // end of nested block } // end of function block Nested code blocks are typically used to define a new scope within an existing scope. Any variables declared within the nested block are local to the scope of that block and will fall from scope when we exit the nested block. This can be useful when we want to instantiate new variables within a larger scope but want to localise them to the code block that actually uses them. In some languages (such as C++) this also allows us to redefine names that were initially defined by the enclosing scope, temporarily hiding the original names from within the nested scope (only the local names are visible). When we exit the nested block, the original names become visible again. However, referring to two separate variables by the same name within two scopes of the same function can make code difficult to read, so this is best avoided.


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.


What is automatic storage class specifiers?

Automatic storage is the default storage class for all non-static local variables including formal arguments. All automatic variables are allocated on the call stack and are automatically released when they fall from scope.


Should you avoid local variable scope?

No. In most designs, local variables are considered a good thing thanks to the isolation that they provide (no other code can "abuse" your variable). However, it is important to distinguish between local static variables, local automatic variables, and locally made references to the heap. Local static variables are initialized only once at application initialization time, and retain their most recently assigned value over multiple invocations of the function which contains them. Local static variables must be used with care and can lead to non-reentrant code (the famous example being the strtok() CRT function). However, these variables are usually great to track the total number of something, or the minimum or maximum value of a particular item, etc. In C, local automatic variables are the most common form of local variables. These are typically allocated on the stack, so care must be taken when declaring large automatic variables, or when using recursion. When the size of a local automatic variable is a concern, dynamic memory management method should be considered those maintain a local auto pointer on the stack but place the data in the heap. This method allows to allocate large amounts of data without burden on the stack, and the lifetime of such data is no longer limited by the lifetime of the scope in which it was allocated. Very rare cases exist where local variables are generally avoided; these are mostly in the area of very resource limited embedded programming.