0

What does it mean if something like the two lines after the main function declaration appears in C code?

int main(argc,argv)
  int argc;
  char *argv[];
{
  // main function body
}

I've never seen anything like this before. The code works just fine, but I'm curious to know what this means. Thanks!

Ben Sandeen
  • 1,403
  • 3
  • 14
  • 17
  • 1
    that's obsolete [K&R syntax](http://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi) – Diego Jun 07 '15 at 01:11

3 Answers3

1

It is just one other way of declaring the datatype of arguments of that particular function.

Gajendra Bagali
  • 177
  • 1
  • 2
  • 12
  • No, it's actually not "just one other way of declaring the datatype of arguments". It's a way of defining the arguments the function receives - but only after those arguments have undergone default argument promotion. – Andrew Henle Jun 07 '15 at 01:15
  • Yes. You are right! So can I say - the default argument promotion is used when we are not sure of the datatype of receiving arguments. – Gajendra Bagali Jun 07 '15 at 01:26
  • While it is another way of declaring function arguments, it's a way that's not compatible at all with newer/ANSI/ISO C declarations and definitions. I just wanted to point that out clearly so no one would think they could do something like mix new-style function declarations/prototypes with old K&R style function definitions. – Andrew Henle Jun 07 '15 at 01:32
1

This is the original (read: ancient) style of declaring argument parameters in K&R C. In ANSI C standards, the form you're likely familiar with has been the standard.

See also: What are the major differences between ANSI C and K&R C?

Community
  • 1
  • 1
jangler
  • 949
  • 6
  • 7
1

That's "K&R" C. It's way out of date.

Don't use it, even if your compiler supports it. Arguments passed to a function defined in that manner will have undergone argument promotion so that every argument has the same size (via the same mechanism that varargs are promoted in up-to-date C code).

Such code also does not support function declarations/prototypes. And never try to "improve" such code by creating function prototypes - you'll break the argument promotion that the function is expecting.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56