Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No previous prototype? [duplicate]

Tags:

c

function

xcode

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);
}
like image 553
Stas Jaro Avatar asked Mar 02 '12 23:03

Stas Jaro


1 Answers

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.

like image 110
Jonathan Leffler Avatar answered Oct 20 '22 20:10

Jonathan Leffler