Untyped arguments in a C function declaration

I recently looked at some C code examples from the Steven Skiena Algorithm Guide online resources and were confused by the syntax of some of its function calls. Admittedly, it has been a while since C in uni, but I have never come across untyped function arguments, such as:

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 there / were there any advantages to this style declaration? This seems more verbose than the usual inline argument input.

+4
source share
5 answers

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

History Note: The biggest change between ANSI C and earlier versions is how functions are declared and defined.

Parameters are called between parentheses, and their types are declared before opening the left parenthesis; undeclared parameters accepted as int .

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

+5
source

This is an old way of providing function types. It has been reset to C99 standard and is not suitable for modern code.

+2
source

Arguments are typed, just not built-in. Types are between the first line and the opening bracket.

In any case, this style is old and not recommended.

+1
source

These K & R definitions are a traditionally traditional way of defining functions. In source C, this was the only way to define a function.

Do not use the definitions of K & R. Why not? Because it stops the compiler, which can check for type mismatches.

0
source

This old syntax stems from countless B programming languages (and others claim it is not in the C standard anymore):

 printn(n,b) { extrn putchar; auto a; if(a=n/b) /* assignment, not test for equality */ printn(a, b); /* recursive */ putchar(n%b + '0'); } 

By the way, some compilers may offer a flag that allows you to compile this old K + R code.

0
source

All Articles