CS计算机代考程序代写 Microsoft PowerPoint – 27_CommandLine Args.pptx

Microsoft PowerPoint – 27_CommandLine Args.pptx

1

CS 2211
Systems Programming
Command Line Arguments

1

2

int main( int argc, char *argv[])

The most important function of C is main() function.
It is mostly defined with a return type of int and without parameters

int main(void)

We can also give command-line arguments in C.

Command-line arguments are given after the name of the program in command-line
shell of Operating Systems.

To pass command line arguments, we typically define main() with two arguments :
first argument is the number of command line arguments
second is list of command-line arguments.

Command Line Arguments

3

int main( int argc, char *argv[])

argc (ARGument Count) is int and stores number of command-line arguments passed
by the user including the name of the program.

So if we pass a value to a program, value of argc would be 2 (one for argument and one
for program name)

The value of argc should be non negative.

argv(ARGument Vector) is array of character pointers listing all the arguments.

If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.

Argv[0] is the name of the program , After that till argv[argc-1] every element is
command -line arguments.

Command Line Arguments

4

#include
#include

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

int counter;
printf (“\nProgram name: %s\n\n”, argv [0]);
if (argc > 1) {

for(counter=0;counter
#include

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

int counter;
printf (“\nProgram name: %s\n\n”, argv [0]);
if (argc > 1) {

for(counter=0;counter