Scope of Variables

Scope: The scope of a variable refers to the extent to which different parts of a program have access to the variable, in other words, where the variable is visible.
Lifetime: Lifetime means how long the variable persists in memory, or when the variable’s storage is allocated and deallocated. Scope also affects a variable’s lifetime
Global Variables

  • An global variable is a variable defined outside of any function. This means outside of main() as well, because main() is a function, too.
  • The scope of an global variable is the entire program.

Local Variables

    • A local variable is one that is defined within a function.
    • The scope of a local variable is limited to the function in which it is defined.
    • Method parameters are local variables in that method.

Initializing Local and Global Variables

  • When a local variable is defined, it is not initialized by the system, you must initialize it yourself.
  • Global variables are initialized automatically by the system when you define them as follows:
    Data Type Default Value
    int 0
    char ‘\0’
    float 0
    double 0
    pointer NULL

    Note: It is a good programming practice to initialize variables properly otherwise, your program may produce unexpected results because uninitialized variables will take some garbage value already available at its memory location.

    Storage Classes of Variable:

  • Automatic variables are declared within the function in which they are utilized. They are created when the function is called and destroyed automatically when the function is exited. A variable declared inside the function is by default automatic even if a storage class is not specified.
  • The keyword auto can be used to declare automatic variable explicitly. The memory area in which these variables are allocated memory is called as stack memory.
  • Static: The scope of the static variable is local to the function in which it is declared. The lifetime is till the lifetime of a program. The value of a static variable is retained between function calls. The keyword static is used to declare a static variable.
  • Extern variables are declared outside any function. The scope is global i.e. they can be accessed by any function. The lifetime is till the lifetime of the program.
  • Register:
    1. The register keyword is used to suggest to the compiler that an automatic local variable is stored in a processor register rather than in regular memory.
    2. The central processing unit (CPU) of your computer contains a few data storage locations called registers. It is in the CPU registers that actual data operations, such as addition and division, take place. To manipulate data, the CPU must move the data from memory to its registers, perform the manipulations, and then move the data back to memory. Moving data to and from memory takes a finite amount of time. If a particular variable could be kept in a register, to begin with, manipulations of the variable would proceed much faster
  • The extern keyword:

    main()
    {
    extern int x;
    ———-
    }
    fun1()
    {
    extern int x;
    }
    int x;In case x is declared after the function. Though it is global it can’t be used in these functions.
    Using extern declaration as shown, variable x can be used in any of the function

    Call by value:
    When a function is called the value of the actual argument is copied into corresponding formal arguments of the called function. So any changes made to the formal argument in called function are not reflected in calling the function.
    Call by reference:
    When a function is called the address of actual argument is copied into the formal argument. So any changes made to the formal arguments in called function are reflected in calling function.

 

null

Mr. Sandeep Soni

Founder, Trainer & CEO, Deccansoft Software Services.

Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.