What is command Line Arguments
We know that we can pass arguments to a function. Every C
program atleast one function
main ( ). The
programs that we have discussed so far don’t pass any arguments to main ( ). So a question that
immediately comes to one mind is that, can we pass any argument(s) to main ( ) ?
Yes, it is possible to pass arguments to main ( ) on many
systems like DOS and UNIX from the operating system’s prompt. This argument
that is passed to the main ( ) by the user at the command prompt is known as command line argument. They appear
after the program name and if more arguments appear they are separated by white
spaces.
An expanded version of main
( ) that receives command line arguments specified by the user at the
command prompt is of the form,
Here argc and argv are two
built in formal arguments in main ( ) that
can be used to receive command line arguments. The first argument argc ( argument counter ) receives the
number of command line arguments and the second argument argv ( argument vector ) is pointer to an array of char pointers. Each element in the
array of pointer points to the command line argument that is treated as a
string. The data_type specifies the
data type returned by the main ( ) is
that of int. if main ( ) does’nt
return any value then void is
specified..
Post a Comment