|
argc and argv are two arguments that are often used in C++ programs. argc is an int and argv is a char. argc holds the number of arguments starting with 0. argv holds the list of arguments, the 0 argument is always the program. To obtain an argument from argv, you do argv[argument number]; Here is an example program :
/* C:folderarg.exe */
/* This program demonstrates argc and argv. */
#include /* Include stdio.h for the printf() function. */
using namespace std; /* Declare scope. */
int main(int argc, char *argv[]); /* Prototype the main() function as an int, because it returns an integer. This may not be necessary on your compiler. */
int main(int argc, char *argv[]) /* Put the argc and argv into the main() function. argc is the number of arguments, and argv is the list of arguments */
{ /* Begin the main() function. */
for (int count = 0; count < argc; count ++) /* Initialize a for loop to print the list of arguments. We start with 0. */
{ /* Begin the for loop. */
printf("%s", argv[count]); /* Use the printf() function to print arguments to the screen. %s represents a string of characters. */
} /* End the for loop. */
return 0; /* Return the integer value 0 to end the main() function. */
} /* End the main() function. */
If you dragged a C:folderfile.ext on to this program's icon, it would output : C:folderarg.exe C:folderfile.ext but if you double clicked it, it would output : C:folderarg.exe These two arguments can be useful when making an interpreter or some program to analyze files of a specific type. I used this code when I created the Broccolidog & Cauliflowerdog Script scripting language. There are many more uses for this code, and it is free.
|