0% found this document useful (0 votes)
154 views

Command Line Arguments

Command-line arguments are passed after the program name in the shell and can be accessed in C programs through the main function parameters. The number of arguments is stored in argc as an integer, and the arguments themselves are stored as strings in the argv array. argv[0] contains the program name, and additional arguments are stored from argv[1] to argv[argc-1]. A sample C program is shown that prints the number of arguments and each argument string.

Uploaded by

rohitggothi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Command Line Arguments

Command-line arguments are passed after the program name in the shell and can be accessed in C programs through the main function parameters. The number of arguments is stored in argc as an integer, and the arguments themselves are stored as strings in the argv array. argv[0] contains the program name, and additional arguments are stored from argv[1] to argv[argc-1]. A sample C program is shown that prints the number of arguments and each argument string.

Uploaded by

rohitggothi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Command Line Arguments

C Prog
Command-line arguments

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 and second is
list of command-line arguments.
•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.
cmd2.c
#include <stdio.h>

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


{
int i;
printf("\nYou have entered %d arguments: \n" , argc);

for (i = 0; i < argc; ++i)


{
printf("\n %s",argv[i]);
}
return 0;
}

You might also like