Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding extern storage specifier in C

Tags:

c

I was going through Dennis Ritchie's book, The C programming language. In it, he says:

In certain circumstances, the extern declaration can be omitted. If the definition of the external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.

I tried quick code as follows:

#include "stdio.h"

int n = 0;
int nn = 111;

int main(int argc, char **argv) {
    int n = 1;
    printf("n from main(): %d\n", n);   
    function1();         
    function2();         
}

void function1()
{
    extern int n;
    int nn;  //declaring without extern keyword, this should point to global nn as per Dennis, but that does not seem to happen
    int nnn; //declaring without extern keyword
    printf("n from function1(): %d\n", n);
    printf("nn from function1(): %d\n", nn);
    printf("nnn from function1(): %d\n", nnn);
    n = 10;
}

void function2()
{
    extern int n;
    int nn;  //declaring without extern keyword, this should point to global nn as per Dennis, but that does not seem to happen
    int nnn; //declaring without extern keyword
    printf("n from function2(): %d\n", n);
    printf("nn from function2(): %d\n", nn);
    printf("nnn from function2(): %d\n", nnn);
}

int nnn = 222 ;

Below is sample output:

n from main(): 1
n from function1(): 0
nn from function1(): 1955388784
nnn from function1(): 6422476
n from function2(): 10
nn from function2(): 1955388784
nnn from function2(): 6422476

Notice what both functions function1() and function2() printed above. I guess, as per Dennis' statement, both should have referred global nn and should have printed 111. But that did not happen.

(You can try running code here)

Is it because the version about which Dennis is talking differs from the one using? Am on MinGW.org GCC-8.2.0-5.

like image 487
anir Avatar asked Oct 29 '25 08:10

anir


2 Answers

, then there is no need for an extern declaration in the function.

Here what Dennis Ritchie saying is that, If definition of nn varibale is already occured in source file then now in function1() and funcation2() you do not need to declare variable as extern again, like (extern int nn;) you can directly use them.

But by doing int nn; in your funcation1() you are defining one another local variable. Which is complete different variable.

like image 95
Jeegar Patel Avatar answered Oct 31 '25 00:10

Jeegar Patel


The keyword extern is used with objects to reference objects that have file scope.

This declaration in your functions

int nn;  //declaring without extern keyword, this should point to global nn as per Dennis, but that does not seem to happen

has a block scope. So it is a definition of a local variable.. It does not have linkage.

The quote you provided means that if there is a definition of a file scope variable then it has external or internal linkage. So there is no need to use the keyword extern

For example

//…

int n = 10;  // definition of a variable with external linkage

void f()
{
    printf( "n = %d\n", n );
}

//...
like image 40
Vlad from Moscow Avatar answered Oct 30 '25 23:10

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!