Is it mandatory to write int main (int argc, char * argv []), do not use other names instead of argc and * argv []?

I am having some problems with Visual Studio command line arguments in the project parameters themselves (in the section "Configuration Properties" β†’ "Debugging").

I wrote this " int main(int argc) " after " int main(int main_i) " did not work. On the MS Visual Studio Properties page, the command line argument for the project, I'm not sure what I should write. I only want to pass a single integer to determine whether the file should be read or written this way, just int argc. I don't need char *argv[] . I tried several values ​​in the text field of the command line argument, but it does not reach the exe file, when it is executed, it shows that I did not enter at all.

Could you just provide me with a simple example of what I need to enter in the MSVS C ++ command line argument space? I just can't find an example of how to pass MSVS command line arguments.

I also don't understand, WHY do I need to rebuild the whole project, even when I just changed the value of the command line argument?

+4
source share
6 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 should do this:

 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 the main function to have specific sets of parameters. One of the valid combinations is (int, char *[]) . You can name the parameters whatever you want, but they are almost always called argc and argv .

+9
source

Names are just a well-established convention, you can use whatever names you want (although this may cause some eyebrows), however, I only saw code that used both arguments in that order.

In the days before ANSI, you could just have int main() , and it still is .. but these days argc argv usually shipped.

You cannot just specify one of the parameters.

+5
source

Variable names apply only to the compiler. They will be converted to binary addresses. You can pass up to 3 parameters for the main one.

Here is what I tried -

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

As you can see, the names are not 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, and the rest, after the third, will be invalid. It will compile anyway -

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

And just for fun, you can even do it -

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

Even a simple main allowed, and you can still access these 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 the GetCommandLine function to access the command line. An important aspect of missing / using the main argument is that you can still use command line arguments in any function .

+4
source

Like the others mentioned, it is simply a matter of convenience and what Kernigan and Richie used these arguments in the first version of C, which he wore in this way.

You are allowed to use anything freely.

Here is 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 am using Visual C ++ 2010 Express. If you do this, you can press Alt + F7 to get the project properties, go to "Configuration Properties"> "Debug" and enter the command line arguments in Command Arguments .

What is it.

+1
source

You must use char *argv[] if you want to read the values ​​of command line arguments. Otherwise, you can refuse it.

If you just want one whole to separate two things, you have one way to do it. Just pass the command line argument for one thing and do not pass any others. Now you do not need char *argv[] . Now you pass the command line argument for only one thing, then the argc value is 2 , i.e. The name of the executable file + command line argument.

And if you do not pass the command line argument, then its value will be only 1 , that is, the executable name.

+1
source

All Articles