Thread: command line arguments

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    command line arguments

    Halo all,
    I started reading command line arguments from K&R. But frankly speaking I didn't get even a single word of it. Forget about command line arguments, What is a "command line"? Then I started going through some tutorials over the net on this topic but still much much confused about what they are?
    Where they are useful? After that I saw a code in some site
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int i;
    	printf("Program name: %s\n", argv[0]);
      
      for(i = 1 ; i<argc ; i++)
        printf("\nArgument %d: %s", i, argv[i]);
      return 0;
    }
    The output gives me the path of my file.
    I commented out the following lines and the output was unchanged
    Code:
     for(i = 1 ; i<argc ; i++)
        printf("\nArgument %d: %s", i, argv[i]);
    Then what's the use of these lines more specifically of argv[i].
    And yes please tell me without any reference to files because I haven't studied them yet. Also if possible please show me a code where command line arguments are easy to understand.
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You know that thing, on your start menu, that says "command prompt"? That's you command line interface, where you type things and the computer does it.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok. Then why are argc and argv called command line arguments? What relation do they have with the command line?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, have you ever used a command before? Like say "copy thisfile thatfile", or "del *.*" or whatever. Those things that you type after the command are the command line arguments. argc is the count of the number of arguments, while argv is the array of the arguments themselves.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    If you work with a CLI based OS you would know that "command line" thingy. argc is the number of arguments on the command line while argv is an array of pointers to each of them.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    e.g. when you say "notepad textfile.txt" you start notepad and pass it _two_ arguments: it's name and "testfile.txt" (argv[0] and 1, respectively). Argc is that number, in this case two.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You know that thing, on your start menu, that says "command prompt"? That's you command line interface, where you type things and the computer does it.
    Please tell me that VS does not allow you to start your program without typing it's name or interacting with the shell at all. If so, is this considered a "feature"? Hmmm....

    MS should engineer "Windows 8" to work without a keyboard, then we won't have all these problems.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    Please tell me that VS does not allow you to start your program without typing it's name or interacting with the shell at all. If so, is this considered a "feature"? Hmmm....
    Of course it does. Just click the Run or Debug button and it runs the program (just like Eclipse and many other IDE's). You can also go into the properties and tell it which command line arguments to pass when you run it from the IDE.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cpjust View Post
    Of course it does. Just click the Run or Debug button and it runs the program (just like Eclipse and many other IDE's). You can also go into the properties and tell it which command line arguments to pass when you run it from the IDE.
    That's insane. That's literally devolution. It's not like it could have gotten any easier than it already was. Presuming a command line history, it is only a couple of keystrokes to recompile/remake and re-run in a text terminal. I cannot imagine having to select a menu in order to add arguments. Even further, I cannot imagine the person(s) who implemented this and passed it off to others as something worthwhile.

    I like popcorn, but I loathe microwave popcorn (if they served you microwave popcorn in a theater, you would complain). It is also much less expensive. I find it mind boggling in the grocery store when I have to hunt thru four shelves of overpackaged microwave crap to find the one or two brands of real popcorn still available. You cook it in hot oil, it does not even leave any mess in the pot. It is about as hard as making toast, eg you *can* burn it. But I have a friend whose kids literally did not know what I was doing when I made popcorn on the stove one night.

    This is the same thing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    That's insane. That's literally devolution. It's not like it could have gotten any easier than it already was. Presuming a command line history, it is only a couple of keystrokes to recompile/remake and re-run in a text terminal. I cannot imagine having to select a menu in order to add arguments. Even further, I cannot imagine the person(s) who implemented this and passed it off to others as something worthwhile.
    Your rant reminds me of Elysia, but as an antithesis. If you disagree with this particular type of integration, then do not use such a feature (or misfeature, as the case may be). After all, different people have different working styles, and perhaps your style simply does not match.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    and perhaps your style simply does not match.
    In the case of Elysia vs. MK27, yes, style simply does not match. In the case of BEN10, it's a feature that has allowed a programmer to write a program without having any idea what's going on under the surface. (edit: no offense to BEN10 - I think the situation is the problem - not you)

    Where I work I'm usually able to solve problems faster just because I do everything manually with a text editor and the command-line. So if something breaks, I already understand how it works and what went wrong. I don't have to rely on magic for things to work. C is a language in which you cannot afford to rely on magic - especially not as a beginner.

    edit: Incidentally, BEN10 - the effect of command-line arguments can still be seen in a GUI shell - when you drag a file and drop it on an executable, that executable is automatically started and the file is opened. Try it with a text file and a shortcut to notepad. Or with Microsoft Word. That's probably done through command line arguments in the background.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sean View Post
    In the case of BEN10, it's a feature that has allowed a programmer to write a program without having any idea what's going on under the surface.
    Without wanting to be mean to BEN10, that was kind of my point. I don't think these IDE's are good for beginners. There seems to be a lot of questions about getting them to work correctly, and a lot of basic reality being avoided in the process. Surely it would be better to learn about that reality than learn about your IDE?

    edit: Incidentally, BEN10 - the effect of command-line arguments can still be seen in a GUI shell - when you drag a file and drop it on an executable, that executable is automatically started and the file is opened. Try it with a text file and a shortcut to notepad. Or with Microsoft Word. That's probably done through command line arguments in the background.
    That is definitely how your web browser accepts requests from other applications. Normal drag n' drop (into a running app) may not work that way (it does not in GTK) but in the case of starting the app fresh, you're probably right.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think it depends how your brain is setup. Some people have really great memories for cryptic text commands and they generally use UNIX command line stuff; others (like me) don't have photographic memories when it comes to compressed text commands with all the vowels missing... and buttons & menus are so much easier for those people to use.

    That's exactly why Windows completely dominates the PC industry -- You can start using it without even knowing anything about a computer, whereas UNIX (non-GUI) or DOS you need to learn a lot of commands before you can do anything with it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cpjust View Post
    I think it depends how your brain is setup. Some people have really great memories for cryptic text commands
    I believe in nurture over nature here, altho no doubt there are things like born geniuses and born fools. You are, or want to be, a programmer, so you might as well get to liking "cryptic text".

    I agree with what you are saying about Windows. Not everyone who drives has to touch an engine. BUT if you are, then you should be willing to get your hands dirty.

    That doesn't mean no one should ever use an IDE, but I will say it again more bluntly: they are obviously bad for beginners. I am sure this is all something that happened rapidly in the last decade. If you were doing a CS degree in 1995, or even just tooling around as a hobbyist, you would sure as heck know what "the command line" is before you even started. Anything else would have been impossible.

    I'd commend BEN10 for exposing this one...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Without wanting to be mean to BEN10, that was kind of my point. I don't think these IDE's are good for beginners. There seems to be a lot of questions about getting them to work correctly, and a lot of basic reality being avoided in the process. Surely it would be better to learn about that reality than learn about your IDE?
    Ah, then you should have said as much instead of ranting about the IDE feature itself

    I think that it would be good to learn about both (and not just the IDE, but tools in general). I do not think that it is exposure to an IDE that is at fault here. It is lack of exposure to direct usage of the command line that is at fault here.

    The point is that since the programs being written are meant to be run from the command line, the programmer should know about the command line, just as if you intend to write a GUI program, you should know something about the given GUI environment. A GUI IDE could keep a beginner ignorant about the command line, but rote usage of build tools run on the command line could do the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM