Thread: Static function static or not?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Static function static or not?

    main.c:

    Code:
    static int foo(int);
    
    
    int main ()
    {
        int result = foo(2);
        printf("%i", result);
        return 0;
    }


    foo.c:
    Code:
    extern int foo(int a) {
        return a * 2;
    }

    This outputs 4.

    What exactly is happening under the hood here?

    I do know the following is happening:

    1) Before we call a function, in this case, foo, we must declare it which is done so at the top of main.c
    2) The declaration at the top says static which means: Mr Compiler, dont go looking in other files for foo's definition, look right here in main.c
    3) Yet, the definition of foo itself is extern which means its available to anyone who calls it

    This is a contradiction and I think its actually undefined behaviour which by chance returns 4.

    Thoughts?
    Last edited by Vespasian_2; 04-25-2017 at 02:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2011, 11:22 PM
  2. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  3. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  4. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  5. Replies: 2
    Last Post: 10-02-2004, 10:12 AM

Tags for this Thread