Arguments to main

This is a discussion on Arguments to main within the C Programming forums, part of the General Programming Boards category; I am learning how to pass arguments to main and it should not be hard to learn! But if someone ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    63

    Arguments to main

    I am learning how to pass arguments to main and it should not be hard to learn! But if someone could explain why it is used, what its for. i do not get the whole concept. The more/better i understand what i am learning the better! Thanks in advanced!
    Last edited by codecaine_21; 10-19-2010 at 09:19 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The arguments to main come from the command line you enter to run the program...

    If, for example, you had a program called weather and entered the command line:

    weather Monday Rain Light

    Inside weather main would receive...

    argc = 3;
    argv[1] = Monday
    argv[2] = Rain
    argv[3] = Light

    It's a way of passing options and startup info into a program.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    Still a tad bit confused, sorry! Why am i passing arguments to main anyway? If you may provide a more elaborate definition/explination. And may you give me a working example to run so i can see it work?
    Last edited by codecaine_21; 10-19-2010 at 09:46 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Programs need parameters passed to them, just like our functions do sometimes. This is how it's done.

    What else is there to say about it? Google and read up, maybe?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    i have googled and i have two books on C programming. that is why i am asking for insight and help, because i still do not understand. I try and learn on my own and if i need help i ask. that is what i am doing now.
    Last edited by codecaine_21; 10-19-2010 at 09:53 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codecaine_21 View Post
    Still a tad bit confused, sorry! Why am i passing arguments to main anyway? If you may provide a more elaborate definition/explination. And may you give me a working example to run so i can see it work?
    Well... you're learning C... here's a little project for you...

    Write a program that accepts and displays command line arguments.

    Some examples where command line arguments are extremely helpful can be found by opening a windows CMD shell (Win+r -> CMD -> ok) then type help. Then type help command for each of the programs listed... The whole thing is 100% driven by command line arguments.

    Linux is the same only bigger... just about everything on Linux is CLA driven.

    Why would you use CLA in your own programs... well that's entirely a matter of what you are trying to accomplish. One of the most common uses is to pass in a filename to be opened when the program starts. Every windows program needs this feature.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's what I'm getting at -- there isn't much "insight" on this matter, to share. *Argv[] is how parameters are passed to main().

    Just like an *array[] is passed to a function.

    Read your books chapter on this, and work the exercises. You'll get it. Zee insight barrel, she is emp-ty.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    Thank you CommonTater. And Adak, i didnt understand why it was used. so i asked!

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Code:
    gcc
    gcc: no input files
    Code:
    grep
    Usage: grep [OPTION]... PATTERN [FILE]...
    Try `grep --help' for more information.
    Code:
    cp
    cp: missing file operand
    Try `cp --help' for more information.
    Code:
    sudo
    usage: sudo -h | -K | -k | -L | -V
    usage: sudo -v [-AknS] [-p prompt]
    usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u
                username|#uid] [-g groupname|#gid] [command]
    usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u
                username|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
    usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u
                username|#uid] file ...
    ...you now see the value in passing arguments to programs.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    That was a very nice break down and helps a whole lot
    QuadraticFighte!!!!! So when i pass an argument(s) like lets say sudo apt-get install. apt-get is an argument, install and sudo also. So when i write the code do i make or need a function for the first second and third arg??

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codecaine_21 View Post
    That was a very nice break down and helps a whole lot
    QuadraticFighte!!!!! So when i pass an argument(s) like lets say sudo apt-get install. apt-get is an argument, install and sudo also. So when i write the code do i make or need a function for the first second and third arg??
    Yes, you will have to write internal program code do deal with what comes in from the command line. C is pretty clever, but it's not a mind reader.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by codecaine_21 View Post
    That was a very nice break down and helps a whole lot
    QuadraticFighte!!!!! So when i pass an argument(s) like lets say sudo apt-get install. apt-get is an argument, install and sudo also. So when i write the code do i make or need a function for the first second and third arg??
    Well ya, if you want your program to process command line arguments, you need to program it to handle such arguments.

    In your specific example, sudo is a program that runs other programs with root privileges, and the argument is the program to run with ITS own arguments: "apt-get install". Sudo just prompts for a password, and then, if we enter the right one, it executes apt-get install. Finally, install is an argument to apt-get.

    If you have a program foo, you can pass it arguments, as in
    Code:
    $foo Hello World
    So in your main function:
    Code:
    int main(int argc, char** argv)
    The values of argc and argv are:
    Code:
    argc = 3
    argv[0] = "foo"
    argv[1] = "Hello"
    argv[2] = "World"

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The zeroth argument doesn't have to be the program name so don't depend on that, though. What you can depend on argv looking like is having your arguments in subscripts 1..argc - 1, and then argv[argc] contains NULL.

    I'd like to see a precipitous decline in use of CLI in general, as well.
    Last edited by whiteflags; 10-23-2010 at 12:58 AM.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by whiteflags View Post
    I'd like to see a precipitous decline in use of CLI in general, as well.
    That will only happen when it becomes easier to develop and verify a workable GUI to achieve <any specified function you care to name> than it is to develop a workable command line interface to do the equivalent.

    In other words, possibly never.

    Effectively GUI development sets the bar pretty high in terms of various qualities (ease of use, aesthetics, etc). Developing a CLI does not require meeting those quality constraints. Quality constraints are much harder - and subjective - to meet well.
    Right 98% of the time, and don't care about the other 3%.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    I'd like to see a precipitous decline in use of CLI in general, as well.
    That would end file associations... sending all these point and click file browsers back to the stone age.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many arguments to function int main?
    By plain in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2006, 06:01 PM
  2. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  3. Passing arguments to main?
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2003, 03:59 AM
  4. arguments passed to main?!?
    By threahdead in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2003, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21