Argument parameters in main function?

Why do some programs have parameters in the main function and some do not? I see this a lot and have always wondered.

Some look like this...

int _tmain(int argc, _TCHAR* argv[])


And some look like this...

int main()


Thanks,
Nick
The first one isn't standard, but when the signature is as follows:
int main(int argc,char** argv)
Then argc is the number of program arguments and argv points to an array with the program arguments.
Last edited on
I have never tried this, maybe a stupid question. But how do you start a program with parameters?
@stoffe1100
If you run your program from the command line you just write the arguments after the executable file name.
Think as if the main() function is called by the operating system itself, and passed those arguments.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main(int argc, char *argv[])
{
    std::cout << "Received " << argc << " arguments.\n";

    for (int i=0; i < argc; ++i)
        std::cout << "argv[" << i << "] = " << argv[i] << '\n';

    std::cout << std::endl;
}
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\crazy.c++\args>g++ args.cpp -o args.exe

c:\crazy.c++\args>args.exe Mary had a little lamb.
Received 6 arguments.
argv[0] = args.exe
argv[1] = Mary
argv[2] = had
argv[3] = a
argv[4] = little
argv[5] = lamb.


c:\crazy.c++\args>


Edit: so when you use the simple form, without arguments, you simply make it clear that you'll ignore any arguments you get.
Last edited on

@i2Fluffy

Why do some programs have parameters in the main function and some do not? I see this a lot and have always wondered.

Some look like this...

int _tmain(int argc, _TCHAR* argv[])

And some look like this...

int main()


The both declarations of the main are standard provided that in the first declaration _TCHAR is a typedef name for char.

The first declaration is used in two cases. The first is when a programmer need to use parameters passed by environment. The second is when a programmer is lazy and use the declaration of the main with parameters that some IDEs provide by default though he is not going to use them.
The second declaration means that parameters are not important and may be omitted.
Last edited on
Topic archived. No new replies allowed.