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

Manipulating Bits

Manipulating Bits

In previous hours, you learned that computer data and files are made of bits (or bytes). There is even an operator in C_the sizeof operator_that can be used to measure the number of bytes for data types.

Everything Is Logical

Everything Is Logical

Now, it's time for you to learn about a new set of operators: logical operators.

There are three logical operators in the C language:
&&     The logical AND operator
||     The logical OR operator

Question and Answer

    Q Why do we need the sizeof operator?

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.

Playing with an Infinite Loop

Playing with an Infinite Loop

If you have a for statement like this,

for ( ; ; ){
  /* statement block */
}