answersLogoWhite

0

Back in the days of MSDOS, before Windows, programs were run not in a graphical shell but in a text-mode command-line environment. The name of the program was typed in, and then the user would press ENTER to run the program.

Borrowing from Unix and CP/M systems, programs were given the option of having parameters sent to them - a list separated by spaces after the filename and a space, but before ENTER was pressed. This gave users the opportunity to tell the program how to run.

Windows still uses this system for passing information to a program when it runs the program. The parameters are passed in the same way, but every programming language has its own method of reading them.

Under C, you'll have likely noticed that sometimes you start a program with the following:

int main(int argc, char **argv)

{

...

}

"argc" and "argv" are used by your program to get these parameters. "argc" contains the number of parameters, and "argv" contains the parameter list.

Because the name of your program is always supplied as the first parameter, "argc" will always be at least 1, and "argv[0]" will contain the filename of your program.

"argv" is a list of character arrays - a list of null-terminated strings. The following program will demonstrate how to access the parameters supplied to your program:

#include

int main(int argc, char **argv)

{

int count;

for (count=0; count

printf("Param %d: %s\n", count, argv[count]);

return 0;

}

If you compile this and then run it at the command line, it will display on each line the list of parameters you feed it. For instance, passing the string this is a "test that I've" written will display:

Param 0: (the full path of your program, including directory and filename)

Param 1: this

Param 2: is

Param 3: a

Param 4: test that I've

Param 5: written

User Avatar

Celia Lemke

Lvl 10
2y ago

What else can I help you with?

Related Questions

Command line argument?

Command line argument is a value that is passed to a program whenever the program is executed. It is used to avoid hard coding.


What is the length command line argument in character?

Use the function strlen(string);


How you can got the first character of an argument in command line argument in java?

in your code u always write public static void main(String args[]) { //here array args will hold the command line arguments. Indexing from zero for //first argument //with substring(args[0],1) u can get 1st char of first argument. }


What variable will capture the content of the argument passed to a script at the command line?

If you read your book, you will find the answer!


How do you get the number of argument from command line?

It is very difficult task.but I will manage it by deep thinking on on the issue.


What variable will capture the contents of the second argument passed to a script at the command line?

If you read your book, you will find the answer!


What is the command line interface used for on a cisco router?

Cisco IOS uses a command line interface (CLI) for inputting commands when configuring Cisco Routers


What is the command line interface used for a Cisco router?

Cisco IOS uses a command line interface (CLI) for inputting commands when configuring Cisco Routers


Which variable will capture the contents of the second argument passed to a script at the command line?

$2Straight from the instructor. kinda simple eh?


What reflects an accurate argument in favor of a command economy?

Which statement reflects an accurate argument in favor of a command economy


What is the getopt function in PHP?

It gets options from the command line argument list. It can be used in PHP 4.3 and above, including PHP 5.


Please someone explain you command line argument in C?

Command Line Arguments---- I am trying to explain each word one by oneCommand ------perform specific taskex. When CMD is typed in run window and then press ok button then open a black screen that is called command prompt.Command Line ------on command prompt where command is supplied that is called command line.using command prompt dos commands are executed and java program is also executed using command prompt.for executing java program command is supplied with given syntaxjava java_class_name argument1 argument2.........Java--- is a commandjava_class_name ---name of java file which you want to executeargument1 argument2 --- are the values passing to java application from out sidethese argument1 argument2 are stored in string array argument of main method