What does the C standard say about...

Discussion in 'C Programming' started by Kenny McCormack, May 1, 2012.

  1. Suppose I include a declaration of a function, like:

    int foo(void);

    in my code, but do neither of the following:

    1) Call foo()
    2) link in a library that supplies foo()

    My testing indicates that this is fine, but I'm curious what the standards
    jockeys have to say.

    BTW, this problem arises when you must include an include file that defines
    some stuff you need, but also contains declarations for functions you don't
    have (i.e., don't have the libs for).

    But the Bush apologists hope that you won't remember all that. And they
    also have a theory, which I've been hearing more and more - namely,
    that President Obama, though not yet in office or even elected, caused the
    2008 slump. You see, people were worried in advance about his future
    policies, and that's what caused the economy to tank. Seriously.

    (Paul Krugman - Addicted to Bush)
    Kenny McCormack, May 1, 2012
    1. Advertisements

  2. Kenny McCormack

    Kaz Kylheku Guest

    If you do not call foo or take its address, then there is no reference
    to foo.

    If an identifier declared with external linkage is used in an expression
    (other than as part of the operand of a sizeof operator whose result is an
    integer constant), somewhere in the entire program there shall be exactly one
    external definition for the identifier; otherwise, there shall be no more
    than one. (C99 6.9)

    No more than one means zero or one.

    A similar text is given for internal linkage names, in the Constraints
    section. This means that for internal linkage, a diagnostic is required
    when these rules are violated, but for external names, a diagnostic is
    not required (because the errors can arise after separate compilation,
    at the linking stage, and there are linkage models which are loose, allowing
    multiple definitions.)
    Kaz Kylheku, May 2, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.