Skip to main content

Describe the scopes, visibility and lifetime of variables


Answer:
Scope:
The scope of an identifier is a part of the program in which the identifier can be used to access its object.


There are different categories of scope:
(i) File scope
(ii) Function scope
(iii) Block scope
(iv) Function-prototype scope
These categories depend on how and where identifiers are declared.

(i) File scope: File scope identifiers, also known as globals, are declared outside of all blocks; their scope is from the point of declaration to the end of the source file.

(ii) Function scope: The only identifiers having function scope are statement labels. Label names can be used with goto statements anywhere in the function in which the label is declared. Labels are declared implicitly by writing label_name: followed by a statement. Label names must be unique within a function.

(iii) Block scope: The scope of an identifier with block (or local) scope starts at the declaration point and ends at the end of the block containing the declaration (such block is known as the enclosing block). Parameter declarations with a function definition also have block scope, limited to the scope of the function body.

(iv) Function-prototype scope: Identifiers declared within the list of parameter declarations in a function prototype (not as a part of a function definition) have a function prototype scope. This scope ends at the end of the function prototype.

Visibility:
The visibility of an identifier is a region of the program source code from which an identifier’s associated object can be legally accessed.
Scope and visibility usually coincide, though there are circumstances under which an object becomes temporarily hidden by the appearance of a duplicate identifier: the object still exists but the original identifier cannot be used to access it until the scope of the duplicate identifier ends.
Technically, visibility cannot exceed a scope, but a scope can exceed visibility.

Lifetime:

The lifetime of an object is the time in which memory is reserved while the program is executing. There are three object lifetimes:
(i)static Objects
(ii)automatic Objects
(iii)dynamic Objects

(i)Static Objects:

The memory for static objects is allocated at compile/link time. Their address is fixed by the linker based on the linker control file (LCF).  You may know this file by another name such as linker-script file, linker configuration file or even scatter-loading description file. The LCF file defines the physical memory layout (Flash/SRAM) and placement of the different program regions.
(ii)Dynamic Objects:
Strictly speaking (according to the C standard) dynamically allocated objects are also called automatics. However, it is important to differentiate between this type of object and automatics for two reasons:
Ø  The memory is allocated from a different memory area (the heap not the stack).
Ø  The lifetime is under the control of the programmer rather than the C run-time system.

(iii)Automatic objects:


The majority of variables are defined within functions and classed as automatic variables. This also includes parameters and any temporary-returned-object (TRO) from a non-void function, e.g.

Popular posts from this blog

Draw the basic organization of computer

Answer:                 The basic organization of computer__ Input Unit: ·          It accepts (or reads) instructions and data from outside. ·          It converts these instructions and data in computer acceptable form ·          It supplies the converted instructions and data to the computer system for further processing. Central Processing Unit (CPU): Control Unit: Control unit of a computer system manages and coordinates the operations of all other components of the computer system. Arithmetic Logic Unit(ALU): Arithmetic logic unit of a computer system is the place, where the actual executions of instruction, takes place during processing operation. Storage Unit: Primary Memory: It is volatile ( loses data on power ...

Describe the four basic data types. How could we extend the range of values they represent?

Answer: The basic four data types are: Data Type Integer Type Character Type Floating Point Type Void Type signed int short int long int unsigned unsigned int unsigned short int unsigned long int char signed char unsigned char float double long double