I have been coding in C for a very long time, and I thought I had it all figured out, until I saw this code in an MD5 library:

static void MDPrint (mdContext)
MD5_CTX *mdContext;
{
  int i;

  for (i = 0; i < 16; i++)
    printf ("%02x", mdContext->digest[i]);
}

static void MDString (inString)
char *inString;
{
  MD5_CTX mdContext;
  unsigned int len = strlen (inString);

  MD5Init (&mdContext);
  MD5Update (&mdContext, inString, len);
  MD5Final (&mdContext);
  MDPrint (&mdContext);
  printf (" \"%s\"\n\n", inString);
}

I am using gcc to compile it in Linux, and it works fine. The part that surprised me was the function structure. They are declaring type-less inString and mdContext within the parenthesis, and immediately after that they are redeclaring them as variables floating outside the function body.

I looked in all the C literature thinking maybe I skipped a chapter somewhere a long time ago, but no C reference shows this style of function structure. I googled every combination of keyword that could think of, but there is no mention anywhere.

So here are my questions:

1) What are the rules of this function structure? A link to a website explaining it would be awesome.

2) What are the advantages of this function structure? Are there disadvantages?

2.A) If there is no difference, Is it just a style of writting parameters? Is there an advantage to the style itself?

2.B) Is there any difference in the machine code generated by the compiler between this style and the standard style?

3) Is there a name or term to this function structure? If so what is it?

4) Is this standard C or is this a gcc extension of some sort?


Thanks for your help.

1) What are the rules of this function structure? A link to a website explaining it would be awesome.
This is how function parameters were defined in what is known as "K&R-C", which existed before ANSI/ISO got in on the act.
http://stackoverflow.com/questions/22500/what-are-the-major-differences-between-ansi-c-and-kr-c

2) What are the advantages of this function structure?
Really old compilers will still work.
> Are there disadvantages?
No cross-function type-checking would be one biggie.

2.A) If there is no difference, Is it just a style of writting parameters? Is there an advantage to the style itself?
No, see above.


2.B) Is there any difference in the machine code generated by the compiler between this style and the standard style?
The caller might generate different code, because it doesn't really know what the prototype for the function should be.

3) Is there a name or term to this function structure? If so what is it?
See above.

4) Is this standard C or is this a gcc extension of some sort?
No, it's a backward compatibility thing for the dim and distant past.

This is the old-style format for function. I used to use them in the early 80's until if found out the style changed.

1) The rules to this style is basically don't use it.
2) Disadvantages -- it will confuse people (look in a mirror :icon_wink:)
2A) Just different
2B) I doubt it
3) Obsolete?
4) standard C

Shows how old that code it, eh?

Thank you both for your quick replies. Very interesting stuff.

It looks like the "old" style is redundant, since it repeats the parameter list. And it would seem that they chose the C++ style because that redundancy was eliminated.

What other backwards compatible quirks are there in C? It would be nice to know them so in the event I run into another anomaly I can know what I am looking at.

There is no C++ style chosen. The definition in C was changed. Had nothing whatever to do with C++.

There is no C++ style chosen. The definition in C was changed. Had nothing whatever to do with C++.

My brain was somewhere else when I posted that. C++ was much newer than C, so it was an illogical association.

As for the answer to my own question, here is a very detailed explanation of the K&R C Programming Style:

http://www.iu.hio.no/~mark/CTutorial/CTutorial.html

Of course, as said before, the contents of that document are obsolete, but apparently still functional in some compilers.

This article has been dead for over six months. Start a new discussion instead.