Is it like this?
int main(int argc, char *argv[])
And if the first argument represents the filename (and location), then argc counts all arguments, or just those except the first one. In other words, a program with no extra arguments would have argc==1?
What is the correct form of the main function in C if i expect command line arguments?
You have two synthaxes :
1) int main(int argc, char *argv[]);
and
2) int main(int argc, char **argv);
When a program starts, the arguments to main will have been initialized to meet the following conditions:
- argc is greater than zero.
- argv[argc] is a null pointer.
- argv[0] through to argv[argc-1] are pointers to strings whose meaning will be determined by the program.
- argv[0] will be a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.
To illustrate these points, here is a simple program which writes the arguments supplied to main on the program's standard output.
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
int main(int argc, char **argv)
{
while(argc--)
printf("%s\n", *argv++);
exit(EXIT_SUCCESS);
}
Reply:yes, a program with no extra argu have argc==1.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment