Why is argc not a constant?

2019-01-21 12:10发布

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

As Effective C++ Item#3 states "Use const whenever possible", I start thinking "why not make these 'constant' parameters const"?.

Is there any scenario in which the value of argc is modified in a program?

7条回答
劫难
2楼-- · 2019-01-21 12:23

Because argc is a local variable (and, in C++, not a reference or something), and because the special place of main means that backwards compatibility shenanigans grant it a huge amount of leeway with no compelling reason to force applications to make it const.

main() {}

int main() {}

main() { return 0; }

main(int argc, char* argv[]) { return 0; }

int main(const int argc, const char** argv) { /* no return*/ }

these and many other variations will compile on a wide range of C and C++ compilers.

So ultimately it's not that argc isn't const, just that it doesn't have to be, but it can be if you want it to be.

http://ideone.com/FKldHF, C example:

main(const int argc, const char* argv[]) { return 0; }

http://ideone.com/m1qc9c, C++ example

main(const int argc) {}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-21 12:37

A top level const on a formal argument is not part of the function type. You can add it or remove it as you like: it only affects what you can do with the argument in the function implementation.

So for argc you can add freely add a const.

But for argv you can not make the character data const without thereby changing the function signature. Which means that it's then not one of the standard main function signatures, and will not have to be recognized as a main function. So, not a good idea.


A good reason for not using the standard main arguments in non-toy programs is that in Windows they are not able to represent actual program arguments such as filenames with international characters. That's because in Windows they are by very strong convention encoded as Windows ANSI. In Windows you can implement some more portable argument access facility in terms of the GetCommandLine API function.


Summing up, nothing prevents you from adding const to argc, but the most useful const-ness on argv would give you a non-standard main function, most probably not recognized as such. Happily (in an ironic way) there are good reason to not use the standard main arguments for portable serious code. Quite simply, for the in-practice they only support old ASCII, with only English alphabet letters.

查看更多
乱世女痞
4楼-- · 2019-01-21 12:39

In this case, history is a factor. C defined these inputs as "not constant", and compatibility with (a good portion of) existing C code was an early goal of C++.

Some UNIX APIs, such as getopt, actually do manipulate argv[], so it can't be made const for that reason also.

(Aside: Interestingly, although getopt's prototype suggests it won't modify argv[] but may modify the strings pointed to, the Linux man page indicates that getopt permutes its arguments, and it appears they know they're being naughty. The man page at the Open Group does not mention this permutation.)

Putting const on argc and argv wouldn't buy much, and it would invalidate some old-school programming practices, such as:

// print out all the arguments:
while (--argc)
    std::cout << *++argv << std::endl;

I've written such programs in C, and I know I'm not alone. I copied the example from somewhere.

查看更多
Summer. ? 凉城
5楼-- · 2019-01-21 12:44

The C standard (ISO/IEC 9899:2011) says:

5.1.2.2.1 Program startup

¶1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

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

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

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

or equivalent;10) or in some other implementation-defined manner.

¶2 If they are declared, the parameters to the main function shall obey the following constraints:

  • The value of argc shall be nonnegative.
  • argv[argc] shall be a null pointer.
  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
  • The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and so on.

Note the last bullet point. It says that both argc and argv should be modifiable. They don't have to be modified, but they may be modified.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-21 12:46

The signature of main is somewhat of a historical artifact from C. Historically C did not have const.

However, you can declare your parameter const since the effects of const are compile time only.

查看更多
疯言疯语
7楼-- · 2019-01-21 12:47

argc is not normally a constant because the function signature for main() pre-dates const.

Since argc is a stack variable, changing it won't affect anything other than your own command line processing.

You are, of course, free to declare it const if you want.

查看更多
登录 后发表回答