Is it compulsory to write int main(int argc, char *argv[]), can't use some other names instead of argc and *argv[]?

I am having some real trouble around here giving command line arguments to Visual Studio in the project options itself (in 'Configuration Properties' -> 'Debugging').

I wrote this "int main(int argc)" after the "int main(int main_i)" did not work. In the MS visual studio property pages' Command line argument for the project I am not sure what I should write. I only want to pass a single integer to designate whether a file is to read or written to thus a mere int argc. I do not need the char *argv[]. I have tried a few values in the command line argument text box but it is not reaching the exe file when it executes, it shows what I did not enter at all.

Could you just provide me with a single simple example of what I need to enter in the MSVS C++ Property Pages' Command Line Argument space? I simply can't find any example on how to give the command line arguments to MSVS.

I also do not understand WHY I need to rebuild the whole project even when I just modified the command line argument value only??

Answers


If you want your program to understand a single integer command line argument, you still have to use argc and argv. Something like this ought to do it:

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

    if (argc < 2) {
        fprintf(stderr, "Need a command line argument.\n");
        exit(1);
    }

    int arg = atoi(argv[1]);
    // now you have an integer
    // do whatever with it...
}

The C standard requires that the main function have specific sets of parameters. One of the allowed combinations is (int, char *[]). You can name the parameters whatever you want, but they're almost always called argc and argv.


The names are just a well-established convention, you can use any names you want (though it might raise some eyebrows), however, I have only seen code that has used both arguments in the specified order.

In the pre-ANSI days it was possible to simply have int main(), and it still is .. but these days argc argv are usually supplied.

You can't just supply one of the parameters.


Variables names are only for the compiler. They will be converted to addresses in the binary image. You can pass upto 3 parameters for main.

Here is what I tried -

int main(int i, char* a[], char* e[])

As you can see the names are not the normal names. The first parameter is the number of command line arguments, the second is the actual array of arguments and the third is an array of environment variables.

In fact, main can take any number of arguments, but the rest, after the third will be invalid. This would still compile -

int main(int i, char* a[], char* e[], char* j[])

And just for fun, you can even do this -

int main(int i, float a, float b, float c, float d, float e)

Even a simple main is allowed, and still you can access those arguments.

int main()
{
    int nArgCount = __argc;
    char** pArgs= __argv;

    printf("Argument Count: %d\n", nArgCount-1);
    printf("Arguments:");

    for(int nIndex = 1; nIndex<nArgCount; ++nIndex)
        printf("%s, ", pArgs[nIndex]);
}

You can also use GetCommandLine function to access the command line. The important aspect of not having/using the main's argument is you can still access command line arguments in any function.


Like others mentioned, it is just a matter of convenience and that Kernighan and Ritchie used those arguments in the first C version it has been carried along that way.

You are allowed to freely use anything.

Here's a simple example:

#include <iostream>

int main(int arglen, char *args[])
{
        std::cout << "Running: " << *args << std::endl;
    while (--arglen > 0)
        std::cout << *++args << std::endl;
    return 0;
}

I use Visual C++ 2010 Express. If you do, you can press Alt+F7 to bring the project properties, go to Configuration Properties > Debugging and type your command line arguments in Command Arguments.

That's it.


You have to use char *argv[], if you want to read the values of command line arguments. Else you can drop it.

If you just want to have a single integer to differentiate between two things, then you have one method to do this. Just pass command line argument for one thing and don't pass any for other. Now you don't need char *argv[]. Now you pass command line argument for only one thing then the value of argc is 2, i.e., executable's name + command line argument.

And if you don't pass command line argument, then its value is only 1, i.e. executable's name.


Need Your Help

How can I see the source code of a gem installed on my machine?

ruby-on-rails ruby devise

I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (excep...

How to make GWT Spring project as maven enabled

gwt spring-mvc gwt-2.4 gwt-maven-plugin

I have a project which we implemented using GWT and Spring.