Thread: Where exactly IS the "command line"? And where are the arguments?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Where exactly IS the "command line"? And where are the arguments?

    I'm sure this is a simple one but I can't quite get my head around the explanation in my textbook, nor the one on CProgramming here.

    I understand that argv[0] is the name of the program being called and this is obviously stored as such (i.e. it is the name you save the program as).

    But Argv[1] and upwards - they are stored within the code of that program, yes?
    Do they exist somewhere specifically in the code of the program whose name is argv[0]?

    Say argv[0] (the name of the program I want to call) is "test1"
    What could the code inside that program look like, just for example? Does it have
    Code:
    #include<stdio.h>
    and then also have
    Code:
    main()
    {
    ?

    Just not sure where the character strings for argv[1], argv[2], etc actually originally appear.


    And is the "command line" something that you put before
    Code:
    main()
    ...or IS that the command line right there?

    Thanks a lot
    Last edited by RoDC; 05-18-2011 at 03:46 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Do you use a terminal program? If so, the prompt is the command line, where you enter commands. So if you run your program with an argument like this:

    Code:
    ./yourprogram hello world
    argv[1] will be the string "hello"
    argv[2] will be "world"

    So this is a method you can use to make arguments accessible to your program.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Thanks a lot.

    I think I must still not fully understand though.

    From your example above:
    Am I typing in the arguments myself each time I want to execute a program that uses
    Code:
    main(int argc, char *argv[] )
    ?

    or are the arguments already stored somewhere like I presumed?

    Are the program's arguments just anything at all that gets stored in memory because of lines of code within that program? i.e. variables declared and initialised, strings read in from the user, etc?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by RoDC View Post
    Am I typing in the arguments myself each time I want to execute a program that uses
    Yes, that is the point of them. It's a mechanism provided to give you the ability to add arguments to your program.

    You can try this for example.

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int i;
    
        for(i = 0; i < argc; i++) {
            printf("argv[%d]: %s\n", i, argv[i]);
        }
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    Edit: Only just now saw your latest post. I'll go have another ponder on this information now anyway. I should have enough to help me understand. Thanks for the help.
    Last edited by RoDC; 05-18-2011 at 04:25 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    When you create an application there is some additional startup code that automatically gets added to your final executable program that processes the command line arguments that are passed into your program when you run it. The arguments do not exist hard-coded in your code anywhere. When the program is executed, the startup code is executed first and takes the arguments that were passed into it. Then, the startup code calls your program's main function and passes those arguments into main (along with a count of how many it was able to process). The typical convention within the main function names these variables as argv/argc though they can be given other names.
    Last edited by hk_mp5kpdw; 05-18-2011 at 05:57 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RoDC View Post
    Edit: Only just now saw your latest post. I'll go have another ponder on this information now anyway. I should have enough to help me understand. Thanks for the help.
    Ok, along with the other good informaiton you've been given...

    When you type a command line like "MyProgram.exe -flag1 -flag2 -flag3 "... the entire command line is copied by the operating system to the startup record of the program. By convention (through startup code, different for each OS) the program knows where to access this command line. The C startup code, which is executed before main() then tokenizes the command line and sets pointers into argv[] to each element. argc is loaded with a count of the elements.

    Inside the program argc tells you how many arguments there are and argv[] lets you access them as strings...

    You can then do things like fopen(argv[2],"r"); treating the variables just like ordinary C strings.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Arguments accessible from the main's argv were not necessarily from the command line. If the program was executed from within another program, using function "spawn" in the case if C, then the arguments are provided by the calling program. "Command line" is historical but has become generic speak for whatever executes the program.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Arguments accessible from the main's argv were not necessarily from the command line. If the program was executed from within another program, using function "spawn" in the case if C, then the arguments are provided by the calling program. "Command line" is historical but has become generic speak for whatever executes the program.
    Ok... it's "Invocation Multi-Element String" ... how's that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print name "x" times using command line arguments
    By zekiegypt in forum C Programming
    Replies: 3
    Last Post: 04-20-2011, 11:32 AM
  2. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  3. Multiple arguments in the "if" command
    By |Wiz| in forum C++ Programming
    Replies: 11
    Last Post: 09-28-2005, 12:01 PM
  4. Replies: 3
    Last Post: 06-01-2004, 04:29 PM
  5. command line "shell"
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 01:29 AM