Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this valid standard c?

I am reviewing some optimisation libraries and came across the function signature

double solvopt(unsigned short n,
           double x[], 
           double fun(), 
           void grad(),
           double options[],
           double func(),
           void gradc()
          )

note that fun() and gard() are passed as function. My question is if this is valid standard C grammar.

Thanks.

like image 992
leon Avatar asked Feb 17 '10 21:02

leon


People also ask

What are the C standards?

C has been standardized by ANSI since 1989 (ANSI C) and by the International Organization for Standardization (ISO). C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system.

Which is a valid compiler of C?

So there are many compilers in C like BDS, Clang, GCC, Interactive C, Lattice, Portable C Compiler, Visual Express, etc.

What is the newest standard of C?

What is C standard? The latest C standard is ISO/IEC 9899:2018, also known as C17 as the final draft was published in 2018. Before C11, there was C99. The C11 final draft is available here.

How do I check C version?

Type “gcc –version” in command prompt to check whether C compiler is installed in your machine. Type “g++ –version” in command prompt to check whether C++ compiler is installed in your machine.


3 Answers

The use of double fun() rather than double (*fun)() is an archaic form, that was only valid in standard C and never in C++, and if I remember correctly, only when declaring a function argument. (much like ary[] which is legal for a function argument, but not for an uninitialized variable)

Since it isn't possible (in C) to pass a function by value to another function, the compiler just took double fun() to mean a pointer to a function that returned a double.

So this is valid (but archaic. has fallen out of favor)

like image 93
John Knoeller Avatar answered Nov 03 '22 02:11

John Knoeller


func() and gradc() are in the proper form for functions with unknown parameters. I'm pretty sure this was an acceptable syntax even for Unix 6 circa 1975.

The [] parameters are the same as *, now and in the past. In fact, I remember a 1980s dispute over which was more proper:

int main (int argc, char **argv, char **envp)

or

int main (int argc, char *argv[], char *envp[])

There is no different in effect. The dispute is which is more correct according to semantics.

like image 30
wallyk Avatar answered Nov 03 '22 02:11

wallyk


Your array parameters (x and options) should probably be pointers.

Referring to a function with parenthesis, like "func()" or "gradc()" calls the function. The name of the function alone is a code pointer that can be dereferenced to call the function in question.

When in doubt, try compiling this with an ANSI C compiler - a lot of compilers provide an ANSI compatibility switch to enforce standards compliance. Also, the K&R book is your friend.

Is this a homework question?

like image 28
3Dave Avatar answered Nov 03 '22 00:11

3Dave