Command Line Arguments | C Programming Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • 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!

ความคิดเห็น • 45

  • @Anas-qh3hk
    @Anas-qh3hk ปีที่แล้ว +18

    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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +4

      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.

  • @iCrazy414
    @iCrazy414 2 ปีที่แล้ว +23

    Simple case best cases to learn from. Too many tutorials make it way too fancy and confusing. Thank you for this video

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +4

      You're welcome! :-D

    • @ramakrishna4092
      @ramakrishna4092 2 ปีที่แล้ว +2

      @@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..

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +4

      @@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).

    • @lradhakrishnarao902
      @lradhakrishnarao902 ปีที่แล้ว

      @@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.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +2

      @@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.

  • @___nia
    @___nia 10 หลายเดือนก่อน +2

    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

  • @me_dimko
    @me_dimko ปีที่แล้ว +6

    Your channel is my go-to when I need any information about C language! Thank you for your work, I really appreciate you.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +3

      You're very welcome, I'm glad you find the channel useful, and thank you for the support! :-)

  • @dhunpatel5319
    @dhunpatel5319 ปีที่แล้ว +2

    I was doing my assignment. And i saw your video which is exactly what i want. Thank you so much🙂.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      You're welcome, and good luck on your assignment Dhun! :-)

  • @stephanek9008
    @stephanek9008 ปีที่แล้ว +3

    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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +5

      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

    • @pietraderdetective8953
      @pietraderdetective8953 10 หลายเดือนก่อน

      @@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.

  • @Fatih66622
    @Fatih66622 ปีที่แล้ว +1

    You are a great tutor. Keep going please.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Thank you very much for the kind feedback, I definitely intend to keep making more videos! :-)

  • @boonie6
    @boonie6 4 หลายเดือนก่อน

    thank you so much, you somehow explained this in the best possible way.

    • @PortfolioCourses
      @PortfolioCourses  4 หลายเดือนก่อน +1

      You're welcome, I'm glad you enjoyed the explanation! :-)

  • @kuchkuch9304
    @kuchkuch9304 ปีที่แล้ว

    Very easy understanding and useful tutorial.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      Thank you! 🙂 I'm really happy to hear that you found it easy to understand and useful!

  • @yoruichi5802
    @yoruichi5802 ปีที่แล้ว +1

    Your channel is the reference

  • @cipher1167
    @cipher1167 7 หลายเดือนก่อน

    Very helpful!

    • @PortfolioCourses
      @PortfolioCourses  7 หลายเดือนก่อน +1

      I’m glad it was helpful for you! :-)

  • @phantom7333
    @phantom7333 ปีที่แล้ว

    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?

    • @phantom7333
      @phantom7333 ปีที่แล้ว

      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!

  • @DataStructures
    @DataStructures ปีที่แล้ว

    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?

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      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. 🙂

    • @DataStructures
      @DataStructures ปีที่แล้ว

      @@PortfolioCourses it worked. Thanks so much for the helo

  • @zoquevil4792
    @zoquevil4792 2 ปีที่แล้ว

    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
      @PortfolioCourses  2 ปีที่แล้ว +1

      Yep, return -1 will work too!

    • @zoquevil4792
      @zoquevil4792 2 ปีที่แล้ว

      @@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 💪

  • @germankoga8640
    @germankoga8640 ปีที่แล้ว

    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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      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.

    • @germankoga8640
      @germankoga8640 ปีที่แล้ว

      @@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!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      @@germankoga8640 You're welcome! 🙂

  • @ValliNayagamChokkalingam
    @ValliNayagamChokkalingam ปีที่แล้ว

    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?

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      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. :-)

    • @IO-_-Ol
      @IO-_-Ol 7 หลายเดือนก่อน

      *argv[] is equivalent to **argv not *argv.

  • @Stophidinginthecomments
    @Stophidinginthecomments 11 หลายเดือนก่อน

    What does exit(-1) mean?

    • @PortfolioCourses
      @PortfolioCourses  11 หลายเดือนก่อน +1

      exit(-1) will stop the program with an exit status of -1, which signifies something is wrong :-)

    • @PortfolioCourses
      @PortfolioCourses  11 หลายเดือนก่อน +1

      This video may be helpful: th-cam.com/video/8RucxSeAemw/w-d-xo.html :-)

  • @user-cu3sp4tm3u
    @user-cu3sp4tm3u 5 หลายเดือนก่อน

    900th like