Embed presentation
Download to read offline


![Command-Line Arguments
In C you can pass arguments to main() function.
Main() prototype
int main(int argc, char * argv[]);
argc indicates the number of arguments
argv is an array of input string pointers.](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-3-2048.jpg)
![ What value is argc and argv?
#include <stdio.h>
int main(int argc, char * argv[]);)
{
int i=0;
printf("Hello Worldn");
printf(“The argc is %d n”, argc);
for(i=0; i < argc; i++)
printf(“The %dth element in argv is %sn”,i,argv[i]);
return(0);
}](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-4-2048.jpg)
![Output
./hello 10
The argc is 2
The 0th element in argv is ./hello
The 1th element in argv is 10
The trick is the system always passes the name of the
executable file as the first argument to the main() function.
How to use your argument
Be careful. Your arguments to main() are always in string format.
Taking the above program for example, the argv[1] is string “10”,
not a number. You must convert it into a number before you can
use it.](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-5-2048.jpg)
The document discusses passing command line arguments to the main function in C programs. It explains that the argc parameter indicates the number of arguments passed, and argv is an array of string pointers to the arguments. It provides an example program that prints the argc and argv values when running "./hello 10". The output shows argc is 2, with the first element of argv being "./hello" and the second being the passed string "10". It notes the executable name is always the first argv element, and that arguments are passed as strings, so must be converted to other types like numbers before use.


![Command-Line Arguments
In C you can pass arguments to main() function.
Main() prototype
int main(int argc, char * argv[]);
argc indicates the number of arguments
argv is an array of input string pointers.](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-3-2048.jpg)
![ What value is argc and argv?
#include <stdio.h>
int main(int argc, char * argv[]);)
{
int i=0;
printf("Hello Worldn");
printf(“The argc is %d n”, argc);
for(i=0; i < argc; i++)
printf(“The %dth element in argv is %sn”,i,argv[i]);
return(0);
}](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-4-2048.jpg)
![Output
./hello 10
The argc is 2
The 0th element in argv is ./hello
The 1th element in argv is 10
The trick is the system always passes the name of the
executable file as the first argument to the main() function.
How to use your argument
Be careful. Your arguments to main() are always in string format.
Taking the above program for example, the argv[1] is string “10”,
not a number. You must convert it into a number before you can
use it.](https://image.slidesharecdn.com/commandlinearguments-220316095036/75/Command-line-arguments-5-2048.jpg)