Should function arguments’ types be declared in the argument list of a function or immediately following?
Function arguments should be declared in the argument list, unless you’re dealing with an out-of-date
compiler. In that case, you should use an #ifdef to do it both ways.
compiler. In that case, you should use an #ifdef to do it both ways.
There are two ways to define a function. Consider two functions, foo1 and foo2, that take one char* argument and return an integer. Say they’re defined in the following way:
/* old style */
int
foo1(p)
char* p;
{
/* body of function goes here */
}
/* new style */
int
foo2(char* p)
{
/* body of function goes here */
}
The only advantage of the old style is that it’s prettier for long argument lists.
The advantage of the new style is that it provides a function prototype as well as a function definition. Thus, if any call to foo2 is made in the same .c file in which foo2 is defined, after foo2 is defined, the compiler will check the arguments in the call with the arguments in the definition. If the arguments don’t match, the compiler will probably inform you that something is terribly wrong. (The standard doesn’t require this step, but it occurs with most compilers.) If the arguments in the call can be converted to the arguments in the definition, they will be. That happens only if the function is defined in the new style, or if a function prototype is seen. If the function is defined in the old style and no prototype was seen, no argument conversion will be performed; probably, little or no argument checking will be done either
.The only disadvantage of the new style is that there are still compilers that don’t support it. (These are mostly UNIX-based compilers that are bundled, at no extra charge, with the operating system. On the other hand, many versions of UNIX come standard with ANSI-compliant C compilers.) If you might need to deal with non-ANSI C compilers, your best bet is to pick a macro that will be defined
when prototypes and new style function definitions are supported. A header file for this macro can define it automatically, for cases in which prototypes are known to be supported:
#ifdef __ANSI__
#ifndef USE_PROTOS
#define USE_PROTOS 1
#endif
#endif
Function declarations might look like this:
#ifdef USE_PROTOS
int foo1(char*);
int foo2(char*);
#else
int foo1();
int foo2();
#endif
A function definition might look like this:
int
#ifdef USE_PROTOS
foo1(char* p)
#else
foo1(p)
char* p;
#endif
{
/* body of function goes here */
}
#ifdef USE_PROTOS
int foo1(char*);
int foo2(char*);
#else
int foo1();
int foo2();
#endif
A function definition might look like this:
int
#ifdef USE_PROTOS
foo1(char* p)
#else
foo1(p)
char* p;
#endif
{
/* body of function goes here */
}
If your software runs only on MS-DOS, MS-Windows, or Macintosh personal computers, don’t worry
about the old style; always use the new style.
Cross Reference:
VIII.1: When should I declare a function?
VIII.2: Why should I prototype a function?
No comments:
Post a Comment