2

In this code:

#include<stdio.h>

int var=100;

int main()
{
    extern int var;        //Declaration not Definition
    printf("%d\n",var);
    return 0;
}

100 is printed which is nothing out of the normal, but when declaration is removed from the main(), even then the global definition is being used. How is that happening? This has been taken from K&R which says:

The (global) variable must also be declared in each function that wants to access it.

sg7
  • 6,108
  • 2
  • 32
  • 40
infinite
  • 319
  • 1
  • 10
  • 2
    What's odd there? – Hatted Rooster Mar 25 '18 at 15:21
  • 2
    Usually you use extern for variables declared as global in different modules. In the same there is no need for the extern decleration. Var is allready known in the module. – NeverToLow Mar 25 '18 at 15:22
  • This isn't PHP where you need `global` btw. – Hatted Rooster Mar 25 '18 at 15:25
  • Which page in which edition of K&R? I think you have misunderstood something. In general, modern C would not encourage `extern` declarations inside a function. If the variable is defined in the source file (as here), the declaration is superfluous. If the variable is defined in another source file, then there should be a header that provides the declaration and that header should be used both in the file that defines the variable and in the files that simply reference it. Similarly with functions — they should be declared in headers or be `static` and defined in the current source file. – Jonathan Leffler Mar 25 '18 at 16:08
  • @JonathanLeffler page 32, 2nd edition – infinite Mar 25 '18 at 16:17
  • 1
    Hmmm; so it does. There are words on p33 which say _"In certain circumstances, the `extern` declaration can be omitted. If the definition of an external variable occurs in the source file before its using in a particular function, then there is no need for an `extern` declaration in the function. The `extern` declarations in … are thus redundant. In fact, common practice is to place definitions of all external variables at the beginning of the source file, and then omit all `extern` declarations."_ That applies to the code shown. I'm quite surprised that the example is shown by K&R. – Jonathan Leffler Mar 25 '18 at 16:23
  • @JonathanLeffler Is it fine to reference that book since it is ANSCI C? – infinite Mar 25 '18 at 16:24
  • 1
    K&R 2nd Edition describes C90 (ANSI C 89, ISO C 90). That is a very old version of C — the current version is C11. It doesn't cover modern features of C added in C99, in particular, or C11. Some of the examples later in the book no longer work on modern versions of Unix; they did work on the versions of Unix from almost 30 years ago. The world changes. K&R was a fine book — I learned C from the 1st Edition. But the world of C has evolved, and it wouldn't be my first choice for people learning C nowadays. _[…continued…]_ – Jonathan Leffler Mar 25 '18 at 16:29
  • 1
    _[…continuation…]_ There are many possible alternatives. I've got [C Programming: A Modern Approach, 2nd Edition](https://smile.amazon.com/dp/0393979504/) which I regard as a decent exposition using C99 (it doesn't cover C11). I have not got many other beginner C books because I learned C long enough ago not to need them any more. (I got King to review it based on other people's suggestions before being willing to recommend it.) – Jonathan Leffler Mar 25 '18 at 16:31

3 Answers3

4

There is no need to include the extern keyword for variables which are declared in the same file or module. The global variable is visible to main since it has global scope (i.e. it is visible to all functions within the file/module).

To clarify, using extern int x; tells the compiler that an object of type int called x exists somewhere. It's not the compilers job to know where it exists, it just needs to know the type and name so it knows how to use it. Once all of the source files have been compiled, the linker will resolve all of the references of x to the one definition that it finds in one of the compiled source files.

Source

Community
  • 1
  • 1
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
2

In:

  #include<stdio.h>

  int var=100;

  int main(){...}

The var has a global file scope. The main does not need extern int var; to know about it. extern int var; would be important for other *.c file to inform it that var is defined somewhere else.

Note:

Use of global variable is usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere.

Global variables also make it difficult to integrate modules because software written by others may use the same global names unless names are reserved by agreement, or by naming convention.

sg7
  • 6,108
  • 2
  • 32
  • 40
1

Another tip is if you want to use extern int var; in another file, you can declare this piece of code in that file.

Joe Zhou
  • 19
  • 2
  • It is dangerous to declare an `extern` variable in a source file — you end up with multiple maintenance problems. The variable should be declared in a header and the header should be used both in the file that defines the variable and in all the files that simply reference it. – Jonathan Leffler Mar 25 '18 at 16:11
  • I agree with you. extern should be in header file and include the header file in the c or c++ file, which I usually do. My mis-expressing. – Joe Zhou Mar 25 '18 at 16:14