Home > Net >  C main function const char* [] vs char*- []
C main function const char* [] vs char*- []

Time:12-03

I don't understand what the difference between

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

and

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

is.

I'm aware of the difference between a char*[] and const char*[] but I wonder why one would like to use the latter.

Are there use cases where one would want to change command line arguments? What's the best practice about adding const?

CodePudding user response:

int main(int argc, char *argv[]) is a defined way of declaring main for a hosted environment according to the C standard, per C 2018 5.1.2.2.1 1. int main(int argc, const char *argv[]) is not.

It is good to use const where applicable to indicate that the pointed-to objects will not change, but it must be used appropriately. The types char *[] and const char *[] are not compatible and are not interchangeable as parameter declarations or argument types. If main is declared with const char *argv[], the behavior is not defined by the C standard.

As for why the prescribed declaration is char *argv[] rather than const char *argv[], that is partly historical and partly because some techniques for processing command-line arguments modify the arguments in place.

CodePudding user response:

The first one (char** argv) is defined by the C11 standard:

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¹⁰) or in some other implementation-defined manner.

¹⁰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.

You could argue that const char is "equivalent" to char in the way the C11 standard is defining it. However, the standard also says this about the 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.

So it would seem const is not C standard.

This answer details if it's OK to modify main parameters. The array char **argv is allocated at runtime, and modifying it doesn't affect any program execution.

As to why const isn't used as a declaration, it mainly boils down to historical practices. This question's answers detail why const char **argv might be used instead of its non-const.

  • Related