5

I just noticed that this compiles without any errors or warnings using -pedantic -Wall with both gcc and clang.

#include <stdio.h>

int x = 0;

void func(int f(const char *)) {
    f("func()!");
}

int main(void) {
    func(puts);
}

It appears that the parameter f is treated like pointer to function int (*)(const char *) in this case.

But this a behavior that I have never seen or heard anything about. Is this legal C code? And if so then what happens when you have a function as a parameter to a function?

wefwefa3
  • 3,872
  • 2
  • 29
  • 51
  • 1
    Function pointers are valid parameters, you can pass for example different callback functions with them. The function which receives them, then can call that function, no problem there. – Bence Kaulics Oct 21 '16 at 08:20
  • 1
    In this case the type of the parameter is is written as a "function" and not as a "pointer to a function" in which case it would have been declared `int (*f)(const char *)` instead. – wefwefa3 Oct 21 '16 at 08:23
  • This is syntactic sugar and is allowed to make declarations easier to read. See the first comment to the accepted answer of the duplicate question. – Klas Lindbäck Oct 21 '16 at 08:35
  • @KlasLindbäck Obviously not a dup. The questions are not the same, the answers are not the same. A comment happens to explain the problem, but that doesn't make it a dup. – 2501 Oct 21 '16 at 08:39
  • 1
    It was already answered here: [How do you pass a function as a parameter in C?](http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c) – dogus yuksel Oct 21 '16 at 09:10
  • @dogusyuksel, where? I can not see any reference to what OP is asking in the duplicate – David Ranieri Oct 21 '16 at 09:18
  • 1
    This one seems a better duplicated: [C Why function pointer as parameter instead of just a function?](http://stackoverflow.com/q/36578503/1606345) – David Ranieri Oct 21 '16 at 09:36
  • @unwind OP is asking how is this syntax permitted by the standard not how to pass a function. You closed it incorrectly. – 2501 Oct 21 '16 at 09:38
  • 1
    @KeineLust That is the correct duplicate. – 2501 Oct 21 '16 at 09:43

1 Answers1

2

It is allowed by the standard. From C99 standard chapter 6.9.1 (took from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf):

EXAMPLE 2 To pass one function to another, one might say

int f(void);
/*...*/
g(f);

Then the definition of g might read

void g(int (*funcp)(void))
{
    /*...*/
    (*funcp)(); /* or funcp(); ... */
}

or, equivalently,

void g(int func(void))
{
     /*...*/
     func(); /* or (*func)(); ... */
}
Community
  • 1
  • 1
dkolmakov
  • 637
  • 5
  • 12