1

I know that to use command line arguments in C you do something like this:

int main(int argc, char **argv)
{
//use argc and argv
}

Today I was thinking about it and I realized I have never not seen them called argc and argv. Is it a requirement that they are called this or is it just convention? If it is required why is this the case?

Could I do something like this:

int main(int length, char**myinput){
//use length as you would argc and myinput
// as you would argv
}

I haven't had a chance to write a program to test it out yet.

user3282276
  • 3,674
  • 8
  • 32
  • 48
  • 1
    `argc` and `args` is just a convention that came down from the time where characters were precious... – cmaster - reinstate monica Nov 11 '14 at 20:53
  • You should *totally* call them like: `int main(int NumberOfProgramArguments, char **NullTerminatedArrayOfPointersToProgramArguments )` – 2501 Nov 11 '14 at 20:54
  • 3
    @LyingOnTheSky that's seldom true for C. Lots of things can appear to work when in fact they don't. – Quentin Nov 11 '14 at 20:54
  • 1
    I think you could even call one of them `main` – M.M Nov 11 '14 at 20:55
  • @2501 However, any decent C programmer will recognize the meaning of `int main(int argc, char** args)`... – cmaster - reinstate monica Nov 11 '14 at 20:55
  • @2501 I don't have any problem using `argv` and `argc` I was just curious – user3282276 Nov 11 '14 at 20:55
  • @Quentin What do you mean? If you test it out and it works, there's a chance that it's imaginary not really works? I don't get it I think. – LyingOnTheSky Nov 11 '14 at 20:59
  • int main(int i, char **j) works too – crashxxl Nov 11 '14 at 21:02
  • This may explain the etymology of argc and argv: [Where did the convention of naming command line arguments as 'argv' come from?](https://programmers.stackexchange.com/questions/196654/where-did-the-convention-of-naming-command-line-arguments-as-argv-come-from) –  Nov 11 '14 at 21:08
  • @LyingOnTheSky I insist on "*appear* to work". Undefined behaviour is one such source of things that seem to work until they don't work anymore for no apparent reason. – Quentin Nov 11 '14 at 23:02
  • @Quentin If it's undefined behaviour, then it's different, but if none said it. You actually found an interesting question, why in this specific situation, that behaviour is occurring. – LyingOnTheSky Nov 12 '14 at 09:03
  • @LyingOnTheSky no one mentionned it indeed, but it's just a fair warning that in C you can't just "try it and see if it works", because invisible bugs do exist :) – Quentin Nov 12 '14 at 09:18

2 Answers2

5

The C standard says that you can call them whatever you want.

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): [..]

In What does int argc, char *argv[] mean?, answerer meager says that argc and argv are named so by convention (argument count and argument vector respectively).

Community
  • 1
  • 1
2

It is just a convention, there is no requirement on the names. They are user-defined parameters like any other, so use whatever names you want.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770