Thread: Declaration and definition of an external variable.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Declaration and definition of an external variable.

    Hello to all

    Why I take warning on this code :

    Code:
    #include<stdio.h>
    extern int v  = 0;  // Declaration and definition of an external
    
    int main(void)
    {
        v=5;
    
        printf("%d" , v);
    
        return 0;
    }
    Is there any error? Why I take a warning: 'v' initialized and declared 'extern'|
    ||=== Build finished: 0 errors, 1 warnings ===| ???


    and what is the meaning of :

    An extern declaration that initializes a variable serves as a definition of the variable. This rule prevents multiple extern declarations from initializing a variable in different ways. ??? any example???? of this??? in order to understand it better.

    Thank you in advance

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You don't need the keyword "extern" to declare an object with external linkage. Usually you don't use the word extern in the object definition, only in the declarations (usually in other files) that you want to link to the original object. So your program could be written like this:
    Code:
    int v = 1;
    
    void func(); // declaration of function in other file
    
    int main() {
        v = 5;
        func();
        return 0;
    }
    
    // in another file
    #include <stdio.h>
    
    extern int v;   // this connects v to the v in the other file
    
    void func() {
        printf("%d\n" , v);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Mr.Lnx View Post
    An extern declaration that initializes a variable serves as a definition of the variable.
    This bit is straightforward. Initializing v to 0 makes it a definition, so you can't define any other global variables named 'v' in your program. 'extern' doesn't actually make any difference in this case.

    Code:
    extern int v;  // no initialisation, no definition
    Here 'extern' means "don't define v, I'll define it somewhere else". If you have only 'extern v' declarations in your programs, you can't use 'v', you'll get an indefined symbol error.

    Code:
    int v;
    This is an annoying one (I can't remember the term for it) -- but it's a declaration in that you can have as many as you like so long as they're all the same type. However unlike extern declarations, having these is sufficient for one of them to become the definition at compiler/link time.
    If you have a single
    Code:
    int v = 0;
    definition anywhere in your program, that will take precedence over the others.

    This rule prevents multiple extern declarations from initializing a variable in different ways. ??? [/I] any example???? of this??? in order to understand it better.
    I don't know what this means. You can only ever initialise the variable once. There's nothing wrong with what you've done -- but it is unusual to see 'extern' on a definition.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can think of "extern" as the variables counterpart to function prototypes. You place all function prototypes in .h files, and you define the actual functions in the .c files, right? You can do something similar with the variables

    foo.h file
    Code:
    extern const char PROGNAME[100];
    foo.c file
    Code:
    #include "foo.h"
    
    const char PROGNAME[100] = "Calculator";
    Any file which now #include's that .h file will then have access to the symbol PROGNAME, which in this example is a 100 byte character buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaration and definition of a variable
    By raghu_1991 in forum C Programming
    Replies: 2
    Last Post: 05-11-2013, 02:21 AM
  2. Variable declaration Vs definition
    By callkalpa in forum C Programming
    Replies: 11
    Last Post: 12-17-2009, 12:21 PM
  3. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  4. definition/declaration/...
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2007, 02:38 PM
  5. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM