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

Using the Precision Specifier

Using the Precision Specifier

Aligning Output

Aligning Output
As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output.

 

Adding the Minimum Field Width

Adding the Minimum Field Width

Converting to Hex Numbers

Converting to Hex Numbers

Revisiting the printf() Function

Revisiting the printf() Function

The printf() function is the first C library function you used in this book to print out messages on the screen. printf() is a very important function in C, so it's worth it to spend more time on it.