No previous prototype?

16,917

In C, this:

void printBind();

is not a prototype. It declares a function that returns nothing (void) but takes an indeterminate list of arguments. (However, that list of arguments is not variable; all functions taking a variable length argument list must have a full prototype in scope to avoid undefined behaviour.)

void printBind(void);

That's a prototype for the function that takes no arguments.

The rules in C++ are different - the first declares a function with no arguments and is equivalent to the second.

The reason for the difference is historical (read 'dates back to the mid-1980s'). When prototypes were introduced into C (some years after they were added to C++), there was an enormous legacy of code that declared functions with no argument list (because that wasn't an option before prototypes were added), so backwards compatibility considerations meant that SomeType *SomeFunction(); had to continue meaning 'a function that returns a SomeType * but for which we know nothing about the argument list'. C++ eventually added the SomeType *SomeFunction(void); notation for compatibility with C, but didn't need it since type-safe linkage was added early and all functions needed a prototype in scope before they were defined or used.

Share:
16,917
Stas Jaro
Author by

Stas Jaro

C++, C, Objective-C, VHDL, Java, AVR Assembly, HTML, Javascript, PHP

Updated on August 11, 2022

Comments

  • Stas Jaro
    Stas Jaro almost 2 years

    Possible Duplicate:
    Error: No previous prototype for function. Why am I getting this error?

    I have a function that I prototyped in the header file, however Xcode still gives me warning No previous prototype for the function 'printBind'. I have the function setBind prototyped in the same way but I do not get an warning for this function in my implementation.

    CelGL.h

    #ifndef Under_Siege_CelGL_h
    #define Under_Siege_CelGL_h
    
    void setBind(int input);
    void printBind();
    
    #endif
    

    CelGL.c

    #include <stdio.h>
    #include "CelGL.h"
    
    int bind;
    
    void setBind(int bindin) { // No warning here?
        bind = bindin;
    }
    
    void printBind() { // Warning here
        printf("%i", bind);
    }
    
  • sherrellbc
    sherrellbc over 6 years
    Though I now understand the reason for the error, I do not really understand the usefulness of this feature. When would you define a function with an indeterminate parameter list and what exactly does this have to do with C++? Is this a useful feature in C++?
  • Jonathan Leffler
    Jonathan Leffler over 6 years
    You can’t define a prototype with an indeterminate argument list in C++. In C++, that means “no arguments”, not “indeterminate arguments”. The feature is not so much useful as a necessary concession to reality. When C was standardized, the entire vast code base was written without prototypes; they hadn’t existed in C. The empty parenthesis notation was all there was in pre-standard C. To have outlawed it would have killed the nascent C standard. That was still true for C99. It was less clear for C11, but was left unchanged.