EmbLogic's Blog

Significance of main(int argc, char *argv[]) ?

What is the significance of int argc & char *argv[] ? Where and why is it used ?

One Response to Significance of main(int argc, char *argv[]) ?

  1. m.siddarth says:

    Before the modern graphical interface, there was the command-line interface. DOS and Unix are examples. The command line is the line you type to run your program in a command-line environment. Suppose you have a program in a file named hello. Then the command line to run it might look like this in Unix:
    $ hello
    Or it might look like this in a Windows command-line mode, such as XP’s Command Prompt:
    C> hello
    Command-line arguments are additional items on the same line. Here’s an example:

    % hello -r Ginger

    C compilers allow main() to have no arguments or else to have two arguments. (Some implementations allow additional arguments) With two arguments, the first argument is the number of strings in the command line. By tradition (but not by necessity), this int argument is called argc for argument count. The system uses spaces to tell when one string ends and the next begins.
    The second argument is an array of pointers to strings. Each string on the command line is stored in memory and has a pointer assigned to point to it. By convention, this array of pointers is called argv, for argument values. When possible (some operating systems don’t allow this), argv[0] is assigned the name of the program itself. Then argv[1] is assigned the first following string, and so on.

Leave a Reply to m.siddarth Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>