tags:

views:

178

answers:

8

Possible Duplicates:
What are the arguments to main() for?
What does int argc, char *argv[] mean?

Every program is starts with main(int argc, char *argv[]) definition . I don't understand what does it mean?I would be very glad if somebody could explain why we use these arguments if we dont use them in the program? Why not just : int main() ?

EDIT:

The name of the program is one of the elements of *argv[] and argc is the count of the number of arguments in *argv[]?What are the other arguments sent to *argv[]?How do we send them?

A: 

argc is the number of command line arguments and argv is array of strings representing command line arguments.

This gives you the option to react to the arguments passed to the program. If you are expecting none, you might as well use int main.

Aliostad
+6  A: 

with argc (argument count) and argv (argument vector) you can get the number and the values of passed arguments when your application has been launched.

This way you can use parameters (such as -version) when your application is started to act a different way.

But you can also use int main(void) as a prototype in C.

There is a third (less known and non standard) prototype with a third argument which is envp it contains environment variables.


Resources :

Colin Hebert
What does the name of the program have to do with the arguments passed to main()?
fahad
@Potatoswatter, @You, I updated my answer to explicitly say that envp isn't standard. @fahad, usually the first argument is the name of the application, this way you can know how your application has been called.
Colin Hebert
@colin:Just by its name,how would you know how it has been called?
fahad
@fahad, I meant "how the application had been named". This could for example be used to print an error message such as "Illegal option... usage: applicationName [-v]"
Colin Hebert
+1  A: 

argc means the number of argument that are passed to the program. char* argv[] are the passed arguments. argv[0] is always the program name itself. I'm not a 100% sure, but I think int main() is valid in C/C++.

kraftan
A: 

Those are for passing arguments to your program, for example from command line, when a program is invoked

$ gcc mysort.c -o mysort

$ mysort 2 8 9 1 4 5

Above, the program mysort is executed with some command line parameters. Inside main( int argc, char * argv[]), this would result in

Argument Count, argc = 7 

since there are 7 arguments (counting the program), and

Argument Vector, argv[] = { "mysort", "2", "8", "9", "1", "4", "5" };

Following is a complete example.

$ cat mysort.c
#include <stdio.h>
int main( int argc, char * argv [] ) {
    printf( "argc = %d\n", argc );
    for( int i = 0; i < argc; ++i ) {
        printf( "argv[ %d ] = %s\n", i, argv[ i ] );
    }
}

$ gcc mysort.c -o mysort

$ ./mysort 2 8 9 1 4 5
argc = 7
argv[ 0 ] = ./mysort
argv[ 1 ] = 2
argv[ 2 ] = 8
argv[ 3 ] = 9
argv[ 4 ] = 1
argv[ 5 ] = 4
argv[ 6 ] = 5

[The char strings "2", "8" etc. can be converted to number using some character to number conversion function, e.g. atol() (link)]

ArunSaha
A: 

argc is the number of command line arguments given to the program at runtime, and argv is an array of arrays of characters (rather, an array of C-strings) containing these arguments. If you know you're not going to need the command line arguments, you can declare your main at taking a void argument, instead:

int main(void) {
    /* ... */ 
}

Those are the only two prototypes defined for main as per the standards, but some compilers allow a return type of void as well. More on this on Wikipedia.

You
int main() {} is ok
Nikko
Technically, `int main()` is a function taking arbitrarily many arguments, while `int main(void)` takes exactly zero arguments, so the latter is more correct.
You
@You: no, in a *definition*, `int main()` is a function taking no parameters. In a *declaration* which is not a definition, it's a function taking unspecified parameters. This is 6.7.5.3/14 in n1256, or 6.5.5.3/10 in n794. The questioner is asking about a definition.
Steve Jessop
A: 

You can run your application with parameters such as app -something -somethingelse. int argc represents number of these parameters and char *argv[] is an array with actual parameters being passed into your application. This way you can work with them inside of your application.

Ondrej Slinták
A: 

The comp.lang.c FAQ deals with the question

"What's the correct declaration of main()?"
in Question 11.12a.

cschol
...but this isn't comp.lang.c, it's StackOverflow, a community where we *help* people by *answering* their questions, not redirecting them to manuals, FAQs, or lmgtfy links.
Mark E
The question was already answered above. The C FAQ, however is still worth reading, imho. But it states in its license that it cannot be reproduced without permission. Therefore the links. Just for completeness sake.
cschol
+1  A: 

This is related to the good ool terminal, where you write cryptic things like:

cat file

Here the word cat is a program that takes a file and outputs it somewhere.

From the beginning of C one just wrote

main()
{   /* code */
}

Now one writes something like

int // Specifies that type of variable the function returns, main should always return int
main ( int arc, char **argv )
{    // argc tells how many arguments the program recieved, which is always 1 or more
     // the first argument tells you what your program is named. The rest of the arguments
     // may be very program-specific.
     return 0; // Indicates that everything vent well.
}

Sometimes there is even a third environment-variable after argv, but I'm not sure how that works.

Frank