Are there any other arguments that main () can accept?

I recently discovered the following in my searches regarding environment variables in C:

int main (int argc, char *argv[], *char *envp[])

      

I've searched around and can't find anything convincing about my question.

What are all the available arguments that can take main()

?

+3


source to share


2 answers


The C99 and C11 project standards allow a certain set of parameters to be implemented before main

, these parameters will be specific to these systems (not portable). From the section 5.1.2.2.1

:

[...] or some other way of implementing [...]

The only additional options I can find are envp

and apple

, we can find a good description in the Wikipedia C and C ++ section on entry points :



Other platform-specific formats are also allowed by the C and C ++ standards, except that in C ++ the return type must always be int; [6] for example, Unix (although not POSIX.1) and Microsoft Windows have a third argument giving the programming environment, otherwise available via getenv in stdlib.h:

int main(int argc, char **argv, char **envp);

      

Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information such as the path to the executable binary: [7]

int main(int argc, char **argv, char **envp, char **apple);

      

It looks like Windows has a Microsoft specific wmain that accepts wchar_t

:

int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);

      

+4


source


An alternative is the widescreen version:

int main(int argc, wchar_t* argv[], wchar_t* envp[])

      



The function main

is specified in the language specification as follows, no other function signature is provided other than the get-out clause for function-specific entry points (such as the Apple 3rd parameter apple

) or Microsoft WinMain

.

5.1.2.2.1 Starting the program

The function called when the program starts is called main

. The implementation does not declare a prototype for this function. It must be defined with return type int and no parameters:

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

      

or with two parameters (called argc

and here argv

, although any names can be used since they are local to the function in which they are declared):

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

      

or equivalent or some other way of implementation. If declared, parameters of the main function must obey the following restrictions:

  • The value argc

    must be non-negative.
  • argv[argc]

    must be a null pointer.
  • If the value is argc

    greater than zero, the elements of the array argv[0]

    , argv[argc-1]

    inclusive, must contain pointers to strings that are specified by the implementation-defined value of the host environment before running the program. The goal is to provide information about the program, defined before the program was run, from another location in the hosted environment. If the host environment is unable to supply strings with upper and lower case letters, the implementation must ensure that the strings are received in lower case.
  • If argc is greater than zero, the string pointed argv[0]

    to by the program name argv[0][0]

    must be null if the program name is not available from the host environment. If the value argc

    is greater than one, the lines pointed to argv[1]

    through argv[argc-1]

    represent program parameters.
  • The parameters argc

    and argv

    , and the strings pointed to by the array argv

    can be modified by the program and retain their last saved values ​​between program start and program termination.
+3


source







All Articles