108
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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dinushan
  • 2,067
  • 6
  • 30
  • 47
  • 40
    You are free to declare `argc` as `const`. – Oliver Charlesworth Dec 13 '13 at 03:11
  • 14
    I have seen many production quality code that do this: `--argc` – Aniket Inge Dec 13 '13 at 03:12
  • 2
    I'm thinking it has to do with C legacy, but wouldn't know how. – Luis Machuca Dec 13 '13 at 03:12
  • 1
    See an example: http://www.opensource.apple.com/source/awk/awk-1.2/main.c – leonbloy Dec 13 '13 at 03:12
  • @LuisMachuca obviously C is born before C++ – zinking Dec 13 '13 at 05:47
  • 14
    I suspect that "Use const whenever possible" refers to things passed by reference, where any modification within the function persists once the function has exited. This is not true with things passed by value, like an integer, so there's nothing to be gained by making it `const`; indeed, passing `argc` as a `const int` means that you can't then use `argc` as, say, a counter inside the function. – Steve Melnikoff Dec 13 '13 at 11:32
  • 4
    @SteveMelnikoff : I don't agree that there is nothing to gain by making `const` a pass-by-value parameter. See eg http://stackoverflow.com/a/8714278/277304 and http://stackoverflow.com/a/117557/277304 – leonbloy Dec 13 '13 at 13:34
  • The historical explanation is that `main`'s function signature is unchanged since the days of Unix 7, and there *was* no `const` back then (it was a shiny new feature of C89). – zwol Dec 13 '13 at 20:36

7 Answers7

118

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.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • 1
    -1 Re "Some UNIX APIs, such as getopt, actually do manipulate argv[], so it can't be made const for that reason also.". One can freely add a top-level `const` to `argv`, without affecting what any C function can do. Also, `getopt` is declared as `int getopt(int argc, char * const argv[], const char *optstring);`. And here the `const` is not top level, but declares the pointers to be `const`, a promise to not modify them (although the strings that they point to might conceivably be modified). – Cheers and hth. - Alf Dec 13 '13 at 04:31
  • @Cheersandhth.-Alf : `getopt` _permutes_ the `argv[]` array by default, but does not modify the strings. (The word _permute_ comes directly from the man page.) That means that the `argv` array itself is not `const` even if the strings it points to are. How is permuting the `argv[]` array _not_ manipulating it? – Joe Z Dec 13 '13 at 04:35
  • @Cheersandhth.-Alf : I don't understand the -1. The statement you highlighted was not inaccurate. `getopt` modifies the `argv[]` array passed to `main`. Sure, you can add _some_ amount of `const`-ness to `argv[]` and not run afoul of `getopt`, but the array itself is not `const` and cannot be `const`. Storage passed to `main` and then to `getopt` gets mutated by `getopt`, which is all I said. – Joe Z Dec 13 '13 at 04:38
  • @Cheersandhth.-Alf : BTW, you misunderstand the `getopt` prototype: `getopt` can write whatever it likes to the `argv` array. It promises not to modify the characters pointed to by the pointers stored in the `argv` array. ie. `getopt` _can_ do `argv[0] = "42";` but it can't do `argv[0][0] = '4';`. – Joe Z Dec 13 '13 at 04:41
  • 2
    I'm sorry, [the docs I looked at](http://man7.org/linux/man-pages/man3/getopt.3.html) are inconsistent: the signature does not permit such permutation. See [example](http://coliru.stacked-crooked.com/a/6fcab492c134b11c). However, the following text says it does permute. So, I'm removing the downvote. – Cheers and hth. - Alf Dec 13 '13 at 04:44
  • 1
    That is, you will have to do some edit to "unlock" so I can remove the downvote. And no, you're misunderstanding the prototype. Look at the example I provided and the compilation error, "assignment of read only location". – Cheers and hth. - Alf Dec 13 '13 at 04:47
  • 1
    @Cheersandhth.-Alf : It's interesting... I looked up the prototype in the header, and it has `char* const* ___argv` there, but the interface actually adheres to `const char **`. Such confusion. I had confused the precedence of the `[]` and the `*` with respect to the `const`. My apologies. – Joe Z Dec 13 '13 at 04:48
  • @Joe Z, since you're already incrementing argv, why not: while (++argv) cout << *argv << std::endl; – Alex Dec 17 '13 at 21:11
  • @Alex: I suppose you could; it was just an example, and not intended to be exhaustive. – Joe Z Dec 18 '13 at 01:36
  • 1
    FYI: The GNU `getopt()` knows it does not follow the standard because it pays attention to the `POSIXLY_CORRECT` environment variable to make it behave correctly. In particular, POSIX does not permute the arguments, and does not look for options after the first non-option argument. – Jonathan Leffler Dec 21 '13 at 14:31
38

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Interesting. On a semi-related note, I ran into a crash-causing bug that turned out to be because Qt's `QApplication(argc,argv)` constructor [was defined with `argc` by reference](http://qt-project.org/doc/qt-5.0/qtwidgets/qapplication.html#QApplication). That surprised me. – HostileFork says dont trust SE Dec 13 '13 at 03:31
24

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.

razeh
  • 2,725
  • 1
  • 20
  • 27
8

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    If you're developing for Windows with cygwin or another libc that properly supports UTF-8 (as far as I know, cygwin is the only one at present but I have someone working on another option), the standard `main` signature works fine and you can receive arbitrary Unicode arguments. – R.. GitHub STOP HELPING ICE Dec 13 '13 at 04:46
  • @R they also work fine on most *nix platforms. the issue is not whether there are platforms where they work. but, **very nice initiative**, and as it happens i'm doing the same, more or less seriously – Cheers and hth. - Alf Dec 13 '13 at 04:58
4

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.

George
  • 15,241
  • 22
  • 66
  • 83
2

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) {}
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • Note that the variants without an explicit return type are no longer valid in C99, let alone C11, though compilers continue to allow them for reasons of backwards compatibility. C++ compilers should not accept the variants without a return type. – Jonathan Leffler Dec 13 '13 at 15:11
  • @JonathanLeffler Yep, yet the last ideone example there (http://ideone.com/m1qc9c) is G++ 4.8.2 with `-std=c++11`. Clang also accepts it but does give `warning: only one parameter on 'main' declaration [-Wmain]`, and MSVC only protests about the missing return type specifier and lack of a reference to argc. Again, 'shenanigans' and 'leeway' :) – kfsone Dec 13 '13 at 19:44
1

Aside from the historical reasons, a good reason to keep argc and argv non-const is that the compiler implementation doesn't know what you're going to do with the arguments to main, it just knows that it must give you those arguments.

When you are defining your own functions and associated prototypes, you know which parameters you can make const and which ones your function will modify.

Taken to an extreme, you could declare that all parameters to all functions should be declared const, and then if you had a reason to change them (e.g. decrement an index to search through an array), you'd have to make local non-const variables and copy the const arguments' values into those variables. That makes for busy-work and extra LOC with no real benefit. A decent static analyzer will pick up if you're not modifying the value of an argument, and recommend that you make the parameter const.

Sam Skuce
  • 1,666
  • 14
  • 20