command line arguments

This is a discussion on command line arguments within the C Programming forums, part of the General Programming Boards category; this is merely out of curiosity, i have read a couple of articles recently on the arguments that get parsed/passed(?) ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    23

    command line arguments

    this is merely out of curiosity,

    i have read a couple of articles recently on the arguments that get parsed/passed(?) by int main , and some of the situations where these arguments are filtered/checked and a relevant process is invoked....

    what i haven't been able to read anywhere, is how a app can send arguments to int main when when the application starts up or during application runtime, or if this is possible, or how these arguments arise in argc argv. from what i read, a file could be one of the arguments, how does this file land up there, say for instance i open and read a file in a program, does this automatically become one of the arguments, or is a file specifically run from a command line argument......

    i dont really understand this, but i hope my questions are clear enough to get the correct answers.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    The easiest way to pass arguments to a program is through the command line. Everything after the application's file name is passed as an argument to the application. For example,
    Code:
    $ foo bar1 bar2
    will invoke the "foo" program and pass it the arguments "bar1" and "bar2". These are in addition to "foo", which is how we invoked the program and is always the first argument supplied.

    Now you might be thinking that now all applications are started from the command line. To invoke an application from within another application and pass it arguments, one can use a member of the exec() family of functions.
    http://www.opengroup.org/onlinepubs/...ions/exec.html

    Hope that helps

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Command line arguments (or parameters) are simply strings. Whether these strings represent filenames or switches or whatever is up to you.

    From main()'s point of view, command line arguments are just arguments to the function main() itself, in the form of argc and argv. In other words, all of the arguments are supplied when the program is first started. You can't pass in parameters to a program at any time, only when the program is initially executed and main() is initially called.

    From a shell's point of view, you type strings to be executed. For example, I might type "test" into a shell (perhaps bash for Linux, or the standard Command Prompt for Windows). The shell should then execute the program "test". If I typed "test message", the program "test" would be executed, with one parameter: "message". "test this message" would result in two parameters, and so on.

    There's also an FAQ about this, though it's not very extensive. http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    23
    thanks for the explanations.

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. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  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, 01:11 PM

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