-4

I am reading compression library and encountered definition of main function as

int main(int argc, char *argv[])

and did not get why they use such type of definition. I am not a professional. Please provide an answer as simple as possible so that i can understand it easily.

sth
  • 222,467
  • 53
  • 283
  • 367
user3860949
  • 115
  • 1
  • 1
  • 10
  • Similar: http://stackoverflow.com/questions/4176326/arguments-to-main-in-c – chris Jul 21 '14 at 14:23
  • 3
    What else should they use?! – Kerrek SB Jul 21 '14 at 14:24
  • There are several forms of the `main()` function. This one is defined in such a way as to accommodate capturing and using command line arguments within the main function block. That is, argc will contain an integer indicating how many command line arguments were used to invoke the executable, `argv[]` will contain string representations of the arguments, with the name of the executable itself being in position 0 of the array. Other than that, _Its by definition_. Why is water wet, and comprised of H20?. – ryyker Jul 21 '14 at 14:29
  • The two are identical as far as generated code goes, the only real difference is that making it an array discourages people from attempting to change the pointer location and thus you should use array access (`argv[0]` vs `*argv`, `argv[1]` vs `*++argv`). This is not enforced though, it's exactly the same in effect, it just makes people think about it differently. – scragar Jul 21 '14 at 14:55
  • @scragar, I personally prefer the pointer notation because far too many people think a `T foo[N]` parameter is actually an array and that they can do things like `sizeof` on it. Might as well write what it is. – chris Jul 21 '14 at 15:24
  • `[OFF]`, but so funny to see how younger generation is confused about such simple set of arguments. :) – holex Jul 21 '14 at 16:26

2 Answers2

4

When a program is run from the command line, it may be passed command-line parameters:

For example:

wget --tries=10 -r http://stackoverflow.com

wget is the name of the program, and the command-line is encoded in the argc/argv arguments, so the program can change its behavior based on the parameters passed to it.

In this example, int argc will end up with count of all arguments, including the program name itself: 4

argv will be an array of each parameter separately:

argv[0] == wget
argv[1] == --tries=10
argv[2] == -r
argv[3] == http://stackoverflow.com
abelenky
  • 63,815
  • 23
  • 109
  • 159
1

There are several forms of the main() function. This one is defined in such a way as to accommodate capturing and using command line arguments within the main function block.

That is, argc will contain an integer value indicating how many command line arguments were used to invoke the executable. argv[] will contain string representations of the arguments, with the name of the executable itself being in position 0 of the array, if there are arguments (or switches) following the executable name, they will be contained in array positions matching the order in which they appear on the command line.

For example: for an executable named GetName.exe, and invoked with these arguments:

GetName.exe -t -s  

argc == 3
argv[0] == "GetName.exe"
argv[1] == "-t"
argv[2] == "-s"

Other than that, Its by definition. Why is water wet, and comprised of H2O?

EDIT to answer question in comments:

argc. argv as described provide a way for the executable to aquire, and use command line arguments. Here is a quick example of how that works:

Assuming the name of the executable GetName.exe, and again, called like:

GetName.exe -t -s    

And given the following code:

#include <stdio.h>

int main (int argc, char *argv[])
{
     int i=0;
     printf("\ncmdline args count=%s", argc);

    /* First argument is executable name only */
    printf("\nexe name=%s", argv[0]);

    for (i=1; i< argc; i++) 
    {
         printf("\narg%d=%s", i, argv[i]);
    }

    printf("\n");
    return 0;
 }

The output would be:

cmdline args count=3
 exe name=./GetName.exe
 arg1=-t
 arg2=-s
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • +1 for why is water wet and comprises H2O! – Am_I_Helpful Jul 21 '14 at 14:31
  • Now I wonder what an atom of Zeronium is. We've been lied to. – Quentin Jul 21 '14 at 14:33
  • Ok i get it that what will be the values of argc and argv[] but why this kind of provoking is used. What is the use of it. – user3860949 Jul 21 '14 at 14:45
  • @user3860949: It's used to give the program information at program startup without the program having to prompt for it. Think of a command like `ls -l`; the `-l` command line parameter tells `ls` to produce a long listing. Your compression library is most likely taking the name of the file to be compressed as one of the command line parameters. – John Bode Jul 21 '14 at 15:12
  • Will the down voter please comment on why? – ryyker Jul 21 '14 at 16:09
  • @user3860949 - See my edit to answer your question. John's comment is likely accurate for your specific scenario with compression routine. My example is generic but shows how the values stored using argc and argv can be used. – ryyker Jul 21 '14 at 16:19
  • @Quentin - _We've been lied to_? elaborate. – ryyker Jul 21 '14 at 16:23
  • @holex - Thanks for the edit, ( H2O ). I did not know how to do that :) – ryyker Jul 21 '14 at 16:27
  • Water never contained Oxygen. It was Zeronium, has always been !! What have we done... – Quentin Jul 21 '14 at 19:47
  • @Quentin - Are you referring to ***[THIS](http://en.wikipedia.org/wiki/Pluto_%28Astro_Boy%29)***. i.e. _a German robot detective named Gerhardt, made out of a unique material named "Zeronium"_...? – ryyker Jul 22 '14 at 14:48
  • @ryyker you're searching too far : I was just joking about the "H20" (with a zero instead of an O, hence "zeronium") from your original answer :) – Quentin Jul 22 '14 at 15:03
  • @Quentin - Lol. _zeronium_ was too intriguing not to look up though. – ryyker Jul 22 '14 at 15:12