command line arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maddy dcosta
    New Member
    • Aug 2007
    • 6

    command line arguments

    how to pass the command line arguments in c
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by maddy dcosta
    how to pass the command line arguments in c
    U shuld do this using argc and argv.
    u will get a clear idea about this if u study any C book.

    Raghuram

    Comment

    • maddy dcosta
      New Member
      • Aug 2007
      • 6

      #3
      need of command line arguments

      hello.....

      I am little bit confused with the implementation of command line argument. Actually i want to know why they have invented this feature... Is there is any real reason to implement this... There r other handy features available why they gone with it........... This is not dealing with any C,C++ or unix... I want to know generally...... .....

      Plaese help me guys...........

      Comment

      • DarkArtanis
        New Member
        • Aug 2007
        • 3

        #4
        On C, you can pass command line using something like this:

        Code:
        int
        main(int argc, char * argv[])
        {
        int i;
        
        printf("The arguments of %s are %d, and they are:\n",argv[0],argc-1);
        
        for (i = 0 ;  argv[i] != NULL ; i++ )
        
        printf("%d: %s\n",i,argv[i]);
        
        return 0;
        }
        on argc are strored the amount of argmuments of the program, and on argv[], the pointers to the strings that are the arguments. By deafult, the name of the program is recibed in the first place like an argument, and the array of pointers to the arguments is finished by a NULL.

        you can use pepe and jose instead of argc and argv, they are used with that name just by convention.

        Try to compile it and you will see.

        Yours,
        DarkArtanis

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by maddy dcosta
          hello.....

          I am little bit confused with the implementation of command line argument. Actually i want to know why they have invented this feature... Is there is any real reason to implement this... There r other handy features available why they gone with it........... This is not dealing with any C,C++ or unix... I want to know generally...... .....

          Plaese help me guys...........
          You obviously are a post-unix person. Compare those command line arguments
          and the command itself as the parameters to a function and the function.

          Together with IO (and their redirection) and the piping of commands they are
          the building blocks of zillions of useful applications and services all over the world.

          GUIs are just there for human beings and human beings are proven unreliable
          when it comes to consistency so we won't let them start a second process when
          the first one has finished and we certainly don't trust them to carry over results
          from one process to the other ;-)

          kind regards,

          Jos

          Comment

          • kalar
            New Member
            • Aug 2007
            • 82

            #6
            Some informations you can see here http://publications.gbdirect.co.uk/c...s_to_main.html

            We use command line arguments for many reasons.One example is to pass the name of a file into the programm.As an example we type(in LInux) : [PHP]./progr scores.txt alive.txt[/PHP]

            progr : is the name of the programm
            scores.txt and alive.txt : are the two arguments we pass to the programm.In this example scores.txt and alive.txt are the names of two files we want to process

            argc : is argument counter (bigger than 1)
            argv : argument vector

            argv[0] : is always the name of programm
            for this reason argc must be bigger than 1
            In this example:
            argv[1] : is the scores.txt
            arg[v2] : is the alive.txt

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by maddy dcosta
              I am little bit confused with the implementation of command line argument. Actually i want to know why they have invented this feature...
              That was done in 1972. The smallest computer was the size of a two-car garage and only had a console.

              ANS continues to require console support.

              Consoles had all but disappeared but there remains a cult following.

              Comment

              Working...