OlivierLi
OlivierLi

Reputation: 2846

Why would you declare variables in the middle of defining a function?

I have to modify the code for the G722 codec made by the ITU at work.

I contains realy funky function definitions like this one :

    int 
    main (argc, argv)
    int argc;
    char *argv[];
    {
    .
    .
    .
    }

Why would anybody use such a syntax?

Upvotes: 0

Views: 772

Answers (2)

John Bode
John Bode

Reputation: 123458

That's very old (pre-1989 standard) function definition syntax. It's still legal for backwards-compatibility's sake, but it's definitely no longer recommended.

As to why anyone would use it, the only reasonable scenarios I can think of are:

  • this code is very old and nobody wants to risk breaking it just to bring it up to date;
  • it's hosted on a platform for which compilers for later versions of the language were never implemented;

Upvotes: 0

OlivierLi
OlivierLi

Reputation: 2846

I found the answer by googling "Old C Style Declaration" like it was suggested in comments.

The answer is :

The old-style function declarations and definitions use slightly different rules for declaring parameters than the syntax recommended by the ANSI C standard. First, the old-style declarations don't have a parameter list. Second, in the function definition, the parameters are listed, but their types are not declared in the parameter list. The type declarations precede the compound statement constituting the function body. The old-style syntax is obsolete and should not be used in new code. Code using the old-style syntax is still supported, however.

This is from : http://msdn.microsoft.com/en-us/library/efx873ys.aspx

Upvotes: 1

Related Questions