Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is argc not a constant?

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?

like image 258
Dinushan Avatar asked Dec 13 '13 03:12

Dinushan


People also ask

Is argc always at least 1?

c. As you can see, the first argument ( argv[0] ) is the name by which the program was called, in this case gcc . Thus, there will always be at least one argument to a program, and argc will always be at least 1.

Is argc a global variable?

The __argc global variable is a count of the number of command-line arguments passed to the program.

Why is argc an int?

because int was, in a sense, more important back then. Everything was an int. C evolved in part from a language that did not even have types. Every single varable was a word , which is what int originally was used for.

What is argc equal to?

argc = Arguement count. This is used to determine the number of command line arguements a program is run with. argv is called the arguement vector which holds the names of all command line arguements including the name of your program.


2 Answers

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.

like image 184
Joe Z Avatar answered Oct 07 '22 14:10

Joe Z


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.

like image 23
Jonathan Leffler Avatar answered Oct 07 '22 15:10

Jonathan Leffler