Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>A global variable has <em>external linkage</em> by default. Its scope can be extended to files other than containing it by giving a matching <em>extern</em> declaration in the other file.</li> <li>The scope of a global variable can be restricted to the file containing its declaration by prefixing the declaration with the keyword <em>static</em>. Such variables are said to have <em>internal linkage</em>.</li> </ul> <p>Consider following example:</p> <h2>1.cpp</h2> <pre><code>void f(int i); extern const int max = 10; int n = 0; int main() { int a; //... f(a); //... f(a); //... } </code></pre> <ol> <li>The signature of function f declares f as a function with <em>external linkage</em>(default). Its definition must be provided later in this file or in other translation unit (given below).</li> <li>max is defined as an integer constant. The default linkage for constants is <em>internal</em>. Its linkage is changed to external with the keyword <em>extern</em>. So now max can be accessed in other files. </li> <li>n is defined as an integer variable. The default linkage for variables defined outside function bodies is <em>external</em>.</li> </ol> <h2>2.cpp</h2> <pre><code>#include &lt;iostream&gt; using namespace std; extern const int max; extern int n; static float z = 0.0; void f(int i) { static int nCall = 0; int a; //... nCall++; n++; //... a = max * z; //... cout &lt;&lt; "f() called " &lt;&lt; nCall &lt;&lt; " times." &lt;&lt; endl; } </code></pre> <ol> <li>max is declared to have <em>external linkage</em>. A matching definition for max(with external linkage) must appear in some file. (As in 1.cpp)</li> <li>n is declared to have <em>external linkage</em>.</li> <li>z is <em>defined</em> as a global variable with <em>internal linkage</em>.</li> <li>The definition of nCall specifies nCall to be a variable that retains its value across calls to function f(). Unlike local variables with the default auto storage class, nCall will be initialized only once at the start of the program and not once for each invocation of f(). The storage class specifier <em>static</em> affects the lifetime of the local variable and not its scope.</li> </ol> <p><strong>NB:</strong> The keyword <em>static</em> plays a double role. When used in the definitions of global variables, it specifies <em>internal linkage</em>. When used in the definitions of the local variables, it specifies that the lifetime of the variable is going to be the duration of the program instead of being the duration of the function.</p> <p>Hope that helps!</p>
    singulars
    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. 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.
    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