Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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?

like image 424
Renan Greinert Avatar asked Jan 15 '12 12:01

Renan Greinert


People also ask

How many types of arguments are there in functions?

Hence, we conclude that Python Function Arguments and its three types of arguments to functions. These are- default, keyword, and arbitrary arguments. Where default arguments help deal with the absence of values, keyword arguments let us use any order.

How can you get the type of arguments passed to a function?

There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.

Why is the argument type int in all character handling functions?

In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. "The next biggest type" would actually be short .

What are the 3 common parts of declaring functions?

The function body has three parts: an optional declarative part, an executable part, and an optional exception-handling part.


2 Answers

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.

like image 93
ouah Avatar answered Oct 17 '22 12:10

ouah


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)

like image 28
Matteo Italia Avatar answered Oct 17 '22 12:10

Matteo Italia