C প্রোগ্রামিং এর Scope এবং Storage Classes এর বর্ণনা

Submitted by administrator on Mon, 01/02/2012 - 16:04

CLASS 14 - Scope and Storage Classes in C

 

In the previous hours, you've learned how to declare variables of different data types, as well as to initialize and use those variables. It's been assumed that you can access variables from anywhere. Now, the question is: Can we declare variables that are accessible only to certain portions of a program? In this lesson you'll learn about the scope and storage classes of data in C. The main topics covered in this lesson are

 

    Block scope
    Function scope
    File scope
    Program scope
    The auto specifier
    The static specifier
    The register specifier
    The extern specifier
    The const modifier
    The volatile modifier

 


Summary

  •     A variable declared within a block has block scope. Such a variable is also called a local variable and is only visible within the block
  •     The goto label has function scope, which means that it is visible through the whole block of the function within which the label is placed. No two goto labels share the same name within a function block.
  •  
  •     A variable declared with the static specifier outside a function has file scope, which means that it is visible throughout the entire source file in which the variable is declared.
  •     A variable declared outside a function is said to have program scope. Such a variable is also called a global variable. A global variable is visible in all source files that make up an executable program.
  •     A variable with block scope has the most limited visibility. On the other hand, a variable with program block is the most visible through all files, functions, and other blocks that make up the program.
  •     The storage class of a variable refers to the combination of its spatial and temporal regions (that is, its scope and duration.)
  •     By default, a variable with block scope has an auto duration, and its memory storage is temporary.
  •     A variable declared with the static specifier has permanent memory storage, even though the function in which the variable is declared has been called and the function scope has exited.
  •     A variable declared with the register specifier may be stored in a register to speed up the performance of a program; however, the compiler can ignore the specifier if there is no register available or if some other restrictions have to apply.
  •     You can also allude to a global variable defined elsewhere by using the extern specifier from the current source file.
  •     To make sure the value saved by a variable cannot be changed, you can declare the variable with the const modifier.
  •     If you want to let the compiler know that the value of a variable can be changed without an explicit assignment statement, declare the variable with the volatile modifier so that the compiler will turn off optimizations on expressions involving the variable.


 

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?