0

Possible Duplicate:
Alternate C syntax for function declaration use cases

I see this very different definition of main() in an old C-program. (It does compile, with gcc)

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

   }

What kind of variables are being declared and why are they being done before the beginning of the function brace?

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191

5 Answers5

3

This is old style of function definition in C. This non-prototype style is marked as obsolescent in the C Standard and should not be used.

And omitting the return type (implicitly int) is no longer permitted in C.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

It implicitly returns int per the C spec (although per other answers in this and the linked question this was obsoleted in C99?), as is the case with functions with undeclared return type.

This is an old style of declaration, it comes from Fortran.

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

Tells you exactly what you'd expect - that argc is an int and argv is an arryay of char *.

djechlin
  • 59,258
  • 35
  • 162
  • 290
1

This is called a K&R-style definition. You basically just list the parameters and declare them between the closing argument bracket and the opening curly bracket.

fuz
  • 88,405
  • 25
  • 200
  • 352
1

That's an old KnR C style, pre 1989 C (non-standard). It was a way to pass arguments to functions.. not only with main() that was with every other function as well.

For example:

void non_main(argx, argy)
int argx,argy;
{
   /*do something here*/
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

This is simply an alternative way of specifying the types of the arguments to main. It is exactly equivalent to main(int argc, char *argv[]). This is known as K&R style, as it's the original style used in the original C compiler and K&R books. It is considered obsolete; it is not compatible with C++. There's a GCC option to warn about it, -Wold-style-definition, but you will still see it in some old code.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 1
    It is not equivalent. For example if you have a function call `main(a, b)` and `a` is an object of type `double`, `a` will not be converted to `int` as with prototype definition. – ouah Nov 08 '12 at 18:33
  • @ouah Thanks for the clarification; I hadn't thought of that. – Brian Campbell Nov 10 '12 at 00:08