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

A local variable is one that occurs within a specific scope. They exist only in the function where they are created.

They are sometimes called automatic variables because they are automatically created when the function starts execution, and automatically go away when the function is finished executing.

The keyword auto can be used to explicitly create these variables, but isn't necessary since auto is the default.

Global Variables and extern

A global variable is a variable that is defined outside all functions and available to all functions.

These variables are unaffected by scopes and are always available, which means that a global variable exists until the program ends.

It is possible to create a global variable in one file and access it from another file. In order to do this, the variable must be declared in both files, but the keyword extern must precede the "second" declaration.

Static Variables

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static.

A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

When a local static variable is created, it should be assigned an initial value. If it's not, the value will default to 0.

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope.

Constant Variables

In C, the preprocessor directive #define was used to create a variable with a constant value. This still works in C++, but problems could arise.

When #define is used, the preprocessor will go through the code and replace every instance of the #defined variable with the appropriate value. Well, since the #defined variable exists only in the file where it is created, it is possible to have the same definition in another file with a completely different value. This could lead to disastrous consequences.

To overcome this problem, the concept of a named constant that is just like a variable was introduced to C++.

To create a constant variable in C++, precede the variable declaration with the keyword const. This tells the compiler that "a variable has been created that has a value that cannot be changed"

Variable is a name assign to a storage area that the program can manipulate. A variable type determines the size and layout of the variable’s memory.

It also determines the range of values which need to be stored inside that memory and nature of operations that can be applied to that variable.

Scope of Variables

The scope of the variable is simply lifetime of a variable. It is block of code under which a variable is applicable or alive. For example:

function foo(){
var x;
}

You declare a variable “x” inside a function “foo.” The scope of that variable remains inside that function it can’t be used outside of that function.
There are three places where variables you can declare variable programming language:

  • Inside a function or a block: Local variables
  • Outside of all functions: Global variables
  • In the definition of function parameters: Formal parameters

Local Variable

Local Variable is defined as a type of variable declared within programming block or subroutines. It can only be used inside the subroutine or code block in which it is declared. The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically.

Example of Local Variable

public int add(){
int a =4;
int b=5;
return a+b;
}

Here, ‘a’ and ‘b’ are local variables

Global Variable

A Global Variable in the program is a variable defined outside the subroutine or function. It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program, unless it is shadowed.

Example:

int a =4;
int b=5;
public int add(){
return a+b;
}

Here, ‘a’ and ‘b’ are global variables.

Local Variable vs Global Variables

Here, are some fundamental differences between Local and Global variables.

ParameterLocalGlobalScopeIt is declared inside a function.It is declared outside the function.ValueIf it is not initialized, a garbage value is storedIf it is not initialized zero is stored as default.LifetimeIt is created when the function starts execution and lost when the functions terminate.It is created before the program’s global execution starts and lost when the program terminates.Data sharingData sharing is not possible as data of the local variable can be accessed by only one function.Data sharing is possible as multiple functions can access the same global variable.ParametersParameters passing is required for local variables to access the value in other functionParameters passing is not necessary for a global variable as it is visible throughout the programModification of variable valueWhen the value of the local variable is modified in one function, the changes are not visible in another function.When the value of the global variable is modified in one function changes are visible in the rest of the program.Accessed byLocal variables can be accessed with the help of statements, inside a function in which they are declared.You can access global variables by any statement in the program.Memory storageIt is stored on the stack unless specified.It is stored on a fixed location decided by the compiler.

Advantages of using Global variables

  • You can access the global variable from all the functions or modules in a program
  • You only require to declare global variable single time outside the modules.
  • It is ideally used for storing “constants” as it helps you keep the consistency.
  • A Global variable is useful when multiple functions are accessing the same data.

Advantages of using Local Variables

  • The use of local variables offer a guarantee that the values of variables will remain intact while the task is running
  • If several tasks change a single variable that is running simultaneously, then the result may be unpredictable. But declaring it as local variable solves this issue as each task will create its own instance of the local variable.
  • You can give local variables the same name in different functions because they are only recognized by the function they are declared in.
  • Local variables are deleted as soon as any function is over and release the memory space which it occupies.

Disadvantages of using Global Variables

  • Too many variables declared as global, then they remain in the memory till program execution is completed. This can cause of Out of Memory issue.
  • Data can be modified by any function. Any statement written in the program can change the value of the global variable. This may give unpredictable results in multi-tasking environments.
  • If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called.

Disadvantages of using Local Variables

  • The debugging process of a local variable is quite tricky.
  • Common data required to pass repeatedly as data sharing is not possible between modules.
  • They have a very limited scope.

What is more useful?

The local and global variable equally important while writing a program in any language. However, a large number of the global variable may occupy a huge memory. An undesirable change to global variables is become tough to identify. Therefore, it is advisable to avoid declaring unwanted global variables.

What is a global constant variable?

Global Constants. A global constant is a literal value to which you assign a name. Like a global variable, you can access the value of the global constant from any script or 4GL procedure in the application. You set the value for the global constant when you declare it.

What is difference between global and static variable?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.

Are constants always global?

Constants. The const declaration should only be used in global scope on globals.

What is the difference between constant and variable explain?

What is the Difference between Constant and Variables? A constant does not change its value over time. A variable, on the other hand, changes its value dependent on the equation. Constants are usually written in numbers.