Variables between parentheses and curly braces

I downloaded a piece of C code and it uses this style of function definition:

int somefunction(name1, name2) 
int name1;
int name2;
{
    // do stuff, using name1 and name2
}

      

Why or when will you use this syntax, and what's the difference with the better known syntax, where the parameter types are inside parentheses?

+3


source to share


1 answer


This is a very old, pre-standard C syntax for defining functions, also known as K&R notation after Kernighan and Ritchie, who wrote the book The C Programming Language. This was allowed by C89 / C90 out of necessity - the standard added a new prototype form of the function declaration, but the existing code did not use it (since in 1989 many compilers did not support and very little code was written to use it), you only have to find the notation in the code which has either not been changed in more than twenty years, or still has claims to support the pre-standard compiler (such claims are dubious at best).

Don't use it! If you see this, please update it (carefully).

Note that you could also write any of:



somefunction(name1, name2) 
{
    // do stuff, using name1 and name2
}

int somefunction(name1, name2) 
int name2;
int name1;
{
    // do stuff, using name1 and name2
}

int somefunction(name1, name2) 
int name1;
{
    // do stuff, using name1 and name2
}

      

and undoubtedly other options. If return type was omitted, it was assumed int

. If parameter type was omitted, it was assumed int

. The type specifications between the closing parenthesis and the open parenthesis must not be in the same order as the parameter names in the parenthesized list; the order in the list controls the order of the parameters. \

+5


source







All Articles