Thread: Help with understanding arguments of int main()

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    Help with understanding arguments of int main()

    This is really a noobish question I should already know the answer too since I've been programming for awhile, but what do the arguments in the main() function do? Such as:


    Code:
    int main(int argc, char* args[])
    I know "int argc" is the argument count, keeping track of all the arguments passed in the program from the command line. But even after reading some tutorials on this website and other articles online, I'm still lost to what its purpose is. Plus what exact "arguments" is it keeping track of and what does it do with them. Would some of these functions be considered an argument, argc would keep track of:
    Code:
    cout <<"Hello world!";
    Or,

    Code:
    cin.get();
    Or even,

    Code:
    return 1;


    Also, what is "args"? I came across it while learning SDL. I know there is something called "argv" which I came across from the tutorial here on this website, which states "argv" as being the name of the program, or an empty string if the name is not known. What do both of these definitions mean?

    And what is the purpose of the "char*" next to args? I thought it was making "char" read "args" as a pointer? Also, what is the [] meaning at the end of args, or seen in alot of other programs, "argv". I'm asking because I know you could change "char*" to "char**" and leave out the [] next to the second argument. So what is happening there?


    Sorry for all these long-winded questions. I am just lost in all of this. In the past everytime I have had a question in programming, the community of cprogramming.com have ALWAYS been helpful so any comments will always be GREATLY appreciated!

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Let's pretend your program is called main.exe, this is a console program (meaning there is no gui, only text in/out-put in the console.)

    If you run your program from the console like this: "main.exe", then argv[0] will just be "main.exe" and argc will be zero.

    If you pass parameters to your program like this: "main.exe myParameter1 myParameter2" then argv[0] will still be "main.exe", argv[1] will be "myParameter1" and argv[2] will be "myParameter2", argc will be 2.

    Parameters are just a way for your user to pass certain information along when starting the program.

    also, char *argv[] is functionally similar to char argv[][]
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    It means that you can pass an array of arguments into the program, the argc as you say would be the count, argv will contain the value(s) , e.g filename / string as instruction name passed in, and is char* because it will be sized at runtime and contain the distinct 'chunks' of your argument values, which it is then your responsibility to handle in your program code.

    It lets you as you say write a command line version of your program, like in windows:

    myprog.exe /alertsOff
    myprog.exe /mystartfile.txt

    So your program starts in the first instance with all alert popups disabled
    In the second instance it will accept the file as input data for startup.

    You can drag and drop a file onto your exe and it will take the filename as an arg for example, it is also how you get your program to open when you double click a file associated with it. But that depends on you having the OS recognise your filetype and association etc.

    But, you can drag and drop files on your exe all day long, pass parameters in etc, if there is no code you have written to handle the input then nothing wil happen, your program will just start.

    If you have any experience with say UNIX then it is like when you write:
    Code:
    ls *EX* *SCR foo bar*
    the command takes what you wrote as parameters referenced internally as $1 $2 etc, as pointed out above $0 is reserved for the prog name if i rememeber

    I imagine other more advanced members can add a lot to what i have said
    Last edited by rogster001; 03-21-2012 at 03:14 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The names of the parameters to main are arbitrary, but the conventional names are argc and argv (argument counter and argument vector).

    In C generally, a function argument declared as char x[] is the same as char* x, since arrays are passed as pointers.

    argv is an array of character pointers. Each element of argv is a pointer to a zero-terminated array of characters (a standard C string). The last argument of argv is a NULL pointer.

    Neo1 made a mistake when he said argc would be 0 if the program was called without command-line parameters. It would actually be 1 since the program name (or possibly an empty string) is still passed as element 0 of argv.

    Play around with this program, passing 0 or more args from the command line.
    Code:
    #include <iostream>
    int main(int argc, char* argv[]) {
        int i;
        std::cout << "argc= " << argc << "\n";
        for (i = 0; i < argc; i++)
            std::cout << i << ": " << argv[i] << "\n";
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Thank you so much every one! You have really helped me understand the concept. =)

    But I must ask, what use is this for any program? Because I am only able to actually utilize all the programs that are used as examples for argument passing, such as the one oogabooga gave me (p.s. thank you oogabooga for providing me with one) in his previous comment through command prompt. Can anyone give me an example of a program, such as a game or any other program with an actual use with
    Code:
     int main( int argc, char* argv[])
    with a description of what the program does and give evidence for the use of
    Code:
     int main( int argc, char* argv[])
    to of made it included in the program.

    Thanks in advance!!
    Last edited by cplusplusnoob; 03-21-2012 at 05:42 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    NEVER MIND GUYS, I FOUND A PROGRAM! But I still need someone to tell me what the use of
    Code:
     int main(int argc, char* args[])
    does in this program I'm going to post below:


    Code:
    #include <SDL.h> 
    int main( int argc, char* args[])
    {
        SDL_Surface* message = NULL;
        SDL_Surface* screen = NULL;
        SDL_Init(SDL_INIT_EVERYTHING);
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
        message = SDL_LoadBMP("hello.bmp");
        SDL_BlitSurface(message, NULL, screen, NULL);
        SDL_Flip(screen);
        SDL_Delay(2000);
        SDL_FreeSurface(message);
        SDL_Quit;
        return 0;
    }

    So basically what this program does is display a bmp.file. So is that why we add the two arguments in the int main function? Because it has to have that to use another file? If so, than now I definently understand it haha.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by cplusplusnoob View Post
    Can anyone give me an example of a program, such as a game or any other program with an actual use with
    Code:
     int main( int argc, char* argv[])
    with a description of what the program does and give evidence for the use of
    Code:
     int main( int argc, char* argv[])
    to of made it included in the program.

    Thanks in advance!!
    There are numerous examples. If a command line interface is documented for a program, then you could imagine the argv and argc parameters being used in some way (even if it's not written in C++, just think of it if you were porting the program to C++).

    Internet Explorer Command-Line Options

    Other examples include:
    Foobar2000:Commandline Guide - Hydrogenaudio Knowledgebase
    7-Zip
    Winamp Application Programming Interface - Winamp Forums

    Pretty much any program can be invoked if you know its name, and programs that have a GUI may not be as well documented as things that do not have one.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No, that program makes no use of the arguments passed; therefore it's a poor example. It could just as well have been written with int main(void).

    Nearly any command line program will use command line arguments, like pretty much every tool in the UNIX/Linux toolset.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Neo1 View Post
    If you run your program from the console like this: "main.exe", then argv[0] will just be "main.exe" and argc will be zero.
    incorrect. argc will be 1 in this case.

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elkvis View Post
    incorrect. argc will be 1 in this case.
    Right you are, it's been a while since i've had to use command-line parameters.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Quote Originally Posted by rags_to_riches View Post
    No, that program makes no use of the arguments passed; therefore it's a poor example.
    Which program are you talking about? The one I put up? It MUST use
    Code:
     int main(int argc, char* args[])
    because I tried using just
    Code:
     int main()
    and
    Code:
     int main(void)
    just to see if anything different would happen and the programmed stop working afterwards until I put back the arguments in the main function.... So it has to use it somewhere in the program??

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you say it stopped working, what do you mean? If you got compiler errors related to argc or args, then you certainly do need the parameter list version of main regardless of what we see. To us it looks like the parameters aren't being used at all.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Quote Originally Posted by whiteflags View Post
    When you say it stopped working, what do you mean? If you got compiler errors related to argc or args, then you certainly do need the parameter list version of main regardless of what we see. To us it looks like the parameters aren't being used at all.

    Well, the compilier recieves an error, but it doesn't highlight the error, as if you had forgotten a semi-colon. It say's in blue text that the statement is a reference and not a call, to functoin "SDL_Quit". And if I delete "SDL_Quit"(I just did that to experiment, the program needs SDL_Quit to clean up), and still use just int main() or int main(void), it says "undefined reference to SDL_main...

    So this is what confuses me and paremeters?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Try calling SDL_Quit correctly like in this example (SDL_Quit - SDL Documentation Wiki) and it will fix the error.

    This has nothing to do with command line arguments after all.

  15. #15
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by whiteflags View Post
    Try calling SDL_Quit correctly like in this example (SDL_Quit - SDL Documentation Wiki) and it will fix the error.

    This has nothing to do with command line arguments after all.

    Actually, i believe it does. SDL requires the main function to include argc and argv, for it's own main function SDL_Main.

    So even though the sample program that cplusplusnoob posted doesn't use the arguments, SDL still needs them there.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using arguments in main
    By marlaFC in forum C Programming
    Replies: 10
    Last Post: 12-17-2011, 09:27 PM
  2. Arguments to main
    By codecaine_21 in forum C Programming
    Replies: 17
    Last Post: 10-23-2010, 12:49 PM
  3. arguments to main
    By cantinero74 in forum C Programming
    Replies: 4
    Last Post: 05-27-2010, 10:39 PM
  4. arguments in main
    By cdonlan in forum Linux Programming
    Replies: 3
    Last Post: 01-24-2005, 12:59 AM
  5. Main() arguments
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2003, 09:35 AM