No previous prototype?

Possible duplicate:
Error: there is no previous function prototype. Why am I getting this error?

I have a function that I prototyped in the header file, however Xcode still gives me the warning No previous prototype for the function 'printBind' . I have a setBind function prototyped in the same way, but I do not get a 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); } 
+6
source share
1 answer

In C, this is:

 void printBind(); 

not a prototype. It declares a function that returns nothing ( void ) but accepts an undefined list of arguments. (However, this argument list is not variable: all functions that have a variable length argument list must have a complete prototype in scope to avoid undefined behavior.)

 void printBind(void); 

This is a prototype of a 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 (reading "refers to the mid-1980s"). When prototypes were introduced in C (a few years after they were added in C ++), there was a huge legacy of code declaring functions without a list of arguments (since this was not an option before adding prototypes), so backward compatibility considerations meant that SomeType *SomeFunction(); should continue to mean "a function returning SomeType * , but for which we know nothing about the argument list." C ++ eventually added the SomeType *SomeFunction(void); notation SomeType *SomeFunction(void); for compatibility with C, but did not need it, since an early type binding was added earlier, and all functions needed a prototype in the area before they were defined or used.

+20
source

Source: https://habr.com/ru/post/909863/


All Articles