Alternative syntax for C functions taking another function as a parameter

I recently found out that it is legal C:

#include <stdio.h> int foo(int bar(int)) { return bar(42); } int bar(int x) { return x * 42; } int main() { printf("Baz = %d\n", foo(bar)); return 0; } 

Compiles without warnings and works as expected (compiles and works fine even in a C program, not in C ++)

 rep ~/Documents $ g++ -Wall test.cpp rep ~/Documents $ ./a.out Baz = 1764 rep ~/Documents $ 

Why is this syntax not used more often or not even mentioned anywhere?

+5
source share
1 answer

Perhaps because you can call a function inside another function. This is better than a function as a parameter. Although it is used by frameworks to perform callback functions. I prefer a function from another function as a regular developer.

-6
source

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


All Articles