An introduction to using command line arguments in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
Your channel is too underrated. Your way of explaining things is unbelievable, u make everything seems so easy to understand. It would be great if u made a video on how to explain things as you do in your videos
Wow thank you very much for the very kind feedback, I appreciate that, it's very encouraging to me. :-) Maybe one day I can make a video on "teaching CS" or something like that, that could be fun.
@@PortfolioCourses hi sir I have been following your channel from month I came out one question where the command line arguments will get stored in memory . I searched in Google , stack overflow but I couldn't find anything.can you pls tell me where are the command line arguments will get stored if it's heap or stack someone answers I found in heap Can you pls clarify my doubt..
@@ramakrishna4092 Great question Rama! 🙂 It is not defined as part of the C language standard where the command-line arguments are stored. This means that it's technically possible for the command-line arguments to be stored on the stack OR the heap. As a practical matter though, I am unaware of any compiler that stores them on the heap, to my knowledge all compiler store them on the stack (as with other function arguments/parameters).
@@PortfolioCourses Yes, agreed with you. Command line arguments are stored in an array, and because, they are associated with the main, which is a function, and are local arguments to the main function, there fore, it should be stored in a stack. Besides, Command line arguments are initialized inside main function, therefore, their scope is local to the main function. And variables, which have local scope, are stored inside stack.
@@lradhakrishnarao902 So if we have: int main(int argc, char *argv[]) then the variables argc and argv will definitely be local to main. We could also write **argv instead. But argv is really just "an array of pointers to strings" (however we want to phrase it). So while argv is a parameter in main, it stores a memory address where the actual data is, and the location in memory that it points to could technically be on the stack or the heap, we don't know.
You are a very good teacher. I hope you have videos on python and other languages. I have learned a lot from your videos. More info than an hour long tutorial
Thank you for the positive feedback Stephanek! :-) Right now in addition to C, I have playlists covering Python: Tutorials: th-cam.com/video/HhtcF4jEKNQ/w-d-xo.html Examples: th-cam.com/video/ZLCZkMk69y0/w-d-xo.html And I have playlists covering C++: Tutorials: th-cam.com/video/qWPlRubVQ38/w-d-xo.html Examples: th-cam.com/video/Fk-9KCBOsp8/w-d-xo.html
@@PortfolioCourses any chance you're going to make some Rust videos? looks like it's all the hype these days and there's not many channels covering it.
Hello. I'm on windows and I have no idea how to work like you do through the command prompt (locate and run the program, etc). Could you help me with that?
i am looking for some help. I want to do exactly what you did here. But instead of doing atoi to store the argument in an int, I want to store it inside of a char array. Let's say the input in the command line is a word. I want to put it in a char array. How do I do this?
Great question! I would try using strcpy() to copy the word into a char array, this video may help you out: th-cam.com/video/RQ8O13utXQw/w-d-xo.html. 🙂
Thank you so much, your video is really Great!!! So helpful with all these details !!!! ------------------- At the end of the video I was asking myself: When was it specified in this code that there will be only 3 arguments? (hummmmm it was like [ using the Sherlock Holmes microscope]) lol Very interesting: because we have declared only two parameters (lower and higher) we had specified the length of the array (char *argv[]) . And here argc = 3 , the second argument = lower and the third = higher. It actually works with return (-1) instead of exit(-1) --------------------------------------------------------------------------------------------- #include #include int main(int argc, char *argv[]) { if(argc != 3) { printf("Two args required! "); //exit(-1); return (-1); } int i; int lower; int higher; lower = atoi(argv[1]); higher = atoi(argv[2]); i = lower; printf("argc = %d ", argc); printf("argv[1] = %s ", argv[1]); printf("argv[2] = %s ", argv[2]); while(i
@@PortfolioCourses Very useful when you want to test your code many times with other cases for instance without rewriting all the code just by passing the arguments on the command line because the code is already compiled! As you said, and at the end of the day, it will be easier to improve the behavior of the program much much faster 💪
What I don't understand is why would I use this instead of scanf, because in both cases I'm receiving an input from the user via terminal, just that in the first case is before executing the program, which also I find as a maybe issue because there's no feedback that arguments are expected or required since it can't be a printf before executing the program
Great question! :-) So one example of where you might do this is if you were creating a command-line utility of some sort. For example we use a tool like ssh to connect to remote servers: phoenixnap.com/kb/ssh-to-connect-to-remote-server-linux-or-windows. The tool accepts the server to connect to as a command-line argument, among other things. The people that use this tool are expected to know how it works, and there is what's called a "man page'" that allows them to learn how to use the command (in addition to other forms of documentation). Because it's a tool people use all the time as part of their work, they are expected to know how it works, as opposed to being provided with "an interface" that asks them questions (which would be much slower). We would say that this type of user is a "power user". Another advantage to command-line arguments is that we can then use our program as part of shell scripts. Shell scripts allow us to run programs and give them arguments via the command-line, so we can automate tasks and essentially "write a program that calls and uses other command-line programs": en.wikipedia.org/wiki/Shell_script.
@@PortfolioCourses oh I see so it's like gcc where I'm expected to know how it works or read it elsewhere, and yes now that you say it, it does make it way faster and comfortable to use command line arguments instead of executing a program that ask a lot of questions, I more or less get the idea, thank you very much for the answer!
char *argv[] is an array of pointers to chars, and char *argv is a pointer to a char. So those are not equivalent. That said you'll often see char **argv used for the 2nd parameter instead of char *argv[], and that's actually fine because we don't actually have array parameters in C. When we have a parameter char array[] for a function really that just means char *array in practice, because when we "pass" arrays to functions what we really pass are pointers to the array (i.e. a pointer to the first element in the array). Personally I prefer char *argv[] because it communicates that we have an array of pointers to chars (i.e. strings), but I don't feel strongly about it either. :-)
Your channel is too underrated. Your way of explaining things is unbelievable, u make everything seems so easy to understand. It would be great if u made a video on how to explain things as you do in your videos
Wow thank you very much for the very kind feedback, I appreciate that, it's very encouraging to me. :-) Maybe one day I can make a video on "teaching CS" or something like that, that could be fun.
Simple case best cases to learn from. Too many tutorials make it way too fancy and confusing. Thank you for this video
You're welcome! :-D
@@PortfolioCourses hi sir
I have been following your channel from month I came out one question where the command line arguments will get stored in memory . I searched in Google , stack overflow but I couldn't find anything.can you pls tell me where are the command line arguments will get stored if it's heap or stack someone answers I found in heap
Can you pls clarify my doubt..
@@ramakrishna4092 Great question Rama! 🙂 It is not defined as part of the C language standard where the command-line arguments are stored. This means that it's technically possible for the command-line arguments to be stored on the stack OR the heap. As a practical matter though, I am unaware of any compiler that stores them on the heap, to my knowledge all compiler store them on the stack (as with other function arguments/parameters).
@@PortfolioCourses Yes, agreed with you. Command line arguments are stored in an array, and because, they are associated with the main, which is a function, and are local arguments to the main function, there fore, it should be stored in a stack.
Besides, Command line arguments are initialized inside main function, therefore, their scope is local to the main function. And variables, which have local scope, are stored inside stack.
@@lradhakrishnarao902 So if we have:
int main(int argc, char *argv[])
then the variables argc and argv will definitely be local to main. We could also write **argv instead. But argv is really just "an array of pointers to strings" (however we want to phrase it). So while argv is a parameter in main, it stores a memory address where the actual data is, and the location in memory that it points to could technically be on the stack or the heap, we don't know.
i started comp science major one month ago with no background in programming and your videos have been really helpful especially this one! thank you
Your channel is my go-to when I need any information about C language! Thank you for your work, I really appreciate you.
You're very welcome, I'm glad you find the channel useful, and thank you for the support! :-)
I was doing my assignment. And i saw your video which is exactly what i want. Thank you so much🙂.
You're welcome, and good luck on your assignment Dhun! :-)
You are a very good teacher. I hope you have videos on python and other languages. I have learned a lot from your videos. More info than an hour long tutorial
Thank you for the positive feedback Stephanek! :-)
Right now in addition to C, I have playlists covering Python:
Tutorials: th-cam.com/video/HhtcF4jEKNQ/w-d-xo.html
Examples: th-cam.com/video/ZLCZkMk69y0/w-d-xo.html
And I have playlists covering C++:
Tutorials: th-cam.com/video/qWPlRubVQ38/w-d-xo.html
Examples: th-cam.com/video/Fk-9KCBOsp8/w-d-xo.html
@@PortfolioCourses any chance you're going to make some Rust videos? looks like it's all the hype these days and there's not many channels covering it.
You are a great tutor. Keep going please.
Thank you very much for the kind feedback, I definitely intend to keep making more videos! :-)
thank you so much, you somehow explained this in the best possible way.
You're welcome, I'm glad you enjoyed the explanation! :-)
Very easy understanding and useful tutorial.
Thank you! 🙂 I'm really happy to hear that you found it easy to understand and useful!
Your channel is the reference
Thank you! :-)
Very helpful!
I’m glad it was helpful for you! :-)
Hello. I'm on windows and I have no idea how to work like you do through the command prompt (locate and run the program, etc). Could you help me with that?
Maybe refer to a video of yours I haven't seen. Also, I'm new to this channel and you've been really helpful so far!
i am looking for some help. I want to do exactly what you did here. But instead of doing atoi to store the argument in an int, I want to store it inside of a char array. Let's say the input in the command line is a word. I want to put it in a char array. How do I do this?
Great question! I would try using strcpy() to copy the word into a char array, this video may help you out: th-cam.com/video/RQ8O13utXQw/w-d-xo.html. 🙂
@@PortfolioCourses it worked. Thanks so much for the helo
Thank you so much, your video is really Great!!! So helpful with all these details !!!!
-------------------
At the end of the video I was asking myself: When was it specified in this code that there will be only 3 arguments? (hummmmm it was like [ using the Sherlock Holmes microscope]) lol
Very interesting: because we have declared only two parameters (lower and higher) we had specified the length of the array (char *argv[]) . And here argc = 3 , the second argument = lower and the third = higher.
It actually works with return (-1) instead of exit(-1)
---------------------------------------------------------------------------------------------
#include
#include
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("Two args required!
");
//exit(-1);
return (-1);
}
int i;
int lower;
int higher;
lower = atoi(argv[1]);
higher = atoi(argv[2]);
i = lower;
printf("argc = %d
", argc);
printf("argv[1] = %s
", argv[1]);
printf("argv[2] = %s
", argv[2]);
while(i
Yep, return -1 will work too!
@@PortfolioCourses Very useful when you want to test your code many times with other cases for instance without rewriting all the code just by passing the arguments on the command line because the code is already compiled! As you said, and at the end of the day, it will be easier to improve the behavior of the program much much faster 💪
What I don't understand is why would I use this instead of scanf, because in both cases I'm receiving an input from the user via terminal, just that in the first case is before executing the program, which also I find as a maybe issue because there's no feedback that arguments are expected or required since it can't be a printf before executing the program
Great question! :-) So one example of where you might do this is if you were creating a command-line utility of some sort. For example we use a tool like ssh to connect to remote servers: phoenixnap.com/kb/ssh-to-connect-to-remote-server-linux-or-windows. The tool accepts the server to connect to as a command-line argument, among other things. The people that use this tool are expected to know how it works, and there is what's called a "man page'" that allows them to learn how to use the command (in addition to other forms of documentation). Because it's a tool people use all the time as part of their work, they are expected to know how it works, as opposed to being provided with "an interface" that asks them questions (which would be much slower). We would say that this type of user is a "power user". Another advantage to command-line arguments is that we can then use our program as part of shell scripts. Shell scripts allow us to run programs and give them arguments via the command-line, so we can automate tasks and essentially "write a program that calls and uses other command-line programs": en.wikipedia.org/wiki/Shell_script.
@@PortfolioCourses oh I see so it's like gcc where I'm expected to know how it works or read it elsewhere, and yes now that you say it, it does make it way faster and comfortable to use command line arguments instead of executing a program that ask a lot of questions, I more or less get the idea, thank you very much for the answer!
@@germankoga8640 You're welcome! 🙂
Is char *argv[] the same as char *argv ? Is it a charcater pointer - if yes, then should just passing char *argv or char argv[] work?
char *argv[] is an array of pointers to chars, and char *argv is a pointer to a char. So those are not equivalent.
That said you'll often see char **argv used for the 2nd parameter instead of char *argv[], and that's actually fine because we don't actually have array parameters in C. When we have a parameter char array[] for a function really that just means char *array in practice, because when we "pass" arrays to functions what we really pass are pointers to the array (i.e. a pointer to the first element in the array). Personally I prefer char *argv[] because it communicates that we have an array of pointers to chars (i.e. strings), but I don't feel strongly about it either. :-)
*argv[] is equivalent to **argv not *argv.
What does exit(-1) mean?
exit(-1) will stop the program with an exit status of -1, which signifies something is wrong :-)
This video may be helpful: th-cam.com/video/8RucxSeAemw/w-d-xo.html :-)
900th like