An introduction to using command line arguments in C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com 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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-HhtcF4jEKNQ.html Examples: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ZLCZkMk69y0.html And I have playlists covering C++: Tutorials: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-qWPlRubVQ38.html Examples: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Fk-9KCBOsp8.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.
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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-RQ8O13utXQw.html. 🙂
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?
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. :-)
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!
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 💪