1

My friend asked me could I add "frozen top line" feature to the standard less *NIX program. I started to view the source and was very surprised in function definition the filename.c:

public char *
last_component(name)
    char *name;
{
    ....
}

I wrote some code at the C and usually I met:

  • return_type function_name arguments_list; if it is function prototype
  • return_type function_name arguments_list code-block if it is function definition.

But here I found something new: return_type function_name arguments list something_mysterious; code block.

What does this syntax means?

Dmitry Dmitriev
  • 929
  • 8
  • 23

1 Answers1

4

This is old C syntax (also known as K&R style), where a function definition could be written as where the argument are declared on the subsequent line:

int func(a,b)
   int a;
   char b;
{
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    A function definition *still can* be written that way. It's still in the standard. Also, it is *not* just the same as the corresponding signature with a parameter-type list: the K&R-style definition does not provide a prototype, and if there isn't a prototype in scope at the point of call (which can be provided separately), then that has practical implications for how arguments are interpreted. – John Bollinger May 24 '19 at 12:07
  • Emphasizing what John Bollinger has said, these are **not** “just the same.” When a function declared only with the K&R style is called, the default argument promotions will be applied. When a function declared with the modern style (a *prototype*) is called, the default argument promotions will not be applied (except for arguments corresponding to `...`), and the arguments will be converted to the types of the parameters as if by assignment. *E.g.*, with a prototype, you can pass `float` for an `int` argument or vice-versa. Without a prototype, the behavior is then undefined. – Eric Postpischil May 24 '19 at 12:32
  • @eric and John, What I meant to infer was they were similar from syntax point of view, I've updated to reflect that part. – Sourav Ghosh May 24 '19 at 12:36
  • @EricPostpischil OK, I agree. I would have expanded on the prototype part, but since it's already duped, I'll remove the incorrect part. – Sourav Ghosh May 24 '19 at 12:48