Hello, and welcome back.
In this lecture, I want to talk about command line arguments.
The reason I included this information in the variables and
data types section is because command line arguments are a way
to pass data into your program.
So it's sort of like passing a variable into your program.
And it's very useful and you often do it, so I wanted
to cover this real quick.
So command line arguments.
Basically, there are times when a program is developed that requires
the user enter a small amount of information at the terminal.
So just like this implies, command line arguments are
essentially program arguments.
It's how you can pass data to a program.
So when you ask a user or a program needs data, that's dynamic,
that's entered by the user, there's a number of different
ways you can pass this data in.
You can either ask the user for it or you can pass it as a program.
And the reason you'd want to do this is because again you might
need the user to enter a number that indicates the triangular number that
you want to have calculated or the user has to enter a word that you
want to look up in a dictionary.
Not all data is static or doesn't change.
Some data you need to ask the user to input.
And so like I said you could do this one of two ways.
You can either request the data from the user and we're going to talk about
that in upcoming lectures or you can supply the information the program
at the time the program is executed.
And this is what is referred to as command line arguments.
Passing data to your program via the command line when the program is run.
And so we already know that every c program has a main function.
This is a special function it's basically the entry
point of the program.
It's also special because it can be used to get command line arguments.
So when the main is called by the runtime systems, two arguments
are actually passed to this function, command line arguments.
The two arguments are the first one is the argument count.
How much data is being passed to the program.
It's an integer value.
It specifies the number of arguments typed at the command line.
The second argument which is represented by arg v
for argument vector is an array of character pointers.
Now you may ask yourself what's an array.
We don't know what an array is yet.
But again, it's important to understand how you can pass
in command line arguments.
You can just think of an array as many different variables for now.
So first argument arg c for the argument, count
second argument is a vector.
Remember, arguments are how you can pass in data to a function.
That's all it is. It's really not even that that complicated, so
the main function you can pass the main function some data.
When you run the program, that data can be specified when you run
the program at the command line.
So remember, when you run a program either double click on it if it's
an executable or if you're on Linux or windows you you run it
by doing a dot slash a dot out.
Well, when you do that you can actually pass in
things after the program.
So you could say program.exe and then put parameters
after that, data after that.
And I'm going to show you how to do that.
So again the first entry in the array is a pointer the name of the
program that is executing and then everything after that would be the
arguments and I'll demonstrate this.
So here's an example this is the main function and it has two
arguments or two parameters, really.
We'll talk about the difference between parameters
and arguments as well.
But you see it says int argsie and then char and
there's a star asterisk arg v.
Again, don't worry about that basically everything after the
comma would be some data that you're passing to the program.
And the arg c would be the number of the data.
So let's go through an example that's the best way to learn this.
So here's our example.
If we want to pass in data to our program via the command line, we
have to have parameters in our main.
Up until this point, main has just been empty with two empty parenthesis.
So the first thing we have to do is just have to say arg c.
It can actually be any name but arg c represents the argument counts.
And then char star arg v.
And again, don't worry about this syntax for now.
But we do have to add those two first.
And so I'm going to create some variables here that demonstrate
data being passed and.
So because the data's being passed, I need to store it somewhere.
So I'm going to create a variable of type int just
that is number of arguments.
And this is going to be equal to arg c.
Arg c is just the variable that's passed in, and I'll
show you what this represents.
I'm going to create another variable that just represents
our argument and this is going to be equal to arg v zero.
Again, don't worry about the arg v zero syntax.
What this line of code is doing on line 13 is it's it's setting argument
1 variable equal to the program name.
And then if we do another one, we say character star argument
two and we're going to set that equal to arg v sub 1.
And then we're just going to print out this data.
So we're going to say print out number of arguments
and we want to print out the value variable so we have to use this format specify.
We're going to say percent d.
And then after the comma, we're going to say number of
arguments because this is a variable that we want to display.
And then we have another print statement that's going
to print out argument 1.
And this is going to represent the program name.
And again, we have a percent symbol so we're going to say percent s
because this is a string again not important to understand at this point.
Oftentimes, when you demonstrate code examples, you want to print
out the value of variables.
And so these printf statements on 16 and 17 are printing out the value
of the variables that are passed to main, which are command line arguments
that's why I'm displaying it.
And then the last one I want to display is our second argument, and
this is the command line argument, in addition to the program name.
This would be argument 2 that we're going to display.
And then we're going to return 0.
So it's a very simple program in terms of it doesn't have
a lot of functionality.
It just basically gets data from the function that's passed
in and displays that data.
So when I said you can pass data to a program.
Again, remember that when you execute this program, we're just
double clicking on a menu option.
But what's really going on is you're executing a command, either double
click or executing exe or in linux or unix you're doing a dot out.
And so when you execute the program, you can actually pass data to it.
So since we're not actually executing the command, we're doing the menu,
we have to pass the data via the integrated development environment.
And luckily code blocks has a nice way of doing that.
To pass data as a command line argument to a program, all you
have to do is go to your project, highlight it, go up to the menu
option that says project and then go to set programs arguments.
And see here, a window will pop up and here basically you
can just say your arguments.
And you can put in as many as you want in here.
Let's start.
I'm just going to print my name Jason.
I'm going to pass that argument that data into my program
when the program is run.
And I'm going to say okay.
So I'm going to go ahead and compile this.
And when I run it, what do you think the value of number
of arguments is going to be.
Well, remember that there's one argument that's passed which is
the program name and then I'm passing in Jason, so this should
be equal to 2, the number 2.
Argument 1 is going to be the program name so this should be
the program name whatever I named it and i can't remember if this
will generate a test or not.
Then argument 2 should be Jason because that's what I am passing in.
So now if i run it let's see what happens we say run, you'll see
that number of arguments is two.
Argument one is the program name and it's very hard to read the
format because there's no new lines.
So let's start this over and actually add some new lines.
How can we add new lines to be printed.
We can just use an escape sequence, which is a backslash n.
So let's add a backslash n after the percent d for the
first print statement and a backslash n after the next one.
So it's more readable as output.
And now let's save that let's build it again and let's run it and
see what the output is this time.
Hopefully, it'll be easier to read.
Now when we execute it, you'll see that there's separate
lines, much easier to read.
Number of arguments gets displayed as two because as I mentioned the
first argument is the program name the second argument is what I passed in.
And argument 1 is actually displayed as the name of the program,
which is my entire directory structure and then test.exe.
And argument 2 is Jason because that's what i set in the ID.
Now I can go and I can modify the program arguments by going
to the project menu option and saying project go to set program
arguments and I can change this.
Now when I change this, if I change it to something else
or I'll just say something.
Now when I run it, the data that's going to be passed to the
program is going to be something different, it's going to be and
actually all you have to do is run it you don't have to recompile.
Now it's just something that's displayed.
And so this is a way that you can pass data to a program without asking
for it from the user specifically.
You just type it in in the IDE and pass it to the program.
And you can pass many many arguments.
You don't just have to pass that one.
If you wanted to pass more, you would just again go to set program arguments
and either separated by new lines, of course, if you wanted to use them,
you would have to access them in the array but you can pass in many.
So this is useful to pass data to a program without asking the user
to type something in the keyboard.
If you didn't do it this way, the program would pause and it would
say hey user please enter something.
And then the user would enter something and then you'd have to
save it in a variable and then you have to continue executing.
This way you don't have to ask the user for anything you just
pass it in as part of the program.
So this is what command line arguments are.
Don't worry about these other concepts like arrays, you see the asterisk
for pointers, don't worry about that.
Again, this is just displaying the command line arguments.
And the way that you access them is through these
parameters in the function.
But again, the main point is this is how you can do it.
And it's useful it will be useful for other programs and
is associated with variables.
Thank you.