SSH This
SSH This

Reputation: 1929

Function Syntax in C

I'm pretty new to C, I've been studying someone else's code and I've found this function:

static
    cp_file(output, input, record, ft)
    dy_char  *output;     /* output file */
    dy_char  *input;      /* input file */
    dy_char  *record;     /* record id */
    int     ft;          /* file type */
    {

Does this do exactly the same thing as saying this:

static cp_file(dy_char *output, dy_char *input, dy_char *record, int ft) {   

Is one more efficient than the other, or is it purely a different style of syntax? If they are different, what are the differences?

Upvotes: 0

Views: 152

Answers (3)

ouah
ouah

Reputation: 145829

No, they are not identical.

The first form is the old-style function definition and the second is the prototype form of function definition.

They differ with respect to the argument passing conversion. Functions with prototype convert arguments as if by assignment while non-prototype functions like in your first example perform default argument promotion.

Arguments conversion for the old form:

(C99, 6.5.2.2p6) "If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions."

Arguments conversion for the prototype form:

(C99, 6.5.2.2p7) "If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type."

Note that the old form (non-prototype) is obsolescent and is strongly discouraged.

(C99, 6.11.7p1 Future languages directions) "The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature."

Upvotes: 4

Bob Kaufman
Bob Kaufman

Reputation: 12815

Yes, both statements are mostly identical except as noted by ouah. This is the "original" pre-ANSI syntax for declaring functions. It went into disuse in the early 1990's.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206518

Both are identical, However,
the latter is the standard conformant syntax, while the former is the obscure K&R syntax.

Always, use the standard conformant way when writing any new code, old code bases might use the K&R syntax as it was commonly used syntax at that time.

Upvotes: 2

Related Questions