By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

parameters to main

P: 12
hi
I wrote a program and that should takes some parameters when it is executed
like that

program.exe /s

and I wanna write if statements according to parameters but i could not.
for instance;

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3.  
  4. main(char c)
  5. {
  6. if(c=='s')
  7. func1();  // appropriate function
  8. else 
  9. func2();
  10.  
  11. return 0;
  12. }
  13. I think question is simple.I try to take some parameters from main but my prog. could not work i do something wrong
  14.  
  15. please help me 
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
Nov 11 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
main has the following prototype

int main(int argc, char **argp)
{
// Code here
}

You are not at liberty to change it.

argc and argp however allow you access to the command line. argp is a pointer to an array of char pointers (some people like to write it as char *argp[] to reflect this). argc is the size of this array. The only thing you can be sure of is that

argp[argc] == NULL

this is garunteed by the standard for a conforming implementation.

However normally

argp[0] is the name of the program running

and

argp[1] ... argp[argc-1] are the other items appearing on the command line split of white space characters.

In your example

argc = 2
argp[0] = "program.exe"
argp[1] = "/s"
Nov 11 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.