Renan Greinert
Renan Greinert

Reputation: 3436

Defining the function's argument type after the ")", is it a very old standard?

I am reading an old book about code obfuscation in C (the book was printed in 1993), and I've noticed that the functions with arguments are implemented this way:

real_dump(address, infunc, ofp)
char *address;
int (*infunc)();
FILE *ofp;
{
    /* the code goes here... */
}

Also, no return type is defined.

Is it an old standard? Is it possible to enable gcc to compile this code?

Upvotes: 17

Views: 2405

Answers (4)

ouah
ouah

Reputation: 145899

Function definitions in the non-prototype form are valid C89, C99 and C11 code.

It is called the old-style function definition but this feature is marked since C89 as an obsolescent feature.

This form should be not used in new programs.

C99 Rationale says:

"Characterizing the old style as obsolescent is meant to discourage its use and to serve as a strong endorsement by the Committee of the new style."

even K&R2 discourages its use:

"The old style of declaration and definition still works with 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."

Now your function also doesn't have a return type and omitting the return type in a function declaration or in a function definition is no longer valid since C99. Before C99, functions without a return type implicitely returned an int.

Regarding the gcc question, by default gcc compiles with -std=gnu89. It means C89 Standard + gcc extensions. So by default gcc will accept to compile a program with the functions declaration and definition in their old-style form and without a return type.

Upvotes: 21

Matteo Italia
Matteo Italia

Reputation: 126877

Ouch, this was the pre-ANSI C way to define functions, I really don't recommend using this kind of syntax in "normal" code. Still, gcc seems to accept it without problems (it's still in the standard, although marked as "obsolescent" in §6.11.7).

(by the way, as for the "missing" return type the "implicit int rule" should apply)

Upvotes: 9

Borodin
Borodin

Reputation: 126742

This is the Kernighan and Ritchie style of function declaration. K&R C preceded ANSI C, which added the C++-style function prototypes which are most commonly used now.

Upvotes: 4

ptomato
ptomato

Reputation: 57920

The lack of return type means the return type is int. That's the default.

Upvotes: 2

Related Questions