Getting Command-Line Arguments in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025

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

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

    I love how you identify stumbling blocks and quickly go over the basics nobody else bothers with. Wish I had had your videos 20 years ago!

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

    Such a needed video. We need these kind of short and concise videos man

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

    Really helpful and helped me understand argv and argc better. Thank you

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

    great video! Even though you hurt me in the soul a little by calling me a beginner. It is true, of course

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

      Thanks. I hope your soul heals. We're all just travelers somewhere on the road. 😀

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

    Well explained and easily understandable which is rare.

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

    Hey brother Sorber! I don't know if you remember me but I served part of my mission in your area towards the end of 2018 and beginning of 2019. You talked to me about the difference in computer engineering and software engineering and convinced me to pursue a degree in software engineering. I happened to stumble on this video as I'm trying to quickly learn C for a project in my computer networks class. I hope all is well!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      Hi Jaxon. It's good to hear from you. My memory of that conversation is a bit fuzzy, but I hope software engineering treats you well. And, yes, we're doing well.

  • @MrBrancomusic
    @MrBrancomusic 4 ปีที่แล้ว

    Just started cs50 and could not understand command line arguments,this helped me a lot! Thank you!

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

    Clean and efficient, thank you!

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

    Wonderful video with a great example to help illustrate its function thanks a ton!

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

    Thanks Jacob, it's really helpful

  • @subliminalcastillo2126
    @subliminalcastillo2126 4 ปีที่แล้ว

    Damn that music that plays at 00:10 was funky as hell.
    It made me wanna put on my pizza guy outfit, knock on that rich lady in the bathrobe's doors, come on inside and..

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

    I actually cant believe id did not use this in my coding classes. ty sm!!

  • @nithinpajayan3006
    @nithinpajayan3006 3 ปีที่แล้ว

    You are the best. period.

  • @ΓιάννηςΤσίντζας
    @ΓιάννηςΤσίντζας 2 ปีที่แล้ว

    Helpful video, thanks 👍

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

    Thank you so clear and simple! Thank you

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

    Could you go into more detail in another video on the command line? Like how to make a command utility, run multiple programs using the command utility, and to pass args to a second prog?

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

    Oh, "ASCII to int". That's what that weird, mysterious `atoi` is supposed to mean! Thanks, never knew that! :D

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

    Very good and really helpful tutorial thanks a lot:)

  • @toddschmieder7873
    @toddschmieder7873 3 ปีที่แล้ว

    Holy cow! That was awesome! Thank you!

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

    thank you....grt explanation

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

    Coming from other languages, I tend to find the char *argv[] syntax a little more intuitive. Works the same. Just more explicit that you are dealing with an array of strings

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

    Great video👍

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

    Awesome helpful video! :)

  • @robertwitt1276
    @robertwitt1276 4 ปีที่แล้ว

    Awesome video thanks a ton man keep up the good work

  • @jayarajdavid
    @jayarajdavid 4 ปีที่แล้ว

    This helped me a lot! Thanks

  • @mow3890
    @mow3890 5 ปีที่แล้ว

    thanks, jacob

  • @user-uj5pc7dy2j
    @user-uj5pc7dy2j 4 ปีที่แล้ว

    Hello, I just found you through this video and, from a glance at your channel, I see that you make videos on just what I am passionate about: low-level languages, embedded systems and hardware... well, I *had to* subscribe!! :p haha. Anyway, thank you for sharing your knowledge! Greetings from Argentina.

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      Welcome! Glad you found the channel.

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

    Qwestian:
    - Say I have a UTF terminal in Linux (fug aple)
    - Say I pass an EMOJI as an arg
    - What do I get in my string?
    - Shouldn't all the characters be UTF rather than ASKYBOI?

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

      Unicode and UTF-8 are on my list of things to make future videos about. Short answer is you can store your characters as chars, and then interpret them as UTF-8, or you can convert them to wide characters. There are a few different ways to handle things. I'll try to give a better explanation in the future, but for now, try it out. Pass in the emoji in UTF-8 and then look at the bytes that are passed in (using a debugger). Your standard ASCII characters will all just be 1 byte. The emoji or other non-ASCII characters will be encoded with more bytes.

    • @SuperCape
      @SuperCape 5 ปีที่แล้ว

      @@JacobSorber so, I've arranged this piece of code:
      #include
      #include
      int main (int argc, char** argv){
      int i,j;
      for (i=1; i

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

      @@SuperCape You should look at the UTF-8 encoding. It uses a variable number of bytes per character. So, it is all UTF-8. The first 128 UTF characters just happen to be the same as the ASCII set.

  • @fontanot
    @fontanot 3 ปีที่แล้ว

    thank you, now i got it :)

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

    Okay, I got that so far, but how can I compare like argv[1] with a string?
    As abc in argv ist not "abc". Because '''if (argv[1] == "abc"){....}''' will not be true if I run: argv.exe abc
    EDIT: It is ofc because I need to do a string compare with strcmp()

  • @100taeh3
    @100taeh3 3 ปีที่แล้ว +2

    Hello sir! I'm taehee from South Korea! thanks for great video! There's one thing I'd like to know about argc and argv. It might be further question from this video content! But I'd like to know how main function get "what I type in on the command line"...! like how....! or like how it works!! Is there any video that I might wanna watch to answer this question? or can you explain this for me?? thanks sir!

    • @NateROCKS112
      @NateROCKS112 3 ปีที่แล้ว

      To my knowledge (not much), this is how it works.
      When the shell executes a program, it forks itself, and then it pushes argv[] and argc to the stack, in that order, and it allocates memory for each argument. Then it calls execve on the program name.
      So the stack looks, in reverse order, like "argc argv[0] argv[1] argv[2] ...". argv[0], for instance, is the memory address of the program name (in C, of type char*).

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

    Great video, could you made a video about how to use getopt() and getopt_long()?

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

      Sure. I'll add it to the list. Just need to find time. :)

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

      @@JacobSorber I got you covered: in glibc time() is declared inside the time.h header and defined inside time.c
      😎

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

      You're a life saver! Thank you.

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

    I’m the most literal definition of beginner there is. Im doing cs50 and am currently struggling big time. Im just having a terrible time actually getting the concepts. Im on this video because im working on the prescribed Caesar problem set that utilizes command line arguments. Im curious what the “[i]” means after argv in the printf statement and when to use it.

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

      Welcome and good luck. Don't let the details get overwhelming. argv is an array of strings (char *). argv[0] tells it to get the first string. argv[1] tells it to get the second string. argv[i] tells it to get the i-th string. I hope that helps.

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

      @@JacobSorber It does, but I'm not sure what you mean when you say i-th string. If I had to use my brain in a logical way, I'd say it represents values like tenth, eleventh, twelfth, etc. So anything after 0-9? Do I have that right?

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

      @@southmcwow Yeah. Say that i is a number (12), then argv[i] is argv[12], which is string #12. Just remember that array indexing starts at 0.

  • @Alexis_Lynna
    @Alexis_Lynna 4 ปีที่แล้ว

    What text editor are you using ? Also if you run a file with a text document piped in like “ ./a.out < file.txt “ do you have to open the file or can you simply loop through the file and store data as needed ?

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

      I think I was using VS Code for this one. I also occasionally use atom. And, if you pipe in a file, like that, you don't have to open it. Just read the contents from stdin.

  • @bikki60
    @bikki60 4 ปีที่แล้ว

    how can i use argv to trigger different methods e.g. "anapp dostuff" returns "doing stuff"

  • @activelivingchallenger4298
    @activelivingchallenger4298 5 ปีที่แล้ว

    How do i incorporate this with a make file? I'd like to do something like: make argName. Is there a way to get argv as a variable of some kind for the make file?

    • @JacobSorber
      @JacobSorber  5 ปีที่แล้ว

      You can do something like this.
      make target MYVARIABLE=value
      ...and then in the makefile, you can use $(MYVARIABLE). That's how I've seen it typically done. I don't think you can get the raw argv, but I could be wrong.

  • @tomasvalent4405
    @tomasvalent4405 4 ปีที่แล้ว

    Can I change value of argc or argv? I saw something like that in CodeBlocks, they add some numbers there and argc or argv can write them out. (I use dev cpp)

  • @hellNo116
    @hellNo116 4 ปีที่แล้ว

    i find it hilarious i forgot what that meant in less than a semester....

  • @arkadymagomedov1700
    @arkadymagomedov1700 3 ปีที่แล้ว

    getopt is in order

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      I agree. I'll see what I can do about that. 😀

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

    I.e. Linux -- never windowsz.

  • @jhongzhun1832
    @jhongzhun1832 3 ปีที่แล้ว

    this is too fast for me

  • @gnaneshwaar2594
    @gnaneshwaar2594 4 ปีที่แล้ว

    So basically, its a like comment.?

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

    too fast, lack of explanation

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

    it wasnt help full :(

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

    in my text book it is like this
    char argv* [ ]
    but you written like this, char **argv
    what's is the difference??