Course Content

  • clarguments

Course Content

Command Line Arguments

Hello everyone, my name is (name) and we are continuing this C programming course. (6 seconds pause)

In the last video we discussed how our memory can be efficiently used and managed.

We saw that for this memory management, we have different functions like malloc function, calloc function, free function or re-alloc function.

After learning and seeing memory management there is the time to see some command line arguments, which means our terminal through which we take the input or give the output, at the same time we can give some arguments, which means before our program starts to run.

We can give our compiler some instructions or some arguments we can provide, which they can use and perform the further actions.

So, all these things we would be discussing in this video.

So, without any delay, let's start and you see how these arguments are written.

So, our command line or our terminal, we have all seen how in the terminal we run our program, before running it, we compile it with the help of GCC compiler.

So, all these things that are happening in our space, we call that terminal or we call it as our command line.

Here since we write commands, that’s why it is called the command line.

And when we give extra arguments in it then it is called as command line arguments.

So, these command line arguments are handled by our main function, which means the main functions that we are making till now.

So, in that main function you must have seen that the argument round bracket which is there, is usually empty.

So, when is it used, when we are providing some arguments in the command line in our program and that time our main function absorbs the arguments list from there? And that’s why the round brackets, usually when we are not using the command line arguments. at that time, it usually remains empty.

So, whenever we will pass such arguments, where will they be coming and getting stored? Through the main function, they will come into our program.

So, all these things we will see and quickly understand what are the arguments that we can give in the command line, based on which our compiler can perform a few sets of actions.

2:43

So, we will quickly go towards our practical and we will open our Visual Studio code where we will see a basic argument or command line argument program.

And we will understand how these arguments are handled.

First of all, we will start with the # (hash) include line, which is our preprocessor command line.

We have imported standard input output dot h, header file and after that our main function has started and then you can see that the round brackets of the main functions are not empty.

There are a few arguments given in them as well.

So, what are these arguments? So, the first argument is of integer type, which is called ARGC and third argument is basically of a string or a pointer type which is called ARGV.

So, how will it work? We will go ahead and see.

So, what is our argc basically doing? It is checking how many arguments we are passing, like we had seen variable argument process which is there.

In the first argument we define how many arguments are going to come, in the same way the first argument in our main function tells us how many arguments will come in future.

In argc, it will be stored, how many arguments basically we are writing in our command line.

After that the second argument which is there.

It is an array, it is a string, it is a list.

In that one by one the arguments will get stored.

So, this was a process to access our command line argument and after that we have put here if, else statement.

And then you can see if argc’s value is one.

Which means, if you're passing only one argument, then your printf statement will run which will say the argument provided is and after that, whatever argument you have provided, it will get printed over here because it is been stored at 0th index position but if your argument value is more than two.

Then you can see that the printf statement will start getting printed which is writing “Too many arguments provided”.

How did this basically worked? We have passed a few arguments on the terminal.

We will see how they are passed.

And based on those arguments few conditions are processed.

And how are they getting processed? Because our main function, they had absorbed our arguments through command lines.

So, this was about a few arguments and a number of arguments.

Now we will quickly run it.

Here we have opened our command line or terminal and we came to a location where this program is.

And we have to write this command over here, “GCC CL arguments dot C”.

So, this gcc compiler will compile this particular file and after that the time comes to run it.

What will I do here? I will write “ CL arguments”.

And then after giving the space, we will provide the 1st argument.

My 1st argument is my “first argument”.

So, here we don’t have to give space between the names.

If I have to give the second argument then I can write here “second argument”.

And if I want to give a third, I can also give “third argument”.

Here I'm giving only a test.

In the place of text, we can even use the value.

Or we can provide different types of variables.

6:37

Suppose that we have provided here three arguments.

So, I will run it.

So, you can see that a statement is printed which says “Too many arguments provided”.

Why? Because in ARGC what has come? There were 3 arguments to the value that came inside was 3.

And 3 is greater than 2.

So, the printf statement has been printed over here.

If I try again and I give only one argument like I want here.

If I write “CL arguments first argument”, if I give only one argument and then enter.

So, you can see that my argument’s name has also come and this statement has been executed, which is saying that the argument provided it's the first argument.

So, the first argument has been printed here.

So, in this way, if our program has not even started.

Even at that time we can pass a few arguments via our terminals, which is via our command line.

So, only these are not our arguments, we can have different types of arguments.

If we have to use values or any other type of value or data type we have to use, then.

We can absorb it through our main function and we can use it in our program.

So, in this way, outside of the main function, which means before running our program, we can provide the arguments.

8:08

So, we saw here that through our command line how we can provide arguments.

So, this was about this video.

 If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere