Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Untyped arguments in a C function declaration

Recently I've been looking at the some of the C example code from the online resources of Steven Skiena's "Algorithm Design Manual" and have been baffled by the syntax of some of his function calls. Admittedly it's been a while since did C at uni but I've never encountered untyped function arguments like this:

find_path(start,end,parents)
int start;
int end;
int parents[];
{
    if ((start == end) || (end == -1))
        printf("\n%d",start);
    else {
        find_path(starts,parents[end],parents);
        printf(" %d",end);
    }
}

Is this valid syntax anymore? Are / were there any benefits with this style of function declaration? It seems more verbose than the conventional inline typing of arguments.

like image 690
Alastair Avatar asked Jul 14 '11 11:07

Alastair


People also ask

What is C function declaration?

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.

Can functions have no parameters?

The lesson brief states that “Functions can have zero, one or more parameters”.

What is void foo in C?

void foo(); declares that foo is a function returning void that takes an unspecified but fixed number and type(s) of arguments. It doesn't mean that calls with arbitrary arguments are valid; it means that the compiler can't diagnose incorrect calls with the wrong number or type of arguments.

Can a function have no parameters C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.


3 Answers

They are called K&R style definitions. Don't use them in new code. Even K and R recommend that you stay away from them in "The C Programming Language 2ed".

A note of history: the biggest change between ANSI C and earlier versions is how functions are declared and defined.

The parameters are named between the parentheses, and their types are declared before opening the left brace; undeclared parameters are taken as int.

The new syntax of function prototypes makes it much easier for a compiler to detect errors in the number of arguments or their types. The old style of declaration and definition still works in ANSI C, at least for a transition period, but we strongly recommend that you use the new form when you have a compiler that supports it.

like image 104
cnicutar Avatar answered Oct 05 '22 03:10

cnicutar


This is an old style way of giving the function types. It was dropped in the C99 standard and is not appropriate for modern code.

like image 35
borrible Avatar answered Oct 05 '22 02:10

borrible


The arguments are typed, just not inline. The types are between the first line and the opening bracket.

Anyway, this style is old and not recommended.

like image 23
Mihai Maruseac Avatar answered Oct 05 '22 01:10

Mihai Maruseac