main - argc, argv[]

Are there any other parameters of main() function
besides argc and argv[]?



argumentum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
    cout<<"The number of arguments of the command line (plus the command): "<<endl;
    cout<<"argc: "<<argc<<endl<<endl;

    cout<<"The elements of the command line: "<<endl;

    for(int i=0;i<argc;i++)
        cout<<"argv["<<i<<"]= "<<argv[i]<<endl;

    cout<<endl;

    return 0;
}



argumentum.exe run with a b c d e f arguments:

C:\Cpp_files>argumentum.exe a b c d e f


Command line output:
1
2
3
4
5
6
7
8
9
10
11
The number of arguments of the command line (plus the command): 
argc: 7

The elements of the command line: 
argv[0]= argumentum.exe
argv[1]= a
argv[2]= b
argv[3]= c
argv[4]= d
argv[5]= e
argv[6]= f
Oooh, I've found another one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main (int argc, char **argv, char * env[])
{
    cout<<"The number of  the arguments of the command line (plus the command): "<<endl;
    cout<<"argc: "<<argc<<endl<<endl;

    cout<<"The environment variables: "<<endl<<endl;
    int i=0;
    while(*env) // repeat until it is not NULL, array env is closed by NULL
        cout<<"env["<<i++<<"]= "<<*env++<<endl;
        // array env contains pointers to character sequences
        // where the character sequences are the environment variables

    cout<<endl;

    return 0;
}


The env parameter is a nonstandard extension. The only two standard C++ versions of main are no parameters and the argc, argv version.
The number of arguments that main takes is platform AND usage dependent. "Standard" main takes between zero and three arguments (OS dependent), WinMain takes four arguments, DLL_Main takes three arguments etc...
thanks
Topic archived. No new replies allowed.