Take the 2-minute tour ×

some times i see that functions are defined as below:

char    *dir;   
        DIR * dirp;
        struct dirent *d;

        /* open directory */
        dirp = opendir(dir);
  ......... so  on

here what is the importance of the statement

char    *dir;

what is the intension behind declaring the pointer soon after the function name and then starting the function body.

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

It's an older C syntax, kalled "K&R C" since it's how it appeared in the original version of the legendary book.

What used to be written like this:

foo(a, b)
int a;
int b;

is now

int foo(int a, int b)
share|improve this answer
add comment

It is just "old style", K&R C function definition (see Kernighan & Ritchie's book, commonly referred to as simply Kernighan & Ritchie.)

The code you refer to may have been written in the late eighties, or early nineties with portability (i.e. compatibility with older compilers, possibly on more "exotic" platforms) in mind.


Some people may believe that K&R-style function definition, still supported by compilers, are more readable, which is in fact not necessarily true; compare:

char    *param1;   /* param1 comment */
int     param2;    /* param2 comment */
short   param3;    /* param3 comment */


/* notice also that return type is explicitly specified now */
  char    *param1, /* param1 comment */
  int     param2,  /* param2 comment */
  short   param3   /* param3 comment */

K&R-style function definitions have been obsolete since 1989; see section 6.9.5 "Function definitions" in the C90 standard.

share|improve this answer
add comment

Your Answer


