Hello and welcome.
In this video session, let's discuss the command line argument of a process.
In the main function we have two parameters, which is argc and argv.
We call this as the command line argument.
Arc is of integer type and argv is an array of pointers to string.
Before explaining this program, let's execute this and see what is the output of this program.
We have the name of this program as one dot c.
Let's compile the program.
Now we have got the output file as one.
Let's pass string.
This is a command line demo as a parameter to this exe file.
In previous examples we were just running the program, but now we are giving some extra parameter to
the exe file.
Let's execute this file.
Here if we see the value of rc is seven.
This is nothing but the total number of strings present when we execute the program.
That is, the first string is ./1 followed by second string, third, string, fourth, fifth, sixth
and seventh string.
So in total we have seven strings.
And now we are printing all the strings in a loop using argc and argv.
Here, argv v of zero points to this first string.
Argv of one points to this second string, argv of two points to this string, and so on.
Now let's see one more example.
This is just to demonstrate a small use case.
So one more point to consider here is the command line argument or stored as part of stack of the main
program.
So this forms the stack frame of the main program.
In this case, we are trying to have a small use case and we are trying to add two numbers or subtract
two numbers based on users input.
If user specifies add, then the following two numbers are added.
If user specifies a sub, then the following two numbers are subtracted.
Let's compile this program and see the output.
Let's run the program.
Now here we give ad as one of the parameters.
Then let's pass eight and three as following parameters.
In this case, when we run we have got RC equal to four.
So there are total four parameters.
And RGV points to the individual strings, that is, argv of zero points to first string, argv of one
points to the second string, and so on.
So now let's see the output.
Then understand the program.
Here we see addition results equal to 11.
That is eight plus three is 11.
Here each parameter that we pass is a string.
So here we check whether the user has provided ad or sub as command line argument.
And if the if it is ad then we add the next two parameters, that is arg v of two and arg v of three.
If you notice here the command line parameters are strings.
We are going to convert from Ascii to integer for both the parameter before performing addition or subtraction
of these variables.
Let's run the same program once again and let's pass sub as command line argument and let's see the
output.
Here the given string is converted into numbers and subtraction is performed.
So when this program is executed, a new process is forked or created, and this command line argument
forms the part of the stack frame of the main function.
In next video session, we will discuss environment of a process.
Thank you.