0

I stumbled upon these lines when looking into putchar.c

I'm wondering about why the arguments ptr and c are declared outside the arguments body ? Is this some kind of "good old way" or does it have some actual use ?

int
_putchar_r (ptr, c)
     struct _reent *ptr;
     int c;
{
  return __sputc (c, _stdout_r (ptr));
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sad Adl
  • 35
  • 4

3 Answers3

0

Indeed it is the "olden" way of declaring a function's parameters.

I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.

GermanNerd
  • 643
  • 5
  • 12
  • You shouldn't need the syntax to remind you that C arguments are passed by value. And using old-style declarations means that the compiler won't complain if you pass the wrong number and/or type(s) of arguments (i.e., it doesn't allow for compile-time checking). *Always* use prototypes. – Keith Thompson Jun 16 '19 at 09:18
  • @Keith Thompson I only said I like it. It's the way we wrote our C code in the early 80ies...No, I don't NEED the syntax, but it is a point in it's favour. Just the same with pointers vs. references in C++: I personally like the pointer syntax way better than the reference one; there never is the slightest doubt if any indirection is involved or not. But maybe I'm just getting old... – GermanNerd Jun 16 '19 at 10:15
  • Then I'll just encourage anyone reading this not to follow your example. Pass-by-value is part of the fundamental semantics of C, not related to the syntax of a function declaration or definition. And prototypes are IMHO unambiguously better than old-style declarations and definitions -- which is why prototypes were introduced and why old-style declarations and definitions are officially obsolescent. – Keith Thompson Jun 17 '19 at 00:28
  • I liked it too - some commenting patterns grew up around it. The replacement lead to longer lines which were difficult to nagivate in *vi* on 80 character terminals. – Bathsheba Jun 17 '19 at 06:55
  • @Keith Thompson I only said I *like* the old syntax. I absolutely agree that one should not use it any more; I don't do it myself, nor did I at any time encourage anybody to do it. And *knowing* about the old syntax might be educational for newcomers to the language for the reason I already mentioned: It emphasizes the pass-by-value semantic. – GermanNerd Jun 17 '19 at 07:00
0

Its a K&R C style introduced in classic C Programming Book

krez
  • 38
  • 6
0

It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.

So a function can be defined either with a parameter type list or using the old style with an identifier list.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335