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?
Because
argc
is a local variable (and, in C++, not a reference or something), and because the special place ofmain
means that backwards compatibility shenanigans grant it a huge amount of leeway with no compelling reason to force applications to make it const.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:
http://ideone.com/m1qc9c, C++ example
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 aconst
.But for
argv
you can not make the character dataconst
without thereby changing the function signature. Which means that it's then not one of the standardmain
function signatures, and will not have to be recognized as amain
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 theGetCommandLine
API function.Summing up, nothing prevents you from adding
const
toargc
, but the most usefulconst
-ness onargv
would give you a non-standardmain
function, most probably not recognized as such. Happily (in an ironic way) there are good reason to not use the standardmain
arguments for portable serious code. Quite simply, for the in-practice they only support old ASCII, with only English alphabet letters.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 manipulateargv[]
, so it can't be madeconst
for that reason also.(Aside: Interestingly, although
getopt
's prototype suggests it won't modifyargv[]
but may modify the strings pointed to, the Linux man page indicates thatgetopt
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
onargc
andargv
wouldn't buy much, and it would invalidate some old-school programming practices, such as:I've written such programs in C, and I know I'm not alone. I copied the example from somewhere.
The C standard (ISO/IEC 9899:2011) says:
Note the last bullet point. It says that both
argc
andargv
should be modifiable. They don't have to be modified, but they may be modified.The signature of
main
is somewhat of a historical artifact fromC
. HistoricallyC
did not haveconst
.However, you can declare your parameter
const
since the effects of const are compile time only.argc
is not normally a constant because the function signature formain()
pre-datesconst
.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.