1

If I write a function such as this:

void myfunc(const int argc, const char *argv[]) 

Will argc and argv automatically get their values from command line arguments, or will they need to be passed in their values from main?

user1519226
  • 77
  • 2
  • 12
  • 5
    It must be deliberately passed to the function. E.g `myfunc(argc-1, argv+1);` – BLUEPIXY May 02 '17 at 02:47
  • 4
    Only main() get values from command line. – Nguai al May 02 '17 at 02:49
  • Possible duplicate of [Access command line arguments without using char \*\*argv in main](http://stackoverflow.com/questions/2471553/access-command-line-arguments-without-using-char-argv-in-main) – Lanting May 02 '17 at 08:07

3 Answers3

3

argc and argv must be passed, such as:

int main(int argc, char *argv[])
   {
   myfunc(argc, argv);
   return(0);
   } 
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
3

No, nothing special happens if you call a function's arguments argc and argv. The caller has to pass them, like any other arguments.

user253751
  • 57,427
  • 7
  • 48
  • 90
3

Argument names are not significant per se. You can write main like this:

int main(int count, char *array[]) {...}

if you like. main is a special function because it is the default entry point of a C program and that command-line arguments values are passed to it, that's all.

Declaring/defining a function as:

void myfunc(int argc,char *argv[]) {...}

is exactly the same as:

void myfunc(int foo,char *bar[]) {...}

and such a function can be called from any (possible) point you like with any (acceptable) values you like.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69