Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Scope is defined as <code>the area in which the object is active</code>. There are five scopes in C. They are as follows.</p> <p><strong>Program Scope</strong></p> <p>These are the declarations at the top most layers. They are available up to the life of a program. All the functions have this scope. This is otherwise known as global scope.</p> <p><strong>File Scope</strong></p> <p>It has scope such that it may be accessed from that point to the end of the file. </p> <pre><code>void dummy(void) { } // absence of static automatically gives program scope to `dummy()` static void dummy(void) { } // static keyword here gives function `dummy()` a file scope </code></pre> <p><strong>Function Scope</strong></p> <p>Only labels have this scope. In this scope, they are active up to end of the function. </p> <pre><code>void printFun() { print: printf(“i is less than j”); } int main() { int i=1,j=2; if(i &lt; j) goto print; } </code></pre> <p>This code will be flagged error by the compiler saying that the label print is not known because labels have only function scope. If you have to jump unconditionally between the functions, you have to use <code>setjmp</code>/<code>longjmp</code> functions. </p> <p><strong>Block Scope</strong></p> <p>Declarations that are active up to the end of the block (where a block is defined as statements within <code>{ }</code>). All the declarations inside the function have only block scope. </p> <pre><code>int fun(int a, int b) { int c; { int d; } // a, b, c, d all have block scope } </code></pre> <p>As I have said, function scope applies only to labels. So should not be confused with block scope. The function arguments are treated as if they were declared at the beginning of the block with other variables (remember that the function body is also considered as a block within <code>{ }</code>). So the function arguments have block scope (not function scope).</p> <p>Local scope is general usage to refer the scope that is either function or block scope. </p> <p><strong>Prototype Scope</strong></p> <p>They are having the scope only inside the prototype declaration. This scope is interesting because the variable names are valid only in the declaration of prototype and does not conflict with other variable names. It exists for very little time and of less use and so goes unnoticed. </p> <pre><code>int add(int a, float b); </code></pre> <p>Here the variables <code>a</code> and <code>b</code> are said to have prototype scope.</p> <p><strong>Selecting Minimal Scope</strong></p> <p>When a name has to be resolved, that name is searched in the minimal scope, and if that is not available, it is searched at higher levels of scope. So, if a variable has to be declared, you have to select a minimal scope possible. If you can limit your scope, that increases the efficiency, readability and maintainability of your program. If you want a variable which is not useful outside a block, declare it inside the block and not in the outer ones. Similarly, if you want a variable whose value will be accessed only within the function but has to retain the value between the function calls, opt for static variable to a global one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload