Thread: Static function static or not?

  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.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    744
    I believe we answered this issue in your previous post.

    May I suggest studying the two responses in the previous post, and purchasing a good book on C Programming and studying the difference between static and extern functions.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Read the previous answers and read K&R start to finish. I understand the difference between static and extern but not in this case

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    744
    For the record, I used to teach C, professionally so I do not need to go back and reread K&R which has been succeeded by the C89/90, C99, and C11 C Programming Language Standards!

    Static functions, and extern functions have different uses, and using them together in a multiple module program, is undefined behavior. In other words, don't do it as we tried to explain in your previous post.

    It is possible that different compilers would interpret this differently.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    @rstanley, he probably meant "I read ..." as opposed to "You should read...".

    @OP,

    "static" doesn't say to not look in other files (you mean object files) for the definition. It says to not enter the function name as an external symbol in the generated object code for the current translation unit.

    When you compile main.c you get a warning that foo hasn't been defined (as it should be, since it's static and is therefore expected to be found in the current translation unit). If it had been defined, its name would not have been entered as an external symbol in the object file.

    However, when you combine main.o and foo.o, the function is found (externally defined) in foo.o, so the program can be linked.

    Still, you shouldn't ignore warnings and it is presumably UB. And you should include stdio.h for printf.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    744
    @Vespasian_2, if I did misread your last post, you have my apologies.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Yeah it does indeed look like undefined behavior. I'm not saying I want to design programms like this - instead I'm trying to see what happens under the hood for better understanding.

    @standley - I was trying to say "I read" - no stress my typo

    Second last question then. Can anyone confirm the accuracy of these statements?
    When static is appended to a function declaration it means - don't enter the function name as an external symbol in the generated object code for the current translation unit.
    When static is appended to a function definition it means - this symbol in the generated object code is not made available outside of this object code.

    Last question: I ADDED a function declaration in the foo.c file

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

    Code:
    extern int foo(int a);
    
    extern int foo(int a) {    return a * 2;
    }
    That is redundant correct? theres no reason to add it in because the declaration is already in main.c?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    > instead I'm trying to see what happens under the hood for better understanding.
    You gain understanding by reading the standards, not by trying to reverse engineer what a particular compiler does in particular edge cases.

    Because whatever you think you might learn from the 'foo' compiler you're looking at, the 'bar' compiler could do something else, which is still compatible with the standard.

    It's just another variation on what does y = x++ * x++ do?
    Newbies get all wound up about this for some reason and concoct all sorts of theories - all of which ultimately prove to be false.

    rstanley nailed it here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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