Thread: Mixing Extern and Static Function Declaration and Function Definition

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Mixing Extern and Static Function Declaration and Function Definition

    What is the result of mixing an extern function declaration and static function definition?

    Firstly, by way of example, let's see the file called foo.c below:
    Code:
    extern int foo(int);
    
    extern int foo(int a){
    return a;
    }
    We have declared and consequently defined an extern function called foo. What this means is that the function can be called from other .c files (Yes, best practice says the declaration should go in a header but for simplicity lets assume no header)

    Secondly, by way of example, let's see the file called bar.c below:
    Code:
    static int bar(int);
    
    static int bar(int a){
    return a;
    }
    We have declared and consequently defined a static function called bar. What this means is that the function cannot be called from other .c files (Yes, best practice says the declaration should go in a header but for simplicity lets assume no header)

    Thirdly, by way of example, let's see the file called mixedFoo.c below:
    Code:
    extern int mixedFoo(int);
    
    static int mixedFoo(int a){
    return a;
    }
    The translation unit wont compile and GNU GCC gives an error that "static declaration of "mixedFoo" follows non-static declaration.

    Finally, by way of example, let's see the file called mixedFoo2.c below:
    Code:
    static int mixedFoo2(int);
    
    extern int mixedFoo2(int a){
    return a;
    }
    This does not cause an error as case 3 above? Also, mixedFoo cannot be called from main.c. What exactly is happening under the hood in case 3 and 4?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    An old Henny Youngman joke comes to mind, but...

    When it come to function declaration and definitions, extern and static are mutually exclusive!

    Using "extern" for function declarations/prototypes in a header file are not needed. As long as the function declaration/prototype in the header file, matches the function definition in one .c file, and the call to the same function in a second .c file, all will be resolved correctly by the linker at link time, NOT at compile time.

    Declaring a function as static, defining the function as static, and calling the function in the same single .c file, and ONLY a single .c file, all will be resolved by the compiler, NOT the linker, and the symbol will never be seen by the linker as there is nothing for the linker to do.

    Bottom line, don't mix extern and static for any declaration/definition of functions or any data!
    Last edited by rstanley; 04-25-2017 at 09:32 AM.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by rstanley View Post
    An old Henny Youngman joke comes to mind, but...
    "Take my function ... please!"
    I don't get it.

    Perhaps:
    "Doctor, it hurts when I mix extern and static!"
    "Don't mix extern and static."


    Anyway, he seems to know you shouldn't mix them and is asking why gcc doesn't give an error in the last case. I suppose the answer has something to do with extern being the default. I.e., if you don't say extern of static then it's the same as if you said extern. So the prototype having "static" says the function will be static and the actual definition becomes static whether or not it says "extern". But if the prototype is extern (either with "extern" or with nothing, those cases being identical) then saying "static" for the definition is a contradiction.
    Code:
    int foo1(int);    // says it will be external (the "extern" keyword is implied)
    
    static int foo1(int a){  // now we're saying it will be static
        return a;
    }
    
    
    static int foo2(int);    // says it will be static
    
    int foo2(int a){  // okay, it's static
        return a;
    }

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by algorism View Post
    Perhaps:
    "Doctor, it hurts when I mix extern and static!"
    "Don't mix extern and static."
    That was the one! ;^)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 4
    Last Post: 01-04-2013, 03:10 AM
  3. Replies: 1
    Last Post: 10-12-2011, 04:46 PM
  4. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM
  5. Templated function (declaration and definition)
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2003, 08:07 PM

Tags for this Thread