main - argc, argv[]

Mar 19, 2013 at 7:32pm
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
Mar 20, 2013 at 6:24pm
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;
}


Mar 20, 2013 at 6:33pm
The env parameter is a nonstandard extension. The only two standard C++ versions of main are no parameters and the argc, argv version.
Mar 20, 2013 at 7:46pm
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...
Mar 22, 2013 at 2:37pm
thanks
Topic archived. No new replies allowed.