Thread: extern variable throwing : "has both ‘extern’ and initializer" error

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    extern variable throwing : "has both ‘extern’ and initializer" error

    The code is throwing error despite 'i' being defined after initialization, why?

    Code:
    #include<iostream>
    
    
    using namespace std;
    
    
    int main()
    {
            extern int i=898;
            cout<<i<<endl;
            return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    extern means that the variable defined elsewhere, possibly further down in the same file, but usually in another source file (and therefore object file). Since extern used on a variable does not define the variable, you cannot initialize it there. Here is one possibility that will compile.
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        // tell compiler about existence of i that will be defined elsewhere
        extern int i;
    
        cout << i << '\n';
    
        return 0;
    }
    
    // define the variable (could be in another source file)
    int i = 898;

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    Since extern used on a variable does not define the variable, you cannot initialize it there.
    Initializing an extern variable defines that - the question is why definition - extern i =898; is different from definition - int i =898;

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by gaurav# View Post
    Initializing an extern variable defines that - the question is why definition - extern i =898; is different from definition - int i =898;
    I don't know what to tell you.
    If you have a reference for your assertion, give it.
    But obviously it's not the case here, hence the error.
    Perhaps it has something to do with it being a local variable.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    Check this out:

    Code:
    #include <iostream>
    using namespace std;
    
    extern int k =89;
    
    int main() {
    cout<<k;
    	return 0;
    }
    This works fine

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    That throws a warning for me:

    Code:
    $ g++ -Wall -pedantic -O0 -g -std=c++14 main.cpp 
    main.cpp:3:12: warning: ‘k’ initialized and declared ‘extern’
     extern int k = 898;
                ^
    Source:

    Code:
    #include <iostream>
    
    extern int k = 898;
    
    int main (int argc, char* argv[])
    {
        std::cout << k << std::endl;
    
        return 0;
    }
    Since it throws a warning, i would not count on it working the way you think it works.

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    This warning can only hide a linker error of multiple definition which doesn't stand in this case. focus on this: how these definitions: int K=898 & extern int K=898 are different at function scope.

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    A rule that causes that compiler error

    In 2.2.2. Variable Declarations and Definitions of c++ Primer 5th ed Lippman et.al. I came across this rule:
    It is an error to provide an initializer on an extern inside a function.
    i.e. trying to define an external variable inside of a function is not allowed, which makes sense. But you can declare it there so that it can be used there.

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    The reason behind it is not given hence the question is asked.

  10. #10
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Yes, I agree it would be more satisfying had they quoted the standard or gave a reason for that rule.

    Here is some reasoning:
    Those 'extern' variables are initialized before any function runs.
    see Non-local variables at:
    Initialization - cppreference.com

    If it was declared 'static' rather than 'extern' within a function block, it would still have static storage duration, but its' linkage would be local to that function vs. external. So would get initialized when the execution first runs through that line within the function:
    Storage class specifiers - cppreference.com
    Last edited by dslowik; 05-07-2017 at 01:23 PM. Reason: Added spacing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2017, 11:34 AM
  2. Replies: 3
    Last Post: 05-01-2010, 02:26 AM
  3. Why I can not "extern" a variable from header file?
    By meili100 in forum C++ Programming
    Replies: 22
    Last Post: 06-23-2008, 03:58 AM
  4. error C2059 with extern "C"
    By Elysia in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2008, 06:16 PM
  5. Replies: 17
    Last Post: 12-15-2006, 11:02 AM

Tags for this Thread