[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


Can we pass arguments to the main() function. and if...

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

Question by Richa
Submitted on 3/10/2004
Related FAQ: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)
Rating: Rate this question: Vote
Can we pass arguments to the main() function.
and if yes,give an example and relevant logic for that.


Answer by harish
Submitted on 4/8/2004
Rating:  Rate this answer: Vote
hi richa

yes of course you can pass arguments to main.
the first argument to main i.e argc called as the argument count is the total no: of arguments passed to main.the second argument is an array of character pointers called as argv.argc will be 1 even if u dont give any command line arguments since it is the name of the program itself.

here is a program that will display the arguments u passed to the main.

# include <stdio.h>

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

for (i = 0; i < argc; i++)
   printf ("%s\t",argv [i]);
}


first argument will print the proram name along with all the path.

rest depending on how many arguments u send will be printed.all the arguments are string.

both argc and argv are local to main.

 

Answer by l.gnanasekar
Submitted on 5/20/2005
Rating: Not yet rated Rate this answer: Vote
Hi dear,
  
My part.
You can pass arguments in main function the program like as

int main(int argc,char **argv,char **env)
{
     int i;
     for(i=0;i<10;i++)
         printf("%s\n",env[i]);
return 0;
}

if u run this program it prints the 10 environment variables.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.