C Static Variables

A.Local Variables

  • Variables declared within a function or a block of code are referred to as local variables. These variables are only accessible within the function in which they were declared. Local variables have a function-specific scope since they can only be accessed by statements that are included within that function or code block.

B. Worldwide Variables

  • Global variables are those that have definitions separate from any and all functions. We can access global variables within any of the program’s declared functions to retrieve their values. The system initialises global variables when they are defined. When a name is defined for both local and global variables, the local variable is given preference.

C. Static Variables

  • One definition of a static variable is one that keeps its value even after the programme leaves the scope in which it was declared. Static variables don’t need to be initialised afresh in the new scope; they keep their value. Static variables retain their memory until the program’s end, but regular variables are deleted when the function they were declared in is exited. Both inside and outside of the function they can be declared. The block is the local area for static variables. A static variable’s default value is zero. A static variable is declared with the word static.

Output:

static datatype variable_name = variable_value;

Static global variables differ from static local variables.

Static global variable

  • Static global variables are any variables with the static keyword specified outside of a function. Any method in the application will allow access to this variable.

Static local variable

  • Static local variables are any variables that are declared with the static keyword inside of a function. A static local variable has the same scope as a local variable, but its memory is accessible for the duration of the program’s execution.

The characteristics of static variables

  • A static variable’s value will remain after the programme leaves the scope in which it was declared.
  • A static variable’s allotted memory is accessible for the duration of the program’s execution.
  • A static variable’s default value is 0 if we don’t initialise it.
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in