user1519226
user1519226

Reputation: 77

argc and argv for functions other than main

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?

Upvotes: 1

Views: 5741

Answers (3)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

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.

Upvotes: 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.

Upvotes: 3

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6003

argc and argv must be passed, such as:

int main(int argc, char *argv[])
   {
   myfunc(argc, argv);
   return(0);
   } 

Upvotes: 3

Related Questions